Mulitpart MIME email text and HTML shows both parts - php

I've been banging my head on this one for awhile and have been unable to find any helpful articles on my issues. I'm writing a PHP site using the built in mail function for some quick confirmation emails. I realize that there is a fair amount of prejudice against the built in mail function but it has been working well for me up to this point and I would like to be able to continue using it. When I send just a plain text email it all works great, as if I send just a HTML email. However if I try to do a multipart Text/HTML email both versions show up in my email client (tried both thunderbird and gmail). I'm hoping someone here can help me figure out what I'm doing wrong (besides using mail() instead of PHPMail). Here is my code snippet
$uid = md5(uniqid(time()));
$strSubject = "Confirmation for $strEventName on $strEventDate";
$strHTMLMsg = "<h1><center>You are confirmed for the following event:</center></h1><br>\n$strEvenDetails";
$strMsg = strip_tags($strHTMLMsg);
$toEmail = "\"$strName\" <$strEmail>";
$header = "$FromEmail\n";
$header .= "MIME-Version: 1.0\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\n\n";
$header .= "Content-Transfer-Encoding: 7bit\n";
$header .= "This is a MIME encoded message.\n\n";
$header .= "--".$uid."\n";
$header .= "Content-type:text/plain; charset=UTF-8\n";
$header .= "Content-Transfer-Encoding: 7bit\n\n";
$header .= $strMsg."\n\n";
$header .= "--".$uid."\n";
$header .= "Content-type:text/HTML; charset=UTF-8\n";
$header .= "Content-Transfer-Encoding: 7bit\n\n";
$header .= $strHTMLMsg."\n\n";
$header .= "--".$uid."\n";
$header .= "Content-Type: application/ics; name=\"".$strFileName."\"; method=PUBLISH; charset=UTF-8\n";
$header .= "Content-Transfer-Encoding: 7bit\n";
$header .= "Content-Disposition: attachment; filename=\"".$strFileName."\"\n\n";
$header .= $strICSEvent."\n\n";
$header .= "--".$uid."\n";
$bSuccess = mail($toEmail,$strSubject,"",$header);

Necrobump, but I arrived here after googling, so for others with the same search terms: multipart/mixed implies that you will view all parts. For both HTML and text parts where only one should be visible, use multipart/alternative instead. The last part gets the highest priority.
Also see Mail multipart/alternative vs multipart/mixed for more info, and info about "stacking" mime content.

Related

Attachment is going to Gmail but not other mailboxes

These are the headers which I am using to send email with an attachment, however the attachment only gets through to Gmail. For all other mailboxes I tried, only the message content arrives, without the attachment.
# Define the main headers
$header = "From:$from_email1\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; ";
$header .= "boundary=$num\r\n";
$header .= "--$num\r\n";
# Define the message section
$header .= "Content-Type: text/html\r\n";
$header .= "Content-Transfer-Encoding:8bit\r\n\n";
$header .= "$message1\r\n";
$header .= "--$num\r\n";
# Define the attachment section
$header .= "Content-Type: multipart/mixed; ";
$header .= "name=\"test.txt\"\r\n";
$header .= "Content-Transfer-Encoding:base64\r\n";
$header .= "Content-Disposition:attachment; ";
$header .= "filename=\"$cv_view12\"\r\n\n";
$header .= "$encoded_content\r\n";
$header .= "--$num--";
# Send email now
$retval = mail ( $to, $subject1, $message1, $header );
What's the size of the attachment?
Gmail has a 25mb attachment limit, which is a lot larger than many email providers.
Now, I'd expect you'd get a bounce-back message telling you that the attachment is too big if this is the case, but if the email address you're using for "From" is not valid, this wouldn't bounce back to anywhere you could see.
I'd start by setting a valid "From" address and see if you get any messages back as to why.
I also highly recommend Swift Mailer:
http://swiftmailer.org/
Which takes a lot of the complexities out of sending mail, and may automatically fix a small typo in manually creating a mail message. There is also a lot of advanced functionality built-in which makes life much easier.

Yahoo showing blank email body for php 5.4 [duplicate]

