Embed Image in Email Message in MPDF Using PHP - 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);

Related

Read any type of file and add its contents in email body in php/laravel

what I want to do is to read a document that could be a pdf, doc or docx file. And add its contents with same styling in an email body. Currently, I am sending the document as an attachment but I want it to be sent in my email body. I tried searching for a generic solution that I could use for all these different types of documents, but couldn't find something related to my scenario. Most of the solutions were related to sending file as an attachment. Your help is appreciated. Thanks.
Here's the code I'm using to send the document as an attachment.
$file = 'path/to/my/file.pdf';
$content = file_get_contents( $file);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$file_name = basename($file);
// header
$header = "From: Support <info#support.com>\r\n";
$header .= "Reply-To: info#support.com \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
// message & attachment
$nmessage = "--".$uid."\r\n";
$nmessage .= "Content-type:text/html; charset=\"iso-8859-1\"\r\n";
$nmessage .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$nmessage .= $message."\r\n\r\n";
$nmessage .= "--".$uid."\r\n";
$nmessage .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n";
$nmessage .= "Content-Transfer-Encoding: base64\r\n";
$nmessage .= "Content-Disposition: attachment; filename=\"".$file_name."\"\r\n\r\n";
$nmessage .= $content."\r\n\r\n";
$nmessage .= "--".$uid."--";
if (mail($mailto, $subject, $nmessage, $header))
return true; // Or do something here
else
return false;

Sending pdf through php mail function gives opening error

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

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.

\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.

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

Categories