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"));
}
?>
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 m trying to send pdf attachment in gmail and webmail. In gmail it works properly but when it goes to webmail pdf file showing corrupt and 0 byte.it goes in gmail,yahoomail,and other mail is ok but in webmail it's not working file is going to webmail but when i m trying to open that pdf it showing file corrupt. Please help me out anyone.code bellow:-
$info_send_mail = $obj->select(TABLEPRIFIX.'send_mail','link',['sale_id'=>$info[0]['sale_id']]);// Fetch data
$filename = $info_send_mail[0]['link']; // fetch file name
$path = 'http://webfreakers.com/crm/admin/pdf/sendmail';
$file = $path . "/" . $filename;
$mailto = $info[0]['email'];
$subject = 'Invoice';
$message = 'Check your invoice';
// retrive file from the folder
$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: Universal Survey & Analysis Services <sms#webfreakers.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
$_SESSION['msg'] = '<p class="alert alert-success alert-dismissable fade in">×Mail sent successfully</p>';
header('location:../sales_list.php');
} else {
echo "mail send ... ERROR!";
print_r( error_get_last() );
$_SESSION['msg'] = '<p class="alert alert-success alert-dismissable fade in">×Mail not sent</p>';
header('location:../sales_list.php');
}
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 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