I try to add send email attachment using php,file attachment working fine but attachment file open blank document.How to solve this issue.Below mentioned my code.
$from_email = 'sender_mail#example.com'; //sender email
$recipient_email = 'manosk24#gmail.com'; //recipient email
$subject = 'Test mail'; //subject of email
$message = 'This is body of the message'; //message body
$filename = "file1.pdf";
$path = $_SERVER['DOCUMENT_ROOT'] . "/mail-function/upload/";
$file = $path.$filename;
$file_size = filesize($file);
$handle = fopen($file, "rb");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$boundary = md5(uniqid(time()));
//header
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From:" . $from_email . "\r\n";
$headers .= "Reply-To: " . $user_email . "" . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";
//plain text
$body = "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($message));
//attachment
$body .= "--$boundary\r\n";
$body .="Content-Type: application/pdf; name=" . $filename . "\r\n";
$body .="Content-Disposition: attachment; filename=" . $filename . "\r\n";
$body .="Content-Transfer-Encoding: base64\r\n";
$body .="X-Attachment-Id: " . rand(1000, 99999) . "\r\n\r\n";
$body .= $encoded_content;
$sentMail = #mail($recipient_email, $subject, $body, $headers);
if ($sentMail) { //output success or failure messages
die('Thank you for your email');
} else {
die('Could not send mail! Please check your PHP mail configuration.');
}
Sorry for my spelling mistake..
try this code,
$filename = "file1.pdf";
$file = $path . "/" . $filename;
$message ="my message";
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
// 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 (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
$headers .= "--" . $separator . $eol;
$headers .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
$headers .= "Content-Transfer-Encoding: 8bit" . $eol;
$headers .= $message . $eol;
// attachment
$headers .= "--" . $separator . $eol;
$headers .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: base64" . $eol;
$headers .= "Content-Disposition: attachment" . $eol;
$headers .= $content . $eol;
$headers .= "--" . $separator . "--";
//SEND Mail
if (mail($mailto, $subject, "", $headers)) {
echo "mail send ... OK"; // or use booleans here
} else {
echo "mail send ... ERROR!";
}
i hope it will be helpful.
Related
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 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 trying to send an attachment but receive blank file.
What am I doing wrong?
So $_POST["screenshot"] it's an image in da:image/png;base64,.... format and here seems everything is ok, it arrives without any problem, what happens than? why ireceive a blank file? how to send it properly WITHOUT saving before(commented area)?
<?php
header("Content-type: application/json; charset=utf-8");
if(isset($_POST["screenshot"]) && strlen($_POST["screenshot"]) > 0){
$data = str_replace("data:image/png;base64,", "", $_POST["screenshot"]);
// $png = fopen("screenshot.png", "w");
// fwrite($png, base64_decode($data));
// fclose($png);
*/
/* ___________ E-MAIL _____________ */
$to = 'mymail#mail.com';
$subject = 'Test email';
$attachment = chunk_split($data);
// 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 (multipart mandatory)
$headers = "From: domain#mail.com\r\nReply-To: mymail#mail.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;
// attachment
$headers .= "--" . $separator . $eol;
$headers .= "Content-Type: application/octet-stream; name=\"" . "cartolina.png" . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: base64" . $eol;
$headers .= "Content-Disposition: attachment" . $eol;
$headers .= $content . $eol;
$headers .= "--" . $separator . "--";
//SEND Mail
mail($to, $subject, "", $headers);
echo json_encode(array("ok"=>"evertyhing is ok. "));
} else {
echo json_encode(array("error"=>"not ok"));
}
?>
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