I'm building a service to send pdf attachments to some email destinations. Recently I have found that some people can't receive the emails properly:
- headers go into email body
- attachments get corrupted
By having a bunch of tests, I'm having a clue that the problem might be the PHP_EOL because when I change it (from \n to \r\n or vice-versa) some people receive it well and others don't.
Is it a coding problem or a server problem?
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// attachment name
$filename = "Convite.pdf";
// encode data (puts attachment in proper format)
$pdfdoc = $content;
$attachment = chunk_split(base64_encode($pdfdoc));
// main header
$headers = "From: Myserver<".$from_mail.">\nBCC: suporte#myserver.com".$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed;\tboundary=\"".$separator."\"";
// no more headers after this, we start the body! //
$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=\"utf-8\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$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.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";
Related
$path=$_SERVER['DOCUMENT_ROOT']."/unwant/test3.pdf";
$filename="test3.pdf";
$pdf->Output($filename,'F');
i got my output file in this code $pdf->Output($filename,'F'); i want to attach my output file in mail.
require('lib/fpdf/fpdf.php');
$pdf = new FPDF('P', 'pt', array(500,233));
$pdf->AddFont('Georgiai','','georgiai.php');
$pdf->AddPage();
$pdf->Image('lib/fpdf/image.jpg',0,0,500);
$pdf->SetFont('georgiai','',16);
$pdf->Cell(40,10,'Hello World!');
// email stuff (change data below)
$to = "myemail#example.com";
$from = "me#example.com";
$subject = "send email with pdf attachment";
$message = "<p>Please see the attachment.</p>";
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// attachment name
$filename = "test.pdf";
// encode data (puts attachment in proper format)
$pdfdoc = $pdf->Output("", "S");
$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! //
$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
mail($to, $subject, $body, $headers);
To use PHPMailer:
Download the PHPMailer script from here:
http://github.com/PHPMailer/PHPMailer
Extract the archive and copy the script's folder to a convenient
place in your project.
Include the main script file --
require_once('path/to/file/class.phpmailer.php');
Now, sending emails with attachments goes from being insanely difficult to incredibly easy:
$attachment= $pdf->Output('attachment.pdf', 'S');
$mailer->AddStringAttachment($attachment, 'attachment.pdf');
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).
I am using this function for attaching pdf in my mail. It's working fine but when the user tries to open the pdf file he gets a message telling him that file can't be opened, because of a problem with file formate.
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = "\r\n";
// attachment name
$filename = $invoice.'.pdf';
// encode data (puts attachment in proper format)
$attachment = chunk_split(base64_encode($filename));
//$attachment = $filename;
// main header
$from = "test#productionserver.in";
$headers = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol;
// no more headers after this, we start the body! //
$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$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."--";
mail($to,$subject,$body,$headers);
Does anyone have an idea what I made wrong ?
$attachment = chunk_split(base64_encode($filename));
You are encoding the filename as your attachment. You need to encode the actual file and not just its name.
$attachment = chunk_split(base64_encode(file_get_contents($filename)));
I'm having a big headache with this issue and I wonder if any1 could help me with this. In my tests and BCC I always see the PDF attachment correctly, but maybe 10% of the people see the PDF file as being corrupted (some people I know that they are using Outlook and I'm using Mail from Mac).
function mail_attachment($content, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// attachment name
$filename = "Invitation.pdf";
// encode data (puts attachment in proper format)
$pdfdoc = $content;
$attachment = chunk_split(base64_encode($pdfdoc));
// main header
$headers = "From: Myself <".$from_mail.">\nBCC: me#hotmail.com".$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed;{$eol}\tboundary=\"".$separator."\"";
// no more headers after this, we start the body! //
$body = "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"utf-8\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message;
$body .= $eol.$eol;
// message
$body .= "Content-Type: text/plain; charset=\"utf-8\"".$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.$eol;
// send message
$em = mail($mailto, $subject, $body, $headers);
return $em;}
What could possibly be happening? I always see it working but few people can't open the file..
It's been a while, but finally got this problem solved. The issue is on PHP_EOL which in my case is returning \n, while some systems the email should have \r\n as line break.
To fix this issue just place the new $eol:
$eol = "\r\n";
The way you have set the headers seems right to me. However, couple things I noticed/do differently:
$headers .= "Content-Type: multipart/mixed;{$eol}\tboundary=\"".$separator."\"".$eol;
Take this */ away from the end
$body .= $message.$eol;*/
And for the content disposition:
"Content-Disposition: attachment; filename=\"" . $filename . "\"".$eol;
Also, body and the attachment headers should be combined to the headers, no need to send body separately in mail():
return mail($mailto, $subject, "", $headers);
I've checked php manual, but still can't get my script to work
If I delete the BCC line, the mail goes to $to1 but with BCC line, it goes nowhere
here is the code:
// email stuff
$to1 = "address1#mail.com";
$to2 = "address2#mail.com";
$from = "noreply#mail.com";
$subject = "Subject";
$message = "message";
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// main header
$headers = "To: ".$to1.$eol;
$headers .= "From: ".$from.$eol;
$headers .= "Bcc: ".$to2.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol;
// no more headers after this, we start the body! //
$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$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;
$body .= $message.$eol;
// send message
mail($to1, $subject, $body, $headers);
If anyone has an idea, thanks a lot