Sending pdf through php mail function gives opening error - php

I am attaching an pdf file but the attachment is not opening at mail.
Its giving following error on mail server
Expected: application/pdf (.pdf); found: application/x-empty
I have following code snippet
<?php
$from='test1#domain.com';
$to = 'test#domain.com';
//define the subject of the email
$subject = 'Test email with attachment';
$message='Startendday reports' ;
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$file = file_get_contents('file.pdf');
$content = chunk_split(base64_encode(file_get_contents($file)));
$uid = md5(uniqid(time())); //unique identifier
$file_name='file.pdf';
//standard mail headers
$header = "From: ".$from."\r\n";
$header .= "Reply-To: ".$to. "\r\n";
$header .= "MIME-Version: 1.0\r\n";
//declare multiple kinds of email (plain text + attch)
$header .="Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .="This is a multi-part message in MIME format.\r\n";
//plain txt part
$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";
//attch part
$header .= "--".$uid."\r\n";
$header .= "Content-Type:application/octet-stream; name=\"".$file_name."\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$file_name."\"\r\n\r\n";
$header .= $content."\r\n\r\n"; //chucked up 64 encoded attch
//sending the mail - message is not here, but in the header in a multi part
if(mail($to, $subject, "", $header)) {
echo "success";
}else {
echo "fail";
}
?>
Please help me out

Related

Php mail send with attachment

I have written code for sending mail with attachment , i am getting mail but wil noname file with icon. I am providing path to file name and rest details to send mail.
I have written code for this migth be there is header problem i am not gettin g this problem.
Code:
$file = $path.$filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$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/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."--";
if (mail($mailto, $subject, "", $header)) {
echo "mail send ... OK"; // or use booleans here
} else {
echo "mail send ... ERROR!";
}

php mail with attachment and html

I have been trying to get mail to send html emails however it seems that whenever i change the header from
$message .= "Content-type:text/plain; charset=iso-8859-1\r\n"; to
$message .= "Content-type:text/html; charset=iso-8859-1\r\n";
the email does not arrive.When using text/plain, i receive the email however the html is not rendered, in other words i received the code rather than formatted html. Changing to text/html, the email is not received at all. I have tried using UTF-8 but can not seem to solve it after hours of searching. Any help would be appreciated.
function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $emessage) {
$file = $path.$filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$name = basename($file);
$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";
$message = "--".$uid."\r\n";
$message .= "Content-type:text/plain; charset=iso-8859-1\r\n";
//$message .= "Content-Type: text/html; charset=\"UTF-8\""."\r\n";
//$message .= "Content-type:text/html; charset=UTF-8\r\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$message .= $emessage."\r\n\r\n";
$message .= "--".$uid."\r\n";
$message .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n";
$message .= "Content-Transfer-Encoding: base64\r\n";
$message .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$message .= $content."\r\n\r\n";
$message .= "--".$uid."--";
return mail($mailto, $subject, $message, $header);
}

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);

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.

\r\n Not Working in text email coding?

I am sending an email which attaches a pdf.
This is the code:
$mpdf->WriteHTML($html);
$content = $mpdf->Output('', 'S');
$content = chunk_split(base64_encode($content));
$mailto = $email;
$from_name = $yourname;
$from_mail = $fromwho;
$replyto = $replyto;
$uid = md5(uniqid(time()));
$subject = 'Horse Details';
$message = 'Please find attached details about the horse medical treatment.';
$filename = 'Horse';
$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."--";
I would like to add more details in the message and use \r\r for new paragraphs etc.
But for some reason the \r\r or \n will not work? Suspect it is because of some of the header info but not sure which one? Had a bit of a play but could not work it out.
Can you see the problem?
Thank you.
well i don't seem to get it wrong when i sent it , here's the source of the sent message
Subject: Horse Details
From: elibyy Reply-To: test#test.org
MIME-Version: 1.0 Content-Type: multipart/mixed;
boundary="660d0865650c12fa07c8430814690009"
This is a multi-part message in MIME format.
--660d0865650c12fa07c8430814690009 Content-type:text/plain;
charset=iso-8859-1 Content-Transfer-Encoding: 7bit
Please find attached details about the horse medical treatment.
--660d0865650c12fa07c8430814690009 Content-Type: application/pdf;
name="Horse" Content-Transfer-Encoding: base64 Content-Disposition:
attachment; filename="Horse"
--660d0865650c12fa07c8430814690009--
Please find attached details about the horse medical treatment.

Categories