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;
}
}
Related
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))
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() );
}
?>
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 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