HTML email, good in Gmail but random string in iMail/Outlook

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

HTML email, good in Gmail but random string in iMail/Outlook

Post by $var »

Hello DevNet,
I am using a function that I found to send html formatted emails. I plunked it in and it worked with no issue. Until it was tested by someone using Outlook or iMail, who got a LONG string of indecipherable data. I am using Gmail as my mail client, and I happily/ignorantly getting the nicely formatted emails.

The function:

Code: Select all

//MAILING FUNCTION
function sendHTMLemail($HTML,$from,$to,$subject)
{
    $headers = "From: $from\r\n"; 
    $headers .= "MIME-Version: 1.0\r\n"; 
    $boundary = uniqid("HTMLEMAIL"); 
      
    $headers .= "Content-Type: multipart/alternative;".
                "boundary = $boundary\r\n\r\n"; 

    $headers .= "This is a MIME encoded message.\r\n\r\n"; 

    $headers .= "--$boundary\r\n".
                "Content-Type: text/plain; charset=ISO-8859-1\r\n".
                "Content-Transfer-Encoding: base64\r\n\r\n"; 
                
    $headers .= chunk_split(base64_encode(strip_tags($HTML))); 

    $headers .= "--$boundary\r\n".
                "Content-Type: text/html; charset=ISO-8859-1\r\n".
                "Content-Transfer-Encoding: base64\r\n\r\n"; 
                
    $headers .= chunk_split(base64_encode($HTML)); 

    mail($to,$subject,"",$headers);
}
I am guessing that perhaps the character encoding is different for each? Or, maybe the header requirements are different for Outlook, Apple, and Gmail?
If you have experienced this, I'd love to know how you've solved this problem.

Thank you
s992
Forum Contributor
Posts: 124
Joined: Wed Oct 27, 2010 3:06 pm

Re: HTML email, good in Gmail but random string in iMail/Out

Post by s992 »

Looks like it has something to do with the base64 encoding $HTML. Google for outlook+base64 and imail+base64 and you'll see that there are a good amount of posts about it.
Post Reply