HTML Email mixed (html & attachement) coming thoough as plain text - php

I've got an HTML email being sent by PHP, with a pdf attached via base64 encoding. It all works, except the email comes through as plain text, so you see all HTML tags and base64 output - obviously not ideal. There must be something I'm missing here to ensure it gets read as HTML & attachment.
If anybody can help that would be fantastic!
My PHP:
<?php $to = $email;
$message = 'testing...';
$subject = 'Health Insurance Quote Request';
$headers = "From: Andy - CEO Health.com.au <" . strip_tags('info#health.com.au') . ">\r\n";
$headers .= "Reply-To: Andy - CEO Health.com.au <". strip_tags('info#health.com.au') . ">\r\n";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$email_message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$email_message .= "--{$mime_boundary}\n";
//get PDF URL
$data = chunk_split(base64_encode(file_get_contents('http://example.com/doge.pdf')));
$email_message .= "Content-Type: {\"application/pdf\"};\n" . " name=\"$product\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$product\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$email_message .= "--{$mime_boundary}\n";
mail($email, $subject, $email_message, $headers);
Example of output:
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="==Multipart_Boundary_xc9c6c260d1e99ba11f86b40c6c7848e0x"
This is a multi-part message in MIME format.
--==Multipart_Boundary_xc9c6c260d1e99ba11f86b40c6c7848e0x
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
testing...
--==Multipart_Boundary_xc9c6c260d1e99ba11f86b40c6c7848e0x
Content-Type: {"application/pdf"};
name="HeartPlus65"
Content-Disposition: attachment;
filename="HeartPlus65"
cwt/SGdVm9X6LXQwvq03c+afGdAA9NlSAk1G3NyG2S5pDSY+CycXKDnuBJ5gFRupvZva0H2CXQqA
t9M6DlWx646bhhsx8NXdde0ES4z+8FP03XNEWTPMc/lWObbIG4ET4qxQ57eCm1V8RZOOzqG6+pu7
mYERMqHobI26KpfZYx27x8ESi65xkqOYkdbFIJ1qm2HXUEGZC0B1Jr2c6qmHeo3VULGurdIKhERP
Q6EJqtQ6rrWPbLiqzHgOlVLrfaIU8Zxe2SUhi4QTauK9G0/qbmjb2TD0HNkakoNlLbDzCql32d4A
khPjiE9rBQZGOvRvtxnEaGAlXimw7Vp4tIsqkjkI2JQ1upHBRgTA31CjqPNBjdM9ISRKHl9MLvc0
LeZUH8J7GNA1R+8mJvqkR0p5f7LZEbSkt7c2fopKX79LsEe2O7nv6qzJrc1xGreCsS
....

It may be not having a -- at the end of the last boundary tag
ie $email_message .= "--{$mime_boundary}\n";
to $email_message .= "--{$mime_boundary}--\n";
I'm not sure it there is a difference between multipart/mixed and multipart/alternative but this works for me
# Setup mime boundary
$mime_boundary = 'Multipart_Boundary_x'.md5(time()).'x';
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/alternative; boundary=\"$mime_boundary\"\n";
$headers .= "Content-Transfer-Encoding: 7bit\n";
$body = "This is a multi-part message in mime format.\n\n";
# Add in plain text version
$body .= "--$mime_boundary\n";
$body .= "Content-Type: text/plain; charset=\"charset=us-ascii\"\n";
$body .= "Content-Transfer-Encoding: 7bit\n\n";
$body .= $text_content;
$body .= "\n\n";
# Add in HTML version
$body .= "--$mime_boundary\n";
$body .= "Content-Type: text/html; charset=\"UTF-8\"\n";
$body .= "Content-Transfer-Encoding: 7bit\n\n";
$body .= $html_content;
$body .= "\n\n";
#Attachments
if ($path!='' && $filename!=''){
$file_size = filesize($path.$filename);
$handle = fopen($path.$filename, "r");
$filecontent = fread($handle, $file_size);
fclose($handle);
$filecontent = chunk_split(base64_encode($filecontent));
$body .= "--$mime_boundary\n";
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
$body .= "Content-Transfer-Encoding: base64\r\n";
$body .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$body .= $filecontent."\r\n\r\n";
}
# End email
$body .= "--$mime_boundary--\n";

