Send mail with multiple PDF files php - php

I'm trying to send more than one pdf and I didn't found any that works for me.
I tried this How to attach two or multiple files and send mail in PHP
My problem is that only one pdf is attached. Here is my code.
public function sendMultiple($from,$to,$subject,$files){
$eol = PHP_EOL;
$separator = md5(time());
$headers = "From: \"".$from."\" <".$this->replyTo.">\r\n";
$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 .= $this->getMessage().$eol;
foreach ($files as $file ) {
$body .= "--" . $separator . $eol;
$body .= "Content-Type: application/octet-stream; name=\"" . $file['name'] . "\"" . $eol;
$body .= "Content-Transfer-Encoding: base64" . $eol;
$body .= "Content-Disposition: attachment" . $eol . $eol;
$body .= $file['file'] . $eol;
$body .= "--" . $separator . "--";
}
$mail = mail($to,$subject,$body,$headers);
if($mail){
return true;
}
return null;
}
//$files is an array of this
$file1 = array("name"=>"name".$name1.".pdf","file"=>chunk_split(base64_encode($pdf)));
$file2 = array("name"=>"name".$name2.".pdf","file"=>chunk_split(base64_encode($pdf2)));
array_push($files, $file1);
array_push($files, $file2);

I solved the problem, here is the code:
public function sendMultiple($from,$to,$subject,$files){
$eol = PHP_EOL;
$separator = md5(time());
$headers = "From: \"".$from."\" <".$this->replyTo.">\r\n";
$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 .= $this->getMessage().$eol;
foreach ($files as $file ) {
$body .= "--" . $separator . $eol;
$body .= "Content-Type: application/octet-stream; name=\"" . $file['name'] . "\"" . $eol;
$body .= "Content-Transfer-Encoding: base64" . $eol;
$body .= "Content-Disposition: attachment" . $eol . $eol;
$body .= $file['file'] . $eol;
//This line is not needed, solved the issue
//$body .= "--" . $separator . "--";
}
$mail = mail($to,$subject,$body,$headers);
if($mail){
return true;
}
return null;
}
//$files is an array of this
$file1 = array("name"=>"name".$name1.".pdf","file"=>chunk_split(base64_encode($pdf)));
$file2 = array("name"=>"name".$name2.".pdf","file"=>chunk_split(base64_encode($pdf2)));
array_push($files, $file1);
array_push($files, $file2);

Related

Why I cannot send two attchments in PHP mail function?

I'm trying to send two attachments in an email using mail() function in PHP, but only attach one.
The problem is in the attachment time, I only receive in the email the first attachment.
Can you help?
This is the code:
$from = "postmaster#localhost";
// $to = "hector.oscos#gmail.com";
$content = file_get_contents($file1);
$content2 = file_get_contents($file2);
$content = chunk_split(base64_encode($content));
$content2 = chunk_split(base64_encode($content2));
// a random hash will be necessary to send mixed content
$semi_rand = md5(time());
$separator = "==Multipart_Boundary_x{$semi_rand}x";
// carriage return type
$eol = "\r\n";
// main header (multipart mandatory)
$headers = "From: Name<" . $from . ">" . $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/html;" . $eol;
$body .= "Content-Transfer-Encoding: 8bit" . $eol;
$body .= $message . $eol;
// attachment
$body .= "--" . $separator . $eol;
$body .= "Content-Type: application/octet-stream; name=\"factura.pdf\"" . $eol;
$body .= "Content-Transfer-Encoding: base64" . $eol;
$body .= "Content-Disposition: attachment; filename=\"factura.pdf\"" . $eol;
$body .= "X-Attachment-Id: " . rand(1000, 99999) . $eol;
$body .= $content . $eol;
$body .= "Content-Type: application/octet-stream; name=\"factura.xml\"" . $eol;
$body .= "Content-Transfer-Encoding: base64" . $eol;
$body .= "Content-Disposition: attachment; filename=\"factura.xml\"" . $eol;
$body .= "X-Attachment-Id: " . rand(1000, 99999) . $eol;
$body .= $content2 . $eol;
// attachment
$body .= "--" . $separator . "--";
mail($to, $subject, $body, $headers))

Send Email with PDF Attachment Using PHP - File Delivered Corrupt

I'm brand new to php and am using the following code to send an email with a PDF attachment.
The Code seems to almost be working. The Email is sent and the file is attached.
However when attempting to open the file it is saying that the file is corrupt.
Please can someone show me where I am going wrong? Thanks in advance.
<?php
$filename = 'My File.pdf';
$path = '/public_html';
$file = $path . "/" . $filename;
$mailto = 'themark#gmail.com';
$subject = 'Subject';
$message = 'My message';
$content = file_get_contents($file);
$content = chunk_split(base64_encode($content));
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (RFC)
$eol = "\r\n";
// main header (multipart mandatory)
$headers = "From: Jennifer <jenniferk#gmail.co.uk>" . $eol;
$headers .= "Reply-To: Jennifer <jenniferk#gmail.co.uk>" .$eol;
$headers .= "Return-Path: Jennifer <jenniferk#gmail.co.uk>" .$eol; // these two to set reply address
$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() );
}
?>

How do I show the attachment on inbox list in email by sending PHP mail

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

Send attachment with PHP Mail() that should open in outlook

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

Generate and send pdf file with mail

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

Categories