I search and try different methods to produce a php mail with an attachment, which is saveable with the Client (Outlook). It doesn't work porper and I don't know why?
If the script send the mail with this code:
$result = mail($mail_to, $subject, $headers);
I got an email with an inline base64 coded attachment and if use it in this way:
$result = mail($mail_to, $subject, "", $headers);
or
$result = mail($mail_to, $subject, $text, $headers);
I got the PHP Error: "false" - the mail could not send.
So it isn't and Mailserverproblem, I think and from headersview I think, it should work and I get no mail error on the Linux site.
A part from the mail coming with the attachment inline:
MIME-Version: 1.0
From: user#nowhere.xyz
Content-Type: multipart/mixed; boundary="F34A3E8D822EE5A70EEDE9C3C143243C"
--F34A3E8D822EE5A70EEDE9C3C143243C
Content-Type: multipart/alternative; boundary="A0403290562B2A2F19FB62E48C51BE33"
--A0403290562B2A2F19FB62E48C51BE33
Content-Type: text/plain
Content-Transfer-Encoding: 8bit
Hello Mr. Xyz,
Some stupid text as an example.
--A0403290562B2A2F19FB62E48C51BE33--
--F34A3E8D822EE5A70EEDE9C3C143243C
Content-Type: application/CSV; charset="ISO-8859-1"; name="trouble.csv"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="trouble.csv"
...
YWNodW5nIG5pY2h0IGVyZm9yZGVybGljaCAvIG3DtmdsaWNoIjsiIjsiIjsK
--F34A3E8D822EE5A70EEDE9C3C143243C--
The matching PHP code:
$text = "Hello Mr. Xyz," . $nl . "Some stupid text as an example." . $nl;
$content = chunk_split(base64_encode($file));
$headers = "MIME-Version: 1.0" . $nl;
$headers .= "From: " . $mail_from . $nl;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $boundary1 . "\"" . $nl . $nl;
$headers .= "--" . $boundary1 . $nl;
$headers .= "Content-Type: multipart/alternative; boundary=\"" . $boundary2 . "\"" . $nl . $nl;
$headers .= "--" . $boundary2 . $nl;
$headers .= "Content-Type: text/plain" . $nl;
$headers .= "Content-Transfer-Encoding: 8bit" . $nl . $nl;
$headers .= $text . $nl;
$headers .= "--" . $boundary2 . "--" . $nl . $nl;
$headers .= "--" . $boundary1 . $nl . $nl;
$headers .= "Content-Type: application/excel; charset=\"ISO-8859-1\"; name=\"xyz.csv\"" . $nl;
$headers .= "Content-Transfer-Encoding: base64" . $nl;
$headers .= "Content-Disposition: attachment; filename=\"xyz.csv\"" . $nl . $nl;
$headers .= $content . $nl;
$headers .= "--" . $boundary1 . "--" . $nl;
So, where is the bug? How can I debug such a Situation?
Thx in advance...
This one works....
$text = "Hello Mr. Xyz," . $crnl . "Some stupid text as an example." . $crnl;
$content = chunk_split(base64_encode($file));
$headers = "MIME-Version: 1.0" . $crnl;
$headers .= "From: " . $mail_from . $crnl;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $boundary1 . "\"" . $nl . $crnl;
$headers .= "Content-Type: text/plain" . $nl;
$headers .= "--" . $boundary1 . $crnl;
$headers .= 'Content-Length: '.strlen($text) . $crnl;
$headers .= "Content-Transfer-Encoding: 8-bit" . $nl . $crnl;
$headers .= $text;
$headers .= "--" . $boundary1 . $crnl;
$headers .= "Content-Type: application/excel; charset=\"ISO8859-1\"; name=\"xyz.csv\"" . $nl . $crnl;
$headers .= "--" . $boundary1 . $crnl;
$headers .= "Content-Disposition: attachment; filename=\"xyz.csv\"" . $crnl;
$headers .= 'Content-Length: ' . strlen($content) . $crnl;
$headers .= "Content-Transfer-Encoding: base64" . $nl . $crnl;
$headers .= $content;
$headers .= "--" . $boundary1 . "--"
...with a small bug. I have an extra attment, but that's for now ok. We other thing is a longer play with "\n", "\r\n" and a mixture of both on the right places (lines).
Related
I have this code :
$to = $input['to'];
$from = $input['from'];
$subject = $input['subject'];
$message = $input['message'];
$file = $input['file'];
$filename = 'Invoice.pdf';
$content = file_get_contents($file);
$content = chunk_split(base64_encode($content));
$separator = md5(time());
// carriage return type (RFC)
$eol = "\r\n";
// main header (multipart mandatory)
$headers = "From: testsite.com < admin#testsite.com >\n";
$headers .= "Cc: ".$from."\n";
$headers .= "X-Sender: testsite < admin#testsite.com >\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
$headers .= "X-Priority: 1\n"; // Urgent message!
$headers .= "Return-Path: testsite\n"; // Return path for errors
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
// message
$body = "--" . $separator . $eol;
$body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
$body .= "Content-Transfer-Encoding: 8bit" . $eol;
$body .= $message . $eol;
// attachment
$body .= "--" . $separator . $eol;
$body .= "Content-Type: application/pdf; name=\"" . $filename . "\"" . $eol;
$body .= "Content-Transfer-Encoding: base64" . $eol;
$body .= "Content-Disposition: attachment" . $eol;
$body .= $content . $eol;
$body .= "--" . $separator . "--";
mail($to, $subject, $body, $headers);
This code already working and already have attached when you read the email. But what's missing is this:
This is the example I wanted to see in inbox list. Thanks you!.
I want to send multiple attachments with an email. The below is my code
$file = 'C:/Users/pdf/Testing.pdf';
$mailto = 'mail#mail.com';
$subject = 'Subject';
$message = 'My message';
$content = file_get_contents($file);
$content = chunk_split(base64_encode($content));
// I can give $content to only one file and I have to give multiple pdf files here
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (RFC)
$eol = PHP_EOL;
// main header (multipart mandatory)
$headers = "From: name <test#test.com>" . $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;
// message
$body = "--" . $separator . $eol;
$body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
$body .= "Content-Transfer-Encoding: 8bit" . $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;
$body .= $content . $eol;
$body .= "--" . $separator . "--";
//SEND Mail
if (mail($mailto, $subject, $body, $headers)) {
echo "mail send ... OK"; // or use booleans here
} else {
echo "mail send ... ERROR!";
print_r( error_get_last() );
}
This code send me only one files as an attachment and I have to send the other file as well.
$file2 = 'C:/Users/pdf/sample1.pdf'; // The path for 2nd pdf file
Please try below code. You can create attachment array like
$attachement = array();
$attachement['data'][0] = 'pdfdata' // Pass PDF content with base64_encode
$attachement['data'][1] = 'tpPdfdata';
$attachement['name'][0] = 'sample1.pdf';
$attachement['name'][1] = 'sample2.pdf';
enter code here
<?php
public function send($to, $from, $subject, $message,
$cc, $attachement='') {
$mail_header = "From: $from\n";
if (isset($cc)) {
$mail_header .= "Cc:$cc\n";
}
$mail_header.= "Reply-To: noreply#demo.com\n";
$mail_header .= "MIME-Version: 1.0";
// boundary
$semi_rand = md5(time());
$boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$mail_header .= "\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$boundary}\"";
// multipart boundary
$message = "--{$boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
// preparing attachments
if (count($attachement) > 0) {
for ($i = 0; $i < count($attachement); $i++) {
$message .= "--{$boundary}\n";
$data = $attachement['data'][$i];
$message .= "Content-Type: application/octet-stream; name=\"" . $attachement['name'][$i] . "\"\n" .
//"Content-Description: ".basename($files[$i])."\n" .
"Content-Disposition: attachment;\n" . " filename=\"" . $attachement['name'][$i] . "\"; size=" . filesize($attachement['name'][$i]) . ";\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
}
$message .= "--{$boundary}--";
return mail($to, $subject, $message, $mail_header);
}
?>
If you using normal mail function you can achieve using above code.You can pass argument like to, from, subject,attachment etc... Please try. Thank you
I need to send a pdf in email and its receiving fine in gmail, yahoo etc but its giving problem in Microsoft outlook, mail body going as attachment in outlook.Please check below code and tell me what i am missing:--
function sendMailWithAttachment($fromName = 'from name', $fromEmail, $recipient, $mail_body, $subject, $filenamewithpath = '', $cc='',$bcc=''){
$fromEmail = 'info#domain.com';
$filename = explode('/', $filenamewithpath);
$filename = $filename[count($filename) - 1];
$content = file_get_contents($filenamewithpath);
$content = chunk_split(base64_encode($content));
// a random hash will be necessary to send mixed content
$separator = md5(time());
$separator = "==Multipart_Boundary_x{$separator}x";
// carriage return type (RFC)
$eol = "\n";
//$eol = "\r\n";
// main header (multipart mandatory)
$headers = "From: ". $fromName . " <" . $fromEmail . ">\r\n";
if(!empty($cc)){
$headers .= "Cc: <" . $cc . ">\r\n";
}
if(!empty($bcc)){
$headers .= "Bcc: <" . $bcc . ">\r\n";
}
$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;
// message
$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 .= $mail_body.$eol;
/* $body = "--" . $separator . $eol;
$body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
$body .= "Content-Transfer-Encoding: base64" . $eol;
$body .= $mail_body . $eol; */
if($filename != '' && $content != ""){
// attachment
$body .= "--" . $separator . $eol;
$body .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . $eol;
$body .= "Content-Transfer-Encoding: base64" . $eol;
$body .= "Content-Disposition: attachment; filename=\"".$filename."\"" . $eol;
$body .= $content . $eol;
$body .= "--" . $separator . "--";
}
//SEND Mail
if(mail($recipient, $subject, $body, $headers)){
return true;
}else{
return false;
}
}
I am using the code below to attach and embed an image into an email. This works in Gmail which I originally thought would be the biggest hurdle. For some reason though, when I access the email via Apple Mail and Thunderbird (same account), the image does not load but shows the following respectively:
What am I doing wrong? I cannot host and link these images normally as they are data driven graphs that are generated on the fly. Multipart attachment style embedding seems like my only option.
$to = "...";
$body = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html><body><img src="cid:image"/></body></html>';
$eol = PHP_EOL;
$separator = md5(time());
// headers
$headers = "From: ..." . $eol;
$headers .= "Reply-To: ..." . $eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
// message
$message = "--" . $separator . $eol;
$message .= "Content-Type: text/html; charset=\"iso-8859-1\"" . $eol;
$message .= "Content-Transfer-Encoding: 8bit" . $eol . $eol;
$message .= $body . $eol;
// attachment
$filename = "image.png";
$content = chunk_split(base64_encode(file_get_contents($filename)));
$message .= "--" . $separator . $eol;
$message .= "Content-Type: application/octet-stream; name=\"image.png\"" . $eol;
$message .= "Content-ID: <image>" . $eol;
$message .= "Content-Transfer-Encoding: base64" . $eol;
$message .= "Content-Disposition: attachment; filename=\"image.png\"" . $eol;
$message .= $content . $eol;
$message .= "--" . $separator . "--";
mail($to, "test", $message, $headers);
I want to generate and send pdf file in mail. When I receive mail, the content inside pdf file is not correct. Here is my code:
$txt = 'hello';
$dompdf = $this->get('slik_dompdf');
$dompdf->getpdf($txt);
$dompdf->stream('karan.pdf');
$pdfoutput = $dompdf->output();
$a = chunk_split(base64_encode($pdfoutput));
// echo"<pre>";print_r($pdf) ;die;
$filename = $pdfoutput;
$email = 'abhinandank#ocodewire.com';
$date = date("Y/m/d.");
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <support#rdrp.com>' . "\r\n";
$headers .= "Content-Disposition: attachment; filename=\"" . $filename . "\"\r\n\r\n";
$to = $email;
$subject = "Registrar Admin Password Reset";
$txt= 'hello your information is in attachment';
mail($to,$subject,$txt,$headers);
Please Help.
Try this...!!
$content="<html>html content here</html>" ;
$html2pdf = Yii::app()->ePdf->HTML2PDF();
$html2pdf->WriteHTML($content);
$to = "dheerajchouhan85#gmail.com";
$from = "no-reply#email.com";
$subject = "Thank you for your Contribution";
$message = "<p>Your Message</p>";
$separator = md5(time());
$eol = PHP_EOL;
$filename = "example.pdf";
$pdfdoc = $html2pdf->Output('', 'S');
$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;
$body .= "Content-Transfer-Encoding: 7bit" . $eol;
$body .= "This is a MIME encoded message." . $eol;
$body .= "--" . $separator . $eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"" . $eol;
$body .= "Content-Transfer-Encoding: 8bit" . $eol . $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 . $eol;
$body .= $attachment . $eol;
$body .= "--" . $separator . "--";
mail($to, $subject, $body, $headers);
I suggest you to use PHPMailer for sending email because it's very simple to use and mpdf library for creating PDFs (html2pdf conversion + utf-8 support). I have created application that sends dinamically created PDFs via email and it works perfectly.
PHPMailer:
https://github.com/PHPMailer/PHPMailer
mpdf:
http://www.mpdf1.com/mpdf/index.php