Ecommerce Shopify WordPress Discussion

WordPress Email to Post publishing

how are you doing? I was creating the custom functionality using WordPress, that is publishing posts by sending emails. Everything is working fine except one thing, that is images are uploading as broken. The issue is very precistent. Here is the code snippet I'm using, if anyone knows the solution that will be very helpful. // Hook the function to WordPress initialization for one-time execution add_action('init', 'schedule_publish_post_from_email'); function schedule_publish_post_from_email() { // Schedule the event 1 second from now $timestamp = time() + 1; // Schedule a one-time event to execute the custom_publish_post_from_email function wp_schedule_single_event($timestamp, 'custom_publish_post_from_email'); } // Directly call the function add_action('custom_publish_post_from_email', 'custom_publish_post_from_email'); function custom_publish_post_from_email() { $mailserver = '{imap.gmail.com:993/imap/ssl/novalidate-cert}'; $username = 'email address'; $password = 'password'; // Connect to the mailbox $inbox = imap_open($mailserver, $username, $password) or die('Cannot connect to the mailbox: ' . imap_last_error()); // Get all emails $emails = imap_search($inbox, 'ALL'); // Process each email if ($emails) { // Get the latest email (change the index if you want to process other emails) $email_number = end($emails); // Check if the email has been processed before $email_uid = imap_uid($inbox, $email_number); if (!has_email_been_processed($email_uid)) { // Fetch email header information $header = imap_headerinfo($inbox, $email_number); // Check if the sender's email matches the desired email address $sender_email = strtolower($header->from[0]->mailbox . "@" . $header->from[0]->host); $desired_email = 'emial address'; if ($sender_email === $desired_email) { // Fetch the HTML part of the email using imap_fetchbody $email_body_html = imap_fetchbody($inbox, $email_number, "1.2"); // Sideload images and upload media $email_body_html = sideloadImages($inbox, $email_number, $email_body_html); // Create a new post without including email styles in the post content $post_data = array( 'post_title' => $header->subject, 'post_content' => $email_body_html, 'post_status' => 'draft', // Change to 'publish' if you want to publish immediately 'post_author' => 1, ); $post_id = wp_insert_post($post_data); // Check if the post was created successfully if (!is_wp_error($post_id)) { // Mark the email as read (optional) imap_setflag_full($inbox, $email_number, "\\Seen"); // Store the unique identifier to avoid processing the same email again mark_email_as_processed($email_uid); } else { // Handle error creating post error_log('Error creating post: ' . $post_id->get_error_message()); } } // Close the mailbox imap_close($inbox); } } } function has_email_been_processed($email_uid) { // Check if the email UID is stored in a custom field $processed_emails = get_transient('processed_emails'); return $processed_emails && in_array($email_uid, $processed_emails); } function mark_email_as_processed($email_uid) { // Store the email UID in a transient option with an expiration time $processed_emails = get_transient('processed_emails') ?: array(); $processed_emails[] = $email_uid; set_transient('processed_emails', $processed_emails, WEEK_IN_SECONDS); } function sideloadImages($inbox, $email_number, $html) { $doc = new DOMDocument(); @$doc->loadHTML($html); $images = $doc->getElementsByTagName('img'); foreach ($images as $image) { $src = $image->getAttribute('src'); // Tip 1: Use getimagesize on remote URLs $image_size = @getimagesize($src); if ($image_size !== false) { $img_cid = $src; $img_data = imap_fetchbody($inbox, $email_number, $img_cid); // Determine MIME type using getimagesize $finfo = finfo_open(FILEINFO_MIME_TYPE); $mime_type = finfo_buffer($finfo, $img_data); finfo_close($finfo); // Generate a unique filename with appropriate extension $filename = md5(uniqid()) . '.' . pathinfo($img_cid, PATHINFO_EXTENSION); // Save the image data to a temporary file $tmp_file = wp_tempnam($filename); file_put_contents($tmp_file, $img_data); // Prepare the attachment data $attachment = array( 'post_mime_type' => $mime_type, 'post_title' => $filename, 'post_content' => '', 'post_status' => 'inherit', ); // Use media_handle_sideload to sideload the attachment $attachment_id = media_handle_sideload(array('file' => $tmp_file, 'name' => $filename), 0); // Check if the attachment was added successfully if (!is_wp_error($attachment_id)) { // Replace the CID with the attachment URL in the HTML $image->setAttribute('src', wp_get_attachment_url($attachment_id)); // Remove any surrounding <p> tags added by the Classic Editor $image_parent = $image->parentNode; while ($image_parent->nodeName === 'p') { $image_parent = $image_parent->parentNode; } // Insert the image back into the DOM without the surrounding <p> tags $image_parent->parentNode->insertBefore($image, $image_parent); $image_parent->parentNode->removeChild($image_parent); } else { // Handle error adding attachment error_log('Error adding attachment: ' . $attachment_id->get_error_message()); } // Delete the temporary file unlink($tmp_file); } } // Save the modified HTML back to the original variable return $doc->saveHTML(); } Posting by sneding emails in WordPress. Expecting to upload images without broken.
TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.