I'm using PHP to send out a multipart/mixed message (plain text, html and attachments). However, whilst it works for most accounts, Yahoo, GMail and Sky seem to show blank emails. Where as everything else seems to display the email. ANY HELP WOULD BE GREATLY APPRECIATED!
My headers are
$headers .= "Content-Type: multipart/mixed; boundary=\"mixed-" . $random_hash . "-mixed\"\n";
$headers .= "MIME-Version: 1.0\n";
And the content is;
--mixed-7df05b31-mixed
Content-Type: multipart/alternative; boundary="alt-7df05b31-alt"
--alt-7df05b31-alt
Content-Type: text/plain; charset=utf-8
Hello how are you? I am just checking the mailing function.
Hopefully this will work!
Cheers.
--alt-7df05b31-alt
Content-Type: text/html; charset=utf-8
<div style="font-family:Arial, Helvetica, sans-serif; font-size: 10pt;">
<div>
Hello how are you? I am just <b>checking</b> the mailing function.<br><br>
Hopefully this will work!<br><br>
Cheers.</div></div>
--alt-7df05b31-alt--
--mixed-7df05b31-mixed
Content-Type: text/plain; name="abc.txt"
Content-Disposition: attachment; filename="abc.txt"
Content-Transfer-Encoding: base64
SEVMTE8gSlVTVCBURVNUSU5HIC4uLiA=
--mixed-7df05b31-mixed--
It may be an artifact from pasting, but try removing the blank space at the end of each boundary. (Highlight the text and you'll notice the boundaries have an extra space, but the closing boundaries-- do not)
Please verify whether your code is look like the following because the following code is working fine for me.
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$headers .= "This is a multi-part message in MIME format.\r\n";
$headers .= "--".$uid."\r\n";
$headers .= "Content-type:text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$headers .= $msg."\r\n\r\n";
$headers .= "--".$uid."\r\n";
$headers .= "Content-Type:application/html; name=\"".$filename."\" \r\n"; // use different content types here
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$headers .= $content."\r\n\r\n";
$headers .= "--".$uid."--";
Don't build your own MIME messages. use Swiftmailer or PHPMailer to do it for you.

CSV email attachment also appearing inline with the message body

I read all of the similar questions here and looked at many examples on other sites.
Despite my belief that I have this setup correctly, the content of my CSV attachment is displaying inline with the body message as well as showing up as an attachment. The attachment opens and renders correctly, but the body of the message looks like this:
Report Attached
payment_reconciliation.csv
Customer #,Invoice,Verifier,Post Date,Payment #,Status,Payment Date,Payment Amount
"456272","1395164","Verified - by Autumn on 2014-04-30 17:15:33","","135927","","2014-04-30","850.00"
"655469","1395163","Verified - by Autumn on 2014-04-30 17:13:17","","135926","","2014-04-30","2,275.00"
"351588","1395161","Verified - by Autumn on 2014-04-30 17:16:54","","135924","","2014-04-30","595.00"
"78444","1395160","Verified - by Autumn on 2014-04-30 ...
Here is the portion of the code that creates the headers:
$myfile = "payment_reconciliation.csv";
$uid = md5(uniqid(time()));
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/html; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: text/csv; name=\"".$myfile."\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$myfile."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";
mail($to, $subject, $message, $header);
Anyone spot something I'm missing? Thank you.

Embed Image in Email Message in MPDF Using PHP

Is there a way to embed an image in a message body in MPDF using php? Simply adding the HTML image tag as shown in the snippet below causes the actual tag to display in an e-mail.
$message = '<img src="Signature%20Card.jpg"/>';
Thank you for any ideas.
By the way, I am also sending an attached PDF. That part works fine though; only embedding the image does not work.
Below is the code for actually sending the e-mail:
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/pdf; name=\"".$filename."\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";
$is_sent = #mail($mailto, $subject, "", $header);
You need to get the $message variable into the body of the email for this to work properly. Currently the code is going into the header rather than the email message body. Change these lines by removing the middle line:
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
//$header .= $message."\r\n\r\n";
$header .= "--".$uid."\r\n";
And in the final line of code, place the $message variable into the mail command:
$is_sent = #mail($mailto, $subject, $message, $header);

Blank Multipart email

I'm using PHP to send out a multipart/mixed message (plain text, html and attachments). However, whilst it works for most accounts, Yahoo, GMail and Sky seem to show blank emails. Where as everything else seems to display the email. ANY HELP WOULD BE GREATLY APPRECIATED!
My headers are
$headers .= "Content-Type: multipart/mixed; boundary=\"mixed-" . $random_hash . "-mixed\"\n";
$headers .= "MIME-Version: 1.0\n";
And the content is;
--mixed-7df05b31-mixed
Content-Type: multipart/alternative; boundary="alt-7df05b31-alt"
--alt-7df05b31-alt
Content-Type: text/plain; charset=utf-8
Hello how are you? I am just checking the mailing function.
Hopefully this will work!
Cheers.
--alt-7df05b31-alt
Content-Type: text/html; charset=utf-8
<div style="font-family:Arial, Helvetica, sans-serif; font-size: 10pt;">
<div>
Hello how are you? I am just <b>checking</b> the mailing function.<br><br>
Hopefully this will work!<br><br>
Cheers.</div></div>
--alt-7df05b31-alt--
--mixed-7df05b31-mixed
Content-Type: text/plain; name="abc.txt"
Content-Disposition: attachment; filename="abc.txt"
Content-Transfer-Encoding: base64
SEVMTE8gSlVTVCBURVNUSU5HIC4uLiA=
--mixed-7df05b31-mixed--
It may be an artifact from pasting, but try removing the blank space at the end of each boundary. (Highlight the text and you'll notice the boundaries have an extra space, but the closing boundaries-- do not)
Please verify whether your code is look like the following because the following code is working fine for me.
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$headers .= "This is a multi-part message in MIME format.\r\n";
$headers .= "--".$uid."\r\n";
$headers .= "Content-type:text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$headers .= $msg."\r\n\r\n";
$headers .= "--".$uid."\r\n";
$headers .= "Content-Type:application/html; name=\"".$filename."\" \r\n"; // use different content types here
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$headers .= $content."\r\n\r\n";
$headers .= "--".$uid."--";
Don't build your own MIME messages. use Swiftmailer or PHPMailer to do it for you.

Categories