Related

php mail function can not send attached pdf file and message body

I want to send mail through php mail function. For that I googled it and found the code which send mail attached with pdf file. Result is fine, mail send but mail only send attached pdf file it can not send message body.
Here is Code:
<?php
$name = "myname";
$to = "receive#gmail.com";
$email = "sender#gmail.com";
$from = "myname";
$subject = "Here is your attachment";
$mainMessage = "Hi, here's the file.";
$fileatt = $_SERVER['DOCUMENT_ROOT']."/xxx/ticket.pdf";
$fileatttype = "application/pdf";
$fileattname = "ticket.pdf";
$headers = "From: $from";
// File
$file = fopen($fileatt, 'rb');
$data = fread($file, filesize($fileatt));
fclose($file);
// This attaches the file
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$message = "This is a multi-part message in MIME format.\n\n" .
"-{$mime_boundary}\n" .
"Content-Type: text/html; charset=\"iso-8859-1\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$mainMessage . "\n\n";
$data = chunk_split(base64_encode($data));
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatttype};\n" .
" name=\"{$fileattname}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileattname}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"-{$mime_boundary}-\n";
// Send the email
if(mail($to, $subject, $message, $headers)) {
echo "The email was sent.";
}
else {
echo "There was an error sending the mail.";
}
?>
I can not identify where i done mistake, please help me and give some suggestion.
Note: Don't Give suggestion to use PHPMailer.
Thanks
You can try this code:
$to = "youremail#gmail.com";
$from = "Myname <sender#gmail.com>";
$subject = "Test Attachment Email";
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// attachment name
$filename = "document.pdf";
//$pdfdoc is PDF generated by FPDF
$pdfdoc = "/opt/transmail/2018-03-07_32_11564_invoice.pdf";
$attachment = chunk_split(base64_encode($pdfdoc));
// main header
$headers = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
// no more headers after this, we start the body! //
$message = "Thanks";
$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$body .= "This is a MIME encoded message.".$eol;
// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;
// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";
// send message
if (mail($to, $subject, $body, $headers)) {
echo "mail send ... OK";
} else {
echo "mail send ... ERROR";
}
hope this will work for you.

After sending email a security warning comes in email body

I am using php mail function with PDF attachment. Here everything is working fine but in the email body this following warning comes
"This email has an attachment that allows unverified scripts to run on your computer when opened. Be careful"
can you suggest ? what is the problem
Are you using the correct headers for sending attachments?
You can try using this pattern (tested and working):
$subject = 'SUBJECT';
$message .= 'Hello world'."\n";
$content = file_get_contents($file);
$content = chunk_split(base64_encode($content));
$separator = md5(time());
$eol = "\r\n";
$headers = "From: ME <me#mymail.org>".$eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol;
$headers .= "Content-Transfer-Encoding: 7bit".$eol;
$headers .= "This is a MIME encoded message.".$eol;
$body = "--".$separator.$eol;
$body .= "Content-Type: text/plain; charset=\"utf-8\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol;
$body .= $message.$eol;
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol;
$body .= $content.$eol;
$body .= "--".$separator."--";
$filename is your attachment, $file is the full path (including $filename).

PHP: Sending mail with attach

