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.
Related
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.
I want to send an attachment with PHPmailer, but when I add the 'addattachment' string, the mail is being send in plain text. This is my code:
$headers .= "AddAttachment('downloads/file.pdf','file.pdf')\n";
$headers .= "Reply-To: Contoso <noreply#contoso.com>\n";
$headers .= "From: Contoso <info#contoso.com>\n";
$headers .= "Organization: Contoso \n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=UTF-8\n";
$headers .= "X-Priority: 3\n";
$headers .= "X-Mailer: PHP". phpversion() ."\n" ;
mail($to, $subject, $body, $headers,$param);
So the script is working without the 'AddAttachment' rule. How can I add an attachment succesfully?
Here is a tutorial how to send an attachment.
http://webcheatsheet.com/php/send_email_text_html_attachment.php#attachment
Its not enough to set only the header. You have to add the attachment as base64 encoded tring to your message.
$attachment = chunk_split(base64_encode(file_get_contents('attachment.zip')));
Perhaps its much easier to use a lib with do the work something like PHPMailer or Swift Mailer what i prefer.
Content-Type: application/zip; name="attachment.zip"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
I need your help!
I have some file that generates a mail with attachment in FPDF and what I need is configure the font family for the mail to Arial and Size 12px, I know how to do that in the PDF but not in the mail.
This is the code for mail with FPDF
$filename = "example.pdf";
// encode data (puts attachment in proper format)
$pdfdoc = $pdf->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc));
// main header (multipart mandatory)
$headers = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol;
$headers .= "Content-Transfer-Encoding: 7bit".$eol;
$headers .= "This is a MIME encoded message.".$eol.$eol;
// message
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$headers .= $message.$eol.$eol;
// attachment
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$headers .= "Content-Transfer-Encoding: base64".$eol;
$headers .= "Content-Disposition: attachment".$eol.$eol;
$headers .= $attachment.$eol.$eol;
$headers .= "--".$separator."--";
// send message
mail($to, $subject, "", $headers);
I hope you can help me, thanks !
Email needs to use inline styling, along the following lines:
<p style="font-family: Arial, sans-serif; font-size: 12px">This paragraph is formatted correctly</p>
Or you could put the style into a
<span>Here is lots of stuff.</span>
tag covering the relevant portions inside the
<body></body>
of the email.
I'm not sure why you are not putting $message as the third arg to mail() function. In any case, you will need to play with its content.
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.
I want to format content of the mail to show the content in different line.
here is my message contetn.
bu the \n and \r is not working in this case. it just shows all the content in one line.
$message = 'Thank you for using . We really appreciate your business.'."\r\n".'If you are making your payment by mail, please make the check out to "blah blah" and send it to:'."\n".'blah blah '."\n".'blah blah'."\n".'San Gabriel, CA 91776'."\n".'Please see the attached invoice in PDF format for easy saving & printing purposes.';
$attachment = chunk_split(base64_encode($pdfdoc));
$headers = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol;
$headers .= "Content-Transfer-Encoding: 7bit".$eol;
$headers .= "This is a MIME encoded message.".$eol.$eol;
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$headers .= $message.$eol.$eol;
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: application/pdf; name=\"".$filename."\"".$eol;
$headers .= "Content-Transfer-Encoding: base64".$eol;
$headers .= "Content-Disposition: attachment".$eol.$eol;
$headers .= $attachment.$eol.$eol;
$headers .= "--".$separator."--";
mail($_POST['add6'],$subject, $message, $headers);
how can i do that?
You're telling the email client the message is HTML, so the CR LF combination will be treated like any other whitespace.
To fix this, change the content type to show you are sending a plain text email
$headers .= "Content-Type: text/plain; charset=\"iso-8859-1\"".$eol;
Alternatively, turn your message into an HTML message - an easy way to do that in your case would be to run it through nl2br to turn the newlines into <br> tags
Yep you've got Content-Type: text/html, so the CR LF is being treated like whitespace. Either send it as Content-Type: text/plain or call nl2br on your contents.
Your content type is HTML so you should use br or p tags instead of line feeds
For \n to work it needs to be double quotes, not single. "\n" is the right thing, '\n' is wrong and will not work.