I read all of the similar questions here and looked at many examples on other sites.
Despite my belief that I have this setup correctly, the content of my CSV attachment is displaying inline with the body message as well as showing up as an attachment. The attachment opens and renders correctly, but the body of the message looks like this:
Report Attached
payment_reconciliation.csv
Customer #,Invoice,Verifier,Post Date,Payment #,Status,Payment Date,Payment Amount
"456272","1395164","Verified - by Autumn on 2014-04-30 17:15:33","","135927","","2014-04-30","850.00"
"655469","1395163","Verified - by Autumn on 2014-04-30 17:13:17","","135926","","2014-04-30","2,275.00"
"351588","1395161","Verified - by Autumn on 2014-04-30 17:16:54","","135924","","2014-04-30","595.00"
"78444","1395160","Verified - by Autumn on 2014-04-30 ...
Here is the portion of the code that creates the headers:
$myfile = "payment_reconciliation.csv";
$uid = md5(uniqid(time()));
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; 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=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: text/csv; name=\"".$myfile."\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$myfile."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";
mail($to, $subject, $message, $header);
Anyone spot something I'm missing? Thank you.
Related
I am attaching an pdf file but the attachment is not opening at mail.
Its giving following error on mail server
Expected: application/pdf (.pdf); found: application/x-empty
I have following code snippet
<?php
$from='test1#domain.com';
$to = 'test#domain.com';
//define the subject of the email
$subject = 'Test email with attachment';
$message='Startendday reports' ;
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$file = file_get_contents('file.pdf');
$content = chunk_split(base64_encode(file_get_contents($file)));
$uid = md5(uniqid(time())); //unique identifier
$file_name='file.pdf';
//standard mail headers
$header = "From: ".$from."\r\n";
$header .= "Reply-To: ".$to. "\r\n";
$header .= "MIME-Version: 1.0\r\n";
//declare multiple kinds of email (plain text + attch)
$header .="Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .="This is a multi-part message in MIME format.\r\n";
//plain txt part
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message. "\r\n\r\n";
//attch part
$header .= "--".$uid."\r\n";
$header .= "Content-Type:application/octet-stream; name=\"".$file_name."\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$file_name."\"\r\n\r\n";
$header .= $content."\r\n\r\n"; //chucked up 64 encoded attch
//sending the mail - message is not here, but in the header in a multi part
if(mail($to, $subject, "", $header)) {
echo "success";
}else {
echo "fail";
}
?>
Please help me out
I am using php mail() function with attachment using Content-Type: multipart/mixed header but it always return noname.txt
Here is my codes:
$upload_name=$_FILES["upload"]["name"];
$upload_type=$_FILES["upload"]["type"];
$upload_size=$_FILES["upload"]["size"];
$upload_temp=$_FILES["upload"]["tmp_name"];
$subject = "Subject";
$to="example#example.com";
$message="message";
$fp = fopen($upload_temp, "rb");
$file = fread($fp, $upload_size);
$file = chunk_split(base64_encode($file));
$num = md5(time());
//Normal headers
$headers = "From: Info Mail<example#example.com>\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; ";
$headers .= "boundary=".$num."\r\n";
$headers .= "--$num\r\n";
// This two steps to help avoid spam
$headers .= "Message-ID: <".gettimeofday()." TheSystem#".$_SERVER['SERVER_NAME'].">\r\n";
$headers .= "X-Mailer: PHP v".phpversion()."\r\n";
// With message
$headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
$headers .= "".$message."\n";
$headers .= "--".$num."\n";
// Attachment headers
$headers .= "Content-Type:".$upload_type." ";
$headers .= "name=\"".$upload_name."\"r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; ";
$headers .= "filename=\"".$upload_name."\"\r\n\n";
$headers .= "".$file."\r\n";
$headers .= "--".$num."--";
// SEND MAIL
$flgchk=#mail($to, $subject, $message, $headers);
fclose($fp);
In Mail Inbox It Returns:
Thanks In Advance...
Remove the last boundary, finish the mail with the last attachment string only, this removes the noname.txt in gmail.
Your code is perfect
change the code here.
In Attachment headers
Replace
$headers .= "Content-Type:".$upload_type." ";
with
$headers .= "Content-Type: multipart/mixed; ";
$header = "From: Info Mail<example#example.com>\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$num."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$num."\r\n";
$header .= "Content-type:text/html; charset=UTF-8\r\n";
$header .= $message."\r\n\r\n";
$header .= "--".$num."\r\n";
$header .= "Content-Type: application/octet-stream; name=\"".$upload_name."\"\r\n"; // use different content types here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$fname."\"\r\n\r\n";
$header .= $file."\r\n\r\n";
$header .= "--".$num."--";
I used the above headers for mails which seemed to work.
$upload_name=$_FILES["your_file_input_name"]["name"];
$upload_type=$_FILES["your_file_input_name"]["type"];
$upload_size=$_FILES["your_file_input_name"]["size"];
$upload_temp=$_FILES["your_file_input_name"]["tmp_name"];
$subject = "Your Subject";
$to="example#example.com";
$message="Your Message";
$fp = fopen($upload_temp, "rb");
$file = fread($fp, $upload_size);
$file = chunk_split(base64_encode($file));
$num = md5(time());
//Normal headers
$headers = "From: Info Mail<example#example.com>\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; ";
$headers .= "boundary=".$num."\r\n";
$headers .= "--$num\r\n";
// This two steps to help avoid spam
$headers .= "Message-ID: <".gettimeofday()." TheSystem#".$_SERVER['SERVER_NAME'].">\r\n";
$headers .= "X-Mailer: PHP v".phpversion()."\r\n";
// With message
$headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
$headers .= "".$message."\n";
$headers .= "--".$num."\n";
// Attachment headers
$headers .= "Content-Type: application/".$upload_type." ";
$headers .= "name=\"".$upload_name."\"r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; ";
$headers .= "filename=\"".$upload_name."\"\r\n\n";
$headers .= "".$file."\r\n";
$headers .= "--".$num."--";
// SEND MAIL
$flgchk=#mail($to, $subject, $message, $headers);
fclose($fp);
if($flgchk)
{
echo 'Sent Successfully';
}
else
{
echo 'Unexpected error occurred!!! Please try again.';
}
Is there a way to embed an image in a message body in MPDF using php? Simply adding the HTML image tag as shown in the snippet below causes the actual tag to display in an e-mail.
$message = '<img src="Signature%20Card.jpg"/>';
Thank you for any ideas.
By the way, I am also sending an attached PDF. That part works fine though; only embedding the image does not work.
Below is the code for actually sending the e-mail:
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; 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/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/pdf; 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 .= "--".$uid."--";
$is_sent = #mail($mailto, $subject, "", $header);
You need to get the $message variable into the body of the email for this to work properly. Currently the code is going into the header rather than the email message body. Change these lines by removing the middle line:
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
//$header .= $message."\r\n\r\n";
$header .= "--".$uid."\r\n";
And in the final line of code, place the $message variable into the mail command:
$is_sent = #mail($mailto, $subject, $message, $header);
I am sending an email which attaches a pdf.
This is the code:
$mpdf->WriteHTML($html);
$content = $mpdf->Output('', 'S');
$content = chunk_split(base64_encode($content));
$mailto = $email;
$from_name = $yourname;
$from_mail = $fromwho;
$replyto = $replyto;
$uid = md5(uniqid(time()));
$subject = 'Horse Details';
$message = 'Please find attached details about the horse medical treatment.';
$filename = 'Horse';
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; 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/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/pdf; 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 .= "--".$uid."--";
I would like to add more details in the message and use \r\r for new paragraphs etc.
But for some reason the \r\r or \n will not work? Suspect it is because of some of the header info but not sure which one? Had a bit of a play but could not work it out.
Can you see the problem?
Thank you.
well i don't seem to get it wrong when i sent it , here's the source of the sent message
Subject: Horse Details
From: elibyy Reply-To: test#test.org
MIME-Version: 1.0 Content-Type: multipart/mixed;
boundary="660d0865650c12fa07c8430814690009"
This is a multi-part message in MIME format.
--660d0865650c12fa07c8430814690009 Content-type:text/plain;
charset=iso-8859-1 Content-Transfer-Encoding: 7bit
Please find attached details about the horse medical treatment.
--660d0865650c12fa07c8430814690009 Content-Type: application/pdf;
name="Horse" Content-Transfer-Encoding: base64 Content-Disposition:
attachment; filename="Horse"
--660d0865650c12fa07c8430814690009--
Please find attached details about the horse medical treatment.
I run PHP on IIS6. I have some PHP that successfully sends a 1KB image as an attachment on an email. When I try and attach a 500KB PDF however (having changed the Content-Type), it hangs and after a few minutes I get "FastCGI process exceeded configured request timeout" (Error Number 258 (0x80070102)).
Any thoughts on why it's taking so long to attach the PDF? The solution is not to increase the timeout limit, I can't have users sitting there for 3+ minutes while the file gets sent.
I've included my code below:
$headers = "From: ".$from."\r\n";
$headers .= "Reply-To: ".$from."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$headers .="This is a multipart message in MIME format. \r\n\r\n";
$headers .= "--".$uid."\r\n\r\n";
$headers .= "Content-Type: text/plain; charset-iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: quoted-printable\r\n\r\n";
$headers .= $text . "\r\n\r\n";
$headers .= "--".$uid."\r\n\r\n";
$headers .= "Content-Type: text/html; charset-iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$headers .= $html . "\r\n\r\n";
$headers .= "--".$uid."\r\n\r\n";
$headers .= "Content-Type: image/png; name=\"".$filename."\"\r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$attachment = chunk_split(base64_encode(file_get_contents($path.$filename)));
$headers .= $attachment . "\r\n\r\n";
$headers .= "--".$uid."\r\n\r\n";
//send the email
$mail_sent = #mail( $to, $subject, $text, $headers );
Thanks in advance for any advice.
Put the attachment in the message parameter of the mail() function instead of the additional headers parameter.
I ran into the same problem today and found that I couldn't submit large files as part of the headers parameter in the mail() function.
e.g.
$headers = "From: ".$from."\r\n";
$headers .= "Reply-To: ".$from."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$body .="This is a multipart message in MIME format. \r\n\r\n";
$body .= "--".$uid."\r\n\r\n";
$body .= "Content-Type: text/plain; charset-iso-8859-1\r\n";
$body .= "Content-Transfer-Encoding: quoted-printable\r\n\r\n";
$body .= $text . "\r\n\r\n";
$body .= "--".$uid."\r\n\r\n";
$body .= "Content-Type: text/html; charset-iso-8859-1\r\n";
$body .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$body .= $html . "\r\n\r\n";
$body .= "--".$uid."\r\n\r\n";
$body .= "Content-Type: image/png; name=\"".$filename."\"\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n";
$body .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$attachment = chunk_split(base64_encode(file_get_contents($path.$filename)));
$body .= $attachment . "\r\n\r\n";
$body .= "--".$uid."\r\n\r\n";
//send the email
$mail_sent = #mail( $to, $subject, $body, $headers );