With this code:
$Message = 'TEXT';
$boundary = "---";
$headers = "From: $from\nX-Mailer: Carline Server www.carline.ru";
$headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"";
$body = "--$boundary\n";
$body .= "Content-type: text/html; charset='utf-8'\n";
$body .= "Content-Transfer-Encoding: quoted-printable\n\n";
$body .= "Content-Disposition: attachment; filename==?utf-8?B?".base64_encode($_FILES['photo1']['name'])."?=\n\n";
$body .= $Message."\n";
$body .= "--$boundary\n";
$text = file_get_contents($_FILES['photo1']['tmp_name']);
$body .= "Content-Type: application/octet-stream; name==?utf-8?B?".base64_encode($_FILES['photo1']['name'])."?=\n";
$body .= "Content-Transfer-Encoding: base64\n";
$body .= "Content-Disposition: attachment; filename==?utf-8?B?".base64_encode($_FILES['photo1']['name'])."?=\n\n";
$body .= chunk_split(base64_encode($text))."\n";
$body .= "--".$boundary ."--\n";
mail($AdminEmail, "RE:", $body, $headers);
It sends a file-content in the message body
Content-Type: application/octet-stream; name==?utf-8?B?MDAwMDU3ODcuanBn?=
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename==?utf-8?B?MDAwMDU3ODcuanBn?=
/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcG
BwcICQsJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz/2wBDAQICAgMDAwYDAwYMCAcIDAwMDAwM
DAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAz/wAARCAGQAZADASIA
...
How to change this code for sending file as attachment?
From php.net # http://php.net/manual/en/function.mail.php
Send Multi attachment email
<?php
function multi_attach_mail($to, $files, $sendermail){
// email fields: to, from, subject, and so on
$from = "Files attach <".$sendermail.">";
$subject = date("d.M H:i")." F=".count($files);
$message = date("Y.m.d H:i:s")."\n".count($files)." attachments";
$headers = "From: $from";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$message = "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
// preparing attachments
for($i=0;$i<count($files);$i++){
if(is_file($files[$i])){
$message .= "--{$mime_boundary}\n";
$fp = #fopen($files[$i],"rb");
$data = #fread($fp,filesize($files[$i]));
#fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"".basename($files[$i])."\"\n" .
"Content-Description: ".basename($files[$i])."\n" .
"Content-Disposition: attachment;\n" . " filename=\"".basename($files[$i])."\"; size=".filesize($files[$i]).";\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
}
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $sendermail;
$ok = #mail($to, $subject, $message, $headers, $returnpath);
if($ok){ return $i; } else { return 0; }
}
?>
An example of header construction
$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";
// content type: mixed or related ( better mixed for attachments
$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/octet-stream; name=\"".$filename."\"\r\n";
// use different content types here
$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."--";
You should be using pre-written libraries (e.g. http://swiftmailer.org/) to send emails, esp. the ones with attachments, whether inline or not.
Constructing valid and cross-mail client supported message is considerably advance task, esp. if you have never done this before. So why spend time re-inventing a weel when someone did the job for you already?
Put this question from the other end and if you want to build your own library, analyze what's already been done in SwiftMailer and use those snippets where needed.

Is there a maximum reasonable file size to attach to a MIME email with PHP?

I run PHP on IIS6. I have some PHP that successfully sends a 1KB image as an attachment on an email. When I try and attach a 500KB PDF however (having changed the Content-Type), it hangs and after a few minutes I get "FastCGI process exceeded configured request timeout" (Error Number 258 (0x80070102)).
Any thoughts on why it's taking so long to attach the PDF? The solution is not to increase the timeout limit, I can't have users sitting there for 3+ minutes while the file gets sent.
I've included my code below:
$headers = "From: ".$from."\r\n";
$headers .= "Reply-To: ".$from."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$headers .="This is a multipart message in MIME format. \r\n\r\n";
$headers .= "--".$uid."\r\n\r\n";
$headers .= "Content-Type: text/plain; charset-iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: quoted-printable\r\n\r\n";
$headers .= $text . "\r\n\r\n";
$headers .= "--".$uid."\r\n\r\n";
$headers .= "Content-Type: text/html; charset-iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$headers .= $html . "\r\n\r\n";
$headers .= "--".$uid."\r\n\r\n";
$headers .= "Content-Type: image/png; name=\"".$filename."\"\r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$attachment = chunk_split(base64_encode(file_get_contents($path.$filename)));
$headers .= $attachment . "\r\n\r\n";
$headers .= "--".$uid."\r\n\r\n";
//send the email
$mail_sent = #mail( $to, $subject, $text, $headers );
Thanks in advance for any advice.
Put the attachment in the message parameter of the mail() function instead of the additional headers parameter.
I ran into the same problem today and found that I couldn't submit large files as part of the headers parameter in the mail() function.
e.g.
$headers = "From: ".$from."\r\n";
$headers .= "Reply-To: ".$from."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$body .="This is a multipart message in MIME format. \r\n\r\n";
$body .= "--".$uid."\r\n\r\n";
$body .= "Content-Type: text/plain; charset-iso-8859-1\r\n";
$body .= "Content-Transfer-Encoding: quoted-printable\r\n\r\n";
$body .= $text . "\r\n\r\n";
$body .= "--".$uid."\r\n\r\n";
$body .= "Content-Type: text/html; charset-iso-8859-1\r\n";
$body .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$body .= $html . "\r\n\r\n";
$body .= "--".$uid."\r\n\r\n";
$body .= "Content-Type: image/png; name=\"".$filename."\"\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n";
$body .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$attachment = chunk_split(base64_encode(file_get_contents($path.$filename)));
$body .= $attachment . "\r\n\r\n";
$body .= "--".$uid."\r\n\r\n";
//send the email
$mail_sent = #mail( $to, $subject, $body, $headers );

Attach file to mail using php

I've created a form which contains an upload field file and some other text fields. I'm using php to send the form's data via email and attach the file.
This is the code I'm using but it's not working properly. The file is normally attached to the message but the rest of the data is not sent.
$body="bla bla bla";
$attachment = $_FILES['cv']['tmp_name'];
$attachment_name = $_FILES['cv']['name'];
if (is_uploaded_file($attachment)) {
$fp = fopen($attachment, "rb");
$data = fread($fp, filesize($attachment));
$data = chunk_split(base64_encode($data));
fclose($fp);
}
$headers = "From: $email<$email>\n";
$headers .= "Reply-To: <$email>\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"----=MIME_BOUNDRY_main_message\"\n";
$headers .= "X-Sender: $first_name $family_name<$email>\n";
$headers .= "X-Mailer: PHP4\n";
$headers .= "X-Priority: 3\n";
$headers .= "Return-Path: <$email>\n";
$headers .= "This is a multi-part message in MIME format.\n";
$headers .= "------=MIME_BOUNDRY_main_message \n";
$headers .= "Content-Type: multipart/alternative; boundary=\"----=MIME_BOUNDRY_message_parts\"\n";
$message = "------=MIME_BOUNDRY_message_parts\n";
$message .= "Content-Type: text/html; charset=\"utf-8\"\n";
$message .= "Content-Transfer-Encoding: quoted-printable\n";
$message .= "\n";
$message .= "$body\n";
$message .= "\n";
$message .= "------=MIME_BOUNDRY_message_parts--\n";
$message .= "\n";
$message .= "------=MIME_BOUNDRY_main_message\n";
$message .= "Content-Type: application/octet-stream;\n\tname=\"" . $attachment_name . "\"\n";
$message .= "Content-Transfer-Encoding: base64\n";
$message .= "Content-Disposition: attachment;\n\tfilename=\"" . $attachment_name . "\"\n\n";
$message .= $data; //The base64 encoded message
$message .= "\n";
$message .= "------=MIME_BOUNDRY_main_message--\n";
$subject = 'bla bla bla';
$to="test#test.com";
mail($to,$subject,$message,$headers);
Why isn't the $body data not sent? Can you help me fix it?
Well, I'd suggest using the PEAR Mail_Mime package... It abstracts all that away...
As for your exact issue, I'd guess it's because you have two different boundaries and two Content-Type headers in the header section. Try generating something like this:
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="------=MIME_BOUNDARY_MESSAGE_PARTS"
------=MIME_BOUNDARY_MESSAGE_PARTS
Content-Type: text/html charset="utf-8"
$body
------=MIME_BOUNDARY_MESSAGE_PARTS
Content-Type: application/octet-stream;name="filename"
Content-Transfer-Encoding: base64
...
$data
------=MIME_BOUNDARY_MESSAGE_PARTS

Categories