Search code examples
phpemailimap

PHP IMAP email body decode issue


I am working on my PHP to store the emails in the sent folder. I have got a problem with storing the url in the emails, because I will be getting id and msgid in the img tag src when it should be id= and msgid=. I have been researching for hours and I couldn't be able to find out how to store the = in the html as I keep getting the and when I store the emails in the sent folder.

When I try this:

<?php
require_once "Mail.php";
require_once "Mail/mime.php";
require_once('Mail/IMAPv2.php');

// Connect to the server:
$username = 'myusername';
$password = '*************';

$sent = '{imap.example.com:993/imap/ssl/novalidate-cert}INBOX.Sent';
$sent_mailbox = imap_open($sent, $username, $password);

$from_email = $_POST['send_from'];
$to_email = $_POST['send_to'];
$subject = $_POST['emailsubject'];
$message = '<div id="emailMessage" class="row" style="padding: 5px 30px;margin-top: 12px;"><div><div dir="ltr"><img src="http://example.com/u/?id=1562453336&amp;attid=0.2&amp;msgid=1644988591229814716&amp;view=attachment&amp;display=view" width="452" height="302" class="CToWUd a6T" tabindex="0"><br><div><br></div><div><br></div><div><img src="http://example.com/u/?id=1562453336&amp;attid=0.1&amp;msgid=1644988591229814716&amp;view=attachment&amp;display=view" class="CToWUd"><br></div><div><br></div><div><br></div><div>



';

$messageID = sprintf("<%s.%s@%s>",
        base_convert(microtime(), 10, 36),
        base_convert(bin2hex(openssl_random_pseudo_bytes(8)), 16, 36),
        'example.com');


imap_append($sent_mailbox, $sent,
    "From: ".$from_email."r\n".
    "To: ".$to_email."\r\n".
    "Subject: ".$subject."\r\n".
    "Date: ".date("r", strtotime("now"))."\r\n".
    "Message-ID: ".$messageID."\r\n".
    "MIME-Version: 1 \r\n".
    "Content-Type: multipart/mixed; boundary=\"$boundary1\"\r\n".
    "\r\n\r\n".
    "--$boundary1\r\n".
    "Content-Type: multipart/alternative; boundary=\"$boundary2\"\r\n".
    "\r\n\r\n".
    // ADD Plain text data
    "--$boundary2\r\n".
    "Content-Type: text/plain; charset=\"utf-8\"\r\n".
    "Content-Transfer-Encoding: quoted-printable\r\n".
    "\r\n\r\n".
    $message."\r\n".
    "\r\n\r\n".
    // ADD Html content
    "--$boundary2\r\n".
    "Content-Type: text/html; charset=\"utf-8\"\r\n".
    "Content-Transfer-Encoding: quoted-printable \r\n".
    "\r\n\r\n".
    $message."\r\n".
    "\r\n\r\n".
    "--$boundary1\r\n".
    "\r\n\r\n".
    // ADD attachment(s)
    $attachment1.
    "\r\n\r\n".
    "--$boundary1--\r\n\r\n",
    "\\Seen"
 );

Here is what it will show next to the id and next to the msgid like this:

<img src="http://example.com/u/?id62453336&amp;attid=0.2&amp;msgid44988591229814716&amp;view=attachment&amp;display=view" width="452" height="302" class="CToWUd a6T">

I have tried this:

$message = html_entity_decode($message);

And I have also tried this:

$message = htmlspecialchars($message);

It doesn't make any difference so I dont know what to do.

Can you please show me an example of what I should use to store the html emails in the sent folder when I have the id= and msgid= in the img tag url without display the and ?

Thank you.


Solution

  • If you are using quoted printable, you need to escape your equals signs(=3D).