This question already has answers here:
Send attachments with PHP Mail()?
(16 answers)
Closed 3 years ago.
i have an email attachment script that does work and sends me a file with the correct name...however the file is 0 bytes.
here is the php:
$namer = $_FILES["cv_upload"]["name"];
$file ="/home2/deserul7/public_html/nkaccounting/"."temp_cv/"."".$namer."";
$contenttype = $_FILES["cv_upload"]['type'];
$handle = fopen($file, "rb");
$file_size = filesize($file);
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$name = basename($file);
$eol = PHP_EOL;
// Basic headers
$header = "From: NK Accounting <sal#desertsunstudio.com>".$eol;
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"";
// Put everything else in $message
$message = "--".$uid.$eol;
$message .= "Content-Type: text/html; charset=ISO-8859-1".$eol;
$message .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$message .= $msg2."<br><br><br>".$eol;
$message .= "--".$uid.$eol;
$message .= "Content-Type: ".$contenttype."; name=\"".$name."\"".$eol;
$message .= "Content-Transfer-Encoding: base64".$eol;
$message .= "Content-Disposition: attachment; filename=\"".$name."\"".$eol;
$message .= $content.$eol;
$message .= "--".$uid."--";
if (#mail($ouremail, $subject2, $message, $header)){
echo "sent";
}
when i send it to my gmail it works fine but when i try to get it thru my mail app in my desktop it comes in at 0 bytes...please help
There's supposed to be a blank line between the attachment header and the attachment contents. You only have one $eol there, so there's no blank line (you did it correctly for the part with $msg2). Change the Content-Disposition line to:
$message .= "Content-Disposition: attachment; filename=\"".$name."\"".$eol.$eol;
You can see things like this more easily if you use a here-string instead of concatenation.
$message = <<<EOF
--$uid
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
$msg2<br><br><br>
--$uid
Content-Type: $contenttype; name="$name"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="$name"
$content
--$uid
EOF;
Related
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I am attempting to send an attachment via email using PHP. I have been following some online tutorial pages and my mail() function is returning true. However, the email is not sending. I have been examining my headers and body and can't pinpoint what I am doing wrong.
$from_email = 'admin#felixxiao.com'; //sender email
$recipient_email = 'admin#felixxiao.com'; //recipient email
$subject = 'Test mail'; //subject of email
$message = 'This is body of the message'; //message body
//get file details we need
$file_tmp_name = $_FILES['data']['tmp_name'];
$file_name = $_FILES['data']['name'];
$file_size = $_FILES['data']['size'];
$file_type = $_FILES['data']['type'];
$file_error = $_FILES['data']['error'];
$user_email = filter_var("admin#felixxiao.com", FILTER_SANITIZE_EMAIL);
if($file_error>0)
{
echo 'upload error';
}
//read from the uploaded file & base64_encode content for the mail
$handle = fopen($file_tmp_name, "r");
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode($content));
$boundary = md5("sanwebe");
//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";
echo $headers;
//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: ".$file_type."; name=".$file_name."\r\n";
$body .="Content-Disposition: attachment; filename=".$file_name."\r\n";
$body .="Content-Transfer-Encoding: base64\r\n";
$body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
echo $body;
$body .= $encoded_content;
$sentMail = #mail($recipient_email, $subject, $body, $headers);
if($sentMail) //output success or failure messages
{
echo 'Thank you for your email';
}else{
echo 'Could not send mail! Please check your PHP mail configuration.';
}
When I print out my $headers and $body, this is what prints out:
MIME-Version: 1.0
From: admin#felixxiao.com
Reply-To: admin#felixxiao.com
Content-Type: multipart/mixed; boundary = 8de2a431c506316063ec3a4044192e46
--8de2a431c506316063ec3a4044192e46
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: base64
VGhpcyBpcyBib2R5IG9mIHRoZSBtZXNzYWdl
--8de2a431c506316063ec3a4044192e46
Content-Type: application/pdf; name=blob
Content-Disposition: attachment; filename=blob
Content-Transfer-Encoding: base64
X-Attachment-Id: 32091
Thank you for your email
The # before the #mail call suppresses the error (see this). (It sets the error to 0). Remove the # and try again.
I have this problem with PHP mail function:
I'm trying to sent an html email with a PDF file in attachment, the file is stored in a folder of mywebsite and created from me with mpdf, but when I send it the mail received has an attachment with size 0b.
this is the code:
<?
$attachment = "path_to_file_pdf.file.pdf";
if( file_exists($attachment)){
// File Exists
$size = filesize($attachment);
if( $size > 0 ){
//Alternative 1
$file = fopen($attachment,'rb');
$content = fread($file, $size);
fclose($file);
$content = chunk_split(base64_encode($content));
//Alternative 1
//$content = chunk_split(base64_encode(file_get_contents($attachment)));
$mailto = "example#maito.com";
$from_name = "MyDomainName";
$from_mail = "example#mydomain.com";
$replyto = "example#mydomain.com";
$uid = md5(uniqid(time()));
$subject = "e-mail subject here";
$message = "HTML MESSAGE HERE" ;
$filename = "file.pdf";
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"PHP-alt-".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--PHP-alt-".$uid."\r\n";
$header .= "Content-Type: text/html; charset=UTF-8\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .= "--PHP-alt-".$uid."\r\n";
$header .= "Content-Type: application/pdf; name=\"".$filename."\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; \r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--PHP-alt-".$uid."--";
if( #mail($mailto, $subject, "", $header) ){
echo "Mail SENT";
}else{
echo "ERROR - Mail error";
}
}else{
echo "ERROR- File size = 0";
}
}else{
echo "ERROR - File doesn't exist";
}
?>
The mail is sent correctly, so the file exist and its size is greater than 0b.
But when I receive the email in my emailbox it's all correct instead of the attachment that is present but empty.
I tryed both the 2 alternative of extracting the file content inserted in the code, but the result is the same.
Someone could help me?
I changed some headers configs. Try this:
<?
$attachment = "path_to_file_pdf.file.pdf";
if( file_exists($attachment)){
// File Exists
$size = filesize($attachment);
if( $size > 0 ){
//Alternative 1
$file = fopen($attachment,'rb');
$content = fread($file, $size);
fclose($file);
$content = chunk_split(base64_encode($content));
//Alternative 1
//$content = chunk_split(base64_encode(file_get_contents($attachment)));
$mailto = "example#maito.com";
$from_name = "MyDomainName";
$from_mail = "example#mydomain.com";
$replyto = "example#mydomain.com";
$uid = md5(uniqid(time()));
$subject = "e-mail subject here";
$message = "HTML MESSAGE HERE" ;
$filename = "file.pdf";
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"PHP-alt-".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--PHP-alt-".$uid."\r\n";
$header .= "Content-Type: text/html; charset=UTF-8\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .= "--PHP-alt-".$uid."\r\n";
$header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--PHP-alt-".$uid."--";
if( #mail($mailto, $subject, "", $header) ){
echo "Mail SENT";
}else{
echo "ERROR - Mail error";
}
}else{
echo "ERROR- File size = 0";
}
}else{
echo "ERROR - File doesn't exist";
}
?>
Seen that I didn't receive answers, I only found one way to do that without problems, this way is to use PHPMailar class.
Thanx to the discussion:
Send attachments with PHP Mail()?
Hello i'm trying to send email from form^) Here is my code
<?php
$to = "some email";
$from = "site";
$subject = "Resume";
$boundary = "---";
if (isset($_POST['ok'])){
$filename = $_POST['fileField'];
$resumeLink = $_POST['linkField'];
$message = "Attachment: " .$resumeLink;
/* Headers*/
$headers = "From: $from\nReply-To: $from\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"";
$body = "--$boundary\n";
/* Add message */
$body .= "Content-type: text/html; charset='utf-8'\n";
$body .= "Content-Transfer-Encoding: quoted-printablenn";
$body .= "Content-Disposition: attachment; filename==?utf-8?B?".base64_encode($filename)."?=\n\n";
$body .= $message."\n";
$body .= "--$boundary\n";
$file = fopen($filename, "r"); //open file
$text = fread($file, filesize($filename)); //read file
fclose($file); //close file
/* Add message type */
$body .= "Content-Type: application/octet-stream; name==?utf-8?B?".base64_encode($filename)."?=\n";
$body .= "Content-Transfer-Encoding: base64\n";
$body .= "Content-Disposition: attachment; filename==?utf-8?B?".base64_encode($filename)."?=\n\n";
$body .= chunk_split(base64_encode($text))."\n";
$body .= "--".$boundary ."--\n";
if(mail($to, $subject, $body, $headers)){header("Location: /");}
}
?>
Attachment will send, but it size will be 1 byte without any content inside. Whats wrong?
It's hard to tell exactly, but you've added header fields into mail body, you should add them in headers
I have successfully generated a PDF using mpdf, which I have verified by downloading the PDF. However, when I send the PDF as an e-mail attachment I receive a blank PDF with an "Out of Memory" error by Adobe Reader. Below is my code:
<?php
include("MPDF57/mpdf.php");
ob_start();
include "Receipt_Template_2.php";
$template = ob_get_contents();
ob_end_clean();
$mpdf=new mPDF('','A4','','',32,25,27,25,16,13,'L');
mpdf->WriteHTML($template);
$content = $mpdf->Output($template, 'S');
$content = chunk_split(base64_encode($content));
$mailto = 'sample#sample.com';
$from_name = 'KIREA';
$from_mail = 'NoReply#kirea.ca';
$uid = md5(uniqid(time()));
$subject = 'KIREA Donation Receipt';
$message = "Thank you for your donation!\n\nAttached is the receipt concerning the donation. If you have any questions, please e-mail us at receipts#kirea.ca";;
$filename = $pdfName;
$header = "From: ".$from_name." <".$from_mail.">\n";
$header .= "MIME-Version: 1.0\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\n";
$header .= "This is a multi-part message in MIME format.\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\n";
$header .= "Content-Transfer-Encoding: 7bit\n";
$header .= $message."\n\r\n";
$header .= "--".$uid."\n";
$header .= "Content-Type: application/pdf; name=\"".$filename."\"\n";
$header .= "Content-Transfer-Encoding: base64\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\n\n";
$header .= $content."\n\n";
$header .= "--".$uid."--";
$is_sent = #mail($mailto, $subject, "", $header);
$mpdf->Output();
exit;
?>
Are there any ideas as to why the PDF is ending up blank after being sent as an e-mail attachment? Thank you.
mpdf->WriteHTML($template);
$content = $mpdf->Output($template, 'S');
You are wrong at here you are not taking the object on which your data is written
please replace below code with above to get correct result.
$pdfdata=mpdf->WriteHTML($template);
$content = $mpdf->Output('' , 'S');
use $content in your email
If you can use swiftmailer, you can attach a MPDF generated PDF to the email, quite easily as follows:
<?php
require_once $swift_mailer_path.'swift_required.php';
$transporter = Swift_SmtpTransport::newInstance($smtp_host, $smtp_port, $smtp_protocol)
->setUsername($smtp_username')
->setPassword($smtp_password');
$mailer = Swift_Mailer::newInstance($transporter);
$message = Swift_Message::newInstance('Email Subject')
->setFrom(array($from_email => $from_name))
->setTo($to_email)
->setBody($email_body);
$attachment = Swift_Attachment::newInstance($mpdf->Output($pdf_path, "S"), $pdf_file_name, 'application/pdf');
$message->attach($attachment);
$message->setContentType("text/html");
$result = $mailer->send($message);
?>
Here is the Reference.
I've been struggling with this script for 5 days now and I just can't get it to work. I want to send a mail using php mail function. It needs to have an inline image, and 2 attachments. What I've got does that, and it displays correcty in Thunderbird, but in Gmail client it shows the image as attachment, and not in the body of the message. Here's the code that I have:
<?php
$filename = "sharewood-lija-cjenik.xlsx";
$filename2 = "sharewood-lija-ponuda.pdf";
$inline = chunk_split(base64_encode(file_get_contents('../img/sharewoodlija.png')));
$sep = sha1(date('r', time()));
$uid = md5(uniqid(time()));
$subject = "Sharewood Lija";
$mailto = "mymail#gmail.com";
$message = '<img src="cid:image_identifier" alt="SWLBanner" /><br><br>';
$message .="<div>html message</div>";
$header = "From: asdf <asdf#asdf.hr>\r\n";
$header .= "Reply-To: asdf#asdf.hr\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/related; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: text/html; charset=uft-8\r\n";
//$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
//image
$header .= "--".$uid."\r\n";
$header .= "Content-Type: image/png;\r\n";
$header .= "name=\"sharewoodlija.png\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-ID: <image_identifier>\r\n";
$header .= "Content-Disposition: inline;\r\n";
$header .= "filename=\"sharewoodlija.png\"\r\n\r\n";
$header .= $inline."\r\n";
//cjenik
$file = "../cjenik/sharewood-lija-cjenik.xlsx";
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$name = basename($file);
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; name=\"".$filename."\"\r\n"; // use different content types here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
//ponuda
$file2 = "../cjenik/sharewood-lija-ponuda.pdf";
$file_size2 = filesize($file2);
$handle2 = fopen($file2, "r");
$content2 = fread($handle2, $file_size2);
fclose($handle2);
$content2 = chunk_split(base64_encode($content2));
$name2 = basename($file2);
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/pdf; name=\"".$filename2."\"\r\n"; // use different content types here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename2."\"\r\n\r\n";
$header .= $content2."\r\n\r\n";
$header .= "--".$uid."--";
if (mail($mailto, $subject, "", $header)) {
echo "mail send ... OK"; // or use booleans here
} else {
echo "mail send ... ERROR!";
}
?>
Any help is appreciated
The problem is not your program. Google mail will not allow images. I have seen 3 surveys of mail clients and most will not handle images like thunderbird. Some won't even indicate that there is an image or show the alt title or anything
.