Attach file to mail using php - php

I've created a form which contains an upload field file and some other text fields. I'm using php to send the form's data via email and attach the file.
This is the code I'm using but it's not working properly. The file is normally attached to the message but the rest of the data is not sent.
$body="bla bla bla";
$attachment = $_FILES['cv']['tmp_name'];
$attachment_name = $_FILES['cv']['name'];
if (is_uploaded_file($attachment)) {
$fp = fopen($attachment, "rb");
$data = fread($fp, filesize($attachment));
$data = chunk_split(base64_encode($data));
fclose($fp);
}
$headers = "From: $email<$email>\n";
$headers .= "Reply-To: <$email>\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"----=MIME_BOUNDRY_main_message\"\n";
$headers .= "X-Sender: $first_name $family_name<$email>\n";
$headers .= "X-Mailer: PHP4\n";
$headers .= "X-Priority: 3\n";
$headers .= "Return-Path: <$email>\n";
$headers .= "This is a multi-part message in MIME format.\n";
$headers .= "------=MIME_BOUNDRY_main_message \n";
$headers .= "Content-Type: multipart/alternative; boundary=\"----=MIME_BOUNDRY_message_parts\"\n";
$message = "------=MIME_BOUNDRY_message_parts\n";
$message .= "Content-Type: text/html; charset=\"utf-8\"\n";
$message .= "Content-Transfer-Encoding: quoted-printable\n";
$message .= "\n";
$message .= "$body\n";
$message .= "\n";
$message .= "------=MIME_BOUNDRY_message_parts--\n";
$message .= "\n";
$message .= "------=MIME_BOUNDRY_main_message\n";
$message .= "Content-Type: application/octet-stream;\n\tname=\"" . $attachment_name . "\"\n";
$message .= "Content-Transfer-Encoding: base64\n";
$message .= "Content-Disposition: attachment;\n\tfilename=\"" . $attachment_name . "\"\n\n";
$message .= $data; //The base64 encoded message
$message .= "\n";
$message .= "------=MIME_BOUNDRY_main_message--\n";
$subject = 'bla bla bla';
$to="test#test.com";
mail($to,$subject,$message,$headers);
Why isn't the $body data not sent? Can you help me fix it?

Well, I'd suggest using the PEAR Mail_Mime package... It abstracts all that away...
As for your exact issue, I'd guess it's because you have two different boundaries and two Content-Type headers in the header section. Try generating something like this:
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="------=MIME_BOUNDARY_MESSAGE_PARTS"
------=MIME_BOUNDARY_MESSAGE_PARTS
Content-Type: text/html charset="utf-8"
$body
------=MIME_BOUNDARY_MESSAGE_PARTS
Content-Type: application/octet-stream;name="filename"
Content-Transfer-Encoding: base64
...
$data
------=MIME_BOUNDARY_MESSAGE_PARTS

Related

After sending email a security warning comes in email body

I am using php mail function with PDF attachment. Here everything is working fine but in the email body this following warning comes
"This email has an attachment that allows unverified scripts to run on your computer when opened. Be careful"
can you suggest ? what is the problem
Are you using the correct headers for sending attachments?
You can try using this pattern (tested and working):
$subject = 'SUBJECT';
$message .= 'Hello world'."\n";
$content = file_get_contents($file);
$content = chunk_split(base64_encode($content));
$separator = md5(time());
$eol = "\r\n";
$headers = "From: ME <me#mymail.org>".$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;
$body = "--".$separator.$eol;
$body .= "Content-Type: text/plain; charset=\"utf-8\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$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;
$body .= $content.$eol;
$body .= "--".$separator."--";
$filename is your attachment, $file is the full path (including $filename).

PHP Mail Attachment showing noname.txt

What's wrong in my code?
Mail sending fine and text also fine but the attachment showing like noname.txt
Please help.
$upload_name=$_FILES["resume"]["name"];
$upload_type=$_FILES["resume"]["type"];
$upload_size=$_FILES["resume"]["size"];
$upload_temp=$_FILES["resume"]["tmp_name"];
$file = $upload_temp;
$content = chunk_split(base64_encode(file_get_contents($file)));
$num = md5(uniqid(time()));
$email_to = "noname#example.com";
$email_subject = $name . " Applied for a job in website careers page";
$email_message = "<b>Form details below.</b><br />";
$email_message .= "<tr><td><strong>Last Name</strong> </td><td>".$name."</td></tr>";
$email_message .= "<tr><td><strong>Phone</strong> </td><td>".$phone."</td></tr>";
$headers .='Reply-To: '. $email_to . "\r\n" ;
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$num."\"\r\n\r\n";
$headers .= "This is a multi-part message in MIME format.\r\n";
$headers .= "--".$num."\r\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
$headers .= $email_message."\r\n\r\n";
$headers .= "--".$num."\n";
$headers .= "Content-Type:".$upload_type."; name=\"".$upload_name."\"\r\n\r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; filename=\"".$upload_name."\"\r\n\r\n";
$headers .= $content."\r\n\r\n";
$headers .= "--".$num."--";
if(#mail($email_to, $email_subject, "", $headers)){
echo "Mail Send";
}
else {
echo "Mail not sent.";
}
Please help. Thanks in advance.
Have you consider using PHPMailer? In my experience, saves yourself from a lot of these hassles.
Otherwise, try changing:
$headers .= "Content-Type:".$upload_type.";name=\"".$upload_name."\"\r\n\r\n";
To:
$headers .= "Content-Type: multipart/mixed; name=\"".$upload_name."\"\r\n\r\n";

HTML Email mixed (html & attachement) coming thoough as plain text

I've got an HTML email being sent by PHP, with a pdf attached via base64 encoding. It all works, except the email comes through as plain text, so you see all HTML tags and base64 output - obviously not ideal. There must be something I'm missing here to ensure it gets read as HTML & attachment.
If anybody can help that would be fantastic!
My PHP:
<?php $to = $email;
$message = 'testing...';
$subject = 'Health Insurance Quote Request';
$headers = "From: Andy - CEO Health.com.au <" . strip_tags('info#health.com.au') . ">\r\n";
$headers .= "Reply-To: Andy - CEO Health.com.au <". strip_tags('info#health.com.au') . ">\r\n";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$email_message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$email_message .= "--{$mime_boundary}\n";
//get PDF URL
$data = chunk_split(base64_encode(file_get_contents('http://example.com/doge.pdf')));
$email_message .= "Content-Type: {\"application/pdf\"};\n" . " name=\"$product\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$product\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$email_message .= "--{$mime_boundary}\n";
mail($email, $subject, $email_message, $headers);
Example of output:
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="==Multipart_Boundary_xc9c6c260d1e99ba11f86b40c6c7848e0x"
This is a multi-part message in MIME format.
--==Multipart_Boundary_xc9c6c260d1e99ba11f86b40c6c7848e0x
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
testing...
--==Multipart_Boundary_xc9c6c260d1e99ba11f86b40c6c7848e0x
Content-Type: {"application/pdf"};
name="HeartPlus65"
Content-Disposition: attachment;
filename="HeartPlus65"
cwt/SGdVm9X6LXQwvq03c+afGdAA9NlSAk1G3NyG2S5pDSY+CycXKDnuBJ5gFRupvZva0H2CXQqA
t9M6DlWx646bhhsx8NXdde0ES4z+8FP03XNEWTPMc/lWObbIG4ET4qxQ57eCm1V8RZOOzqG6+pu7
mYERMqHobI26KpfZYx27x8ESi65xkqOYkdbFIJ1qm2HXUEGZC0B1Jr2c6qmHeo3VULGurdIKhERP
Q6EJqtQ6rrWPbLiqzHgOlVLrfaIU8Zxe2SUhi4QTauK9G0/qbmjb2TD0HNkakoNlLbDzCql32d4A
khPjiE9rBQZGOvRvtxnEaGAlXimw7Vp4tIsqkjkI2JQ1upHBRgTA31CjqPNBjdM9ISRKHl9MLvc0
LeZUH8J7GNA1R+8mJvqkR0p5f7LZEbSkt7c2fopKX79LsEe2O7nv6qzJrc1xGreCsS
....
It may be not having a -- at the end of the last boundary tag
ie $email_message .= "--{$mime_boundary}\n";
to $email_message .= "--{$mime_boundary}--\n";
I'm not sure it there is a difference between multipart/mixed and multipart/alternative but this works for me
# Setup mime boundary
$mime_boundary = 'Multipart_Boundary_x'.md5(time()).'x';
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/alternative; boundary=\"$mime_boundary\"\n";
$headers .= "Content-Transfer-Encoding: 7bit\n";
$body = "This is a multi-part message in mime format.\n\n";
# Add in plain text version
$body .= "--$mime_boundary\n";
$body .= "Content-Type: text/plain; charset=\"charset=us-ascii\"\n";
$body .= "Content-Transfer-Encoding: 7bit\n\n";
$body .= $text_content;
$body .= "\n\n";
# Add in HTML version
$body .= "--$mime_boundary\n";
$body .= "Content-Type: text/html; charset=\"UTF-8\"\n";
$body .= "Content-Transfer-Encoding: 7bit\n\n";
$body .= $html_content;
$body .= "\n\n";
#Attachments
if ($path!='' && $filename!=''){
$file_size = filesize($path.$filename);
$handle = fopen($path.$filename, "r");
$filecontent = fread($handle, $file_size);
fclose($handle);
$filecontent = chunk_split(base64_encode($filecontent));
$body .= "--$mime_boundary\n";
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
$body .= "Content-Transfer-Encoding: base64\r\n";
$body .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$body .= $filecontent."\r\n\r\n";
}
# End email
$body .= "--$mime_boundary--\n";

PHP Sendmail sender name is always Apache when header have attachment

We are trying to send an email using sendmail. Everything works fine with normall headers but the moment we add attachment in the header, the sender name comes as Apache. Here is our code snippet
$from_email = "noreply#domain.com";
$separator = md5(time());
$eol = PHP_EOL;
$filename = "attachment.pdf";
$attachment = chunk_split(base64_encode(file_get_contents($filename)));
$text = "Hi!";
// main header (multipart mandatory)
$headers = "From:".$from_email.$eol;
$headers = "Bcc:user#domain.com".$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol;
$headers .= "Content-Transfer-Encoding: 7bit".$eol;
$headers .= "This is a MIME encoded message.".$eol.$eol;
// message
$message .= "--".$separator.$eol;
$message .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$message .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$message .= $text.$eol.$eol;
// attachment
$message .= "--".$separator.$eol;
$message .= "Content-Type: application/pdf".$eol;
$message .= "Content-Transfer-Encoding: base64".$eol;
$message .= "Content-Disposition: attachment; filename=\"".$filename."\"".$eol;
$message .= $attachment.$eol;
$message .= "--".$separator."--".$eol;
$b = mail($email, "Your Issue of the STQ",$message, $headers, "-fnoreply#domain.com");
By Adding -fnoreply#domain.com, we are getting like this in email header From: noreply#domain.com (Apache). Not sure where this Apache is coming from?
What could be the problem here.
Thanks
You need a dot on the second line.
$headers = "From:".$from_email.$eol;
$headers .= "Bcc:user#domain.com".$eol;
Make the header like this :
$headers .= 'From: <webmaster#example.com>' . "\r\n";
Also missing the dot on the second line as xyzz pointed

PHP mail with attachment - extra file: part 1.4

I'm using the following code to send an email with attachments:
$mime_boundary = "<<<--==+X[".md5(time())."]";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed;\r\n";
$headers .= " boundary=\"".$mime_boundary."\"";
$message .= "This is a multi-part message in MIME format.\r\n\r\n";
$message .= "--".$mime_boundary."\r\n";
$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n";
$message .= "\r\n";
$message .= "$message_body\r\n";
$message .= "--".$mime_boundary."\r\n";
foreach($attachments as $filename => $data)
{
$message .= "Content-Type: application/octet-stream;\r\n";
$message .= " name=\"$filename\"\r\n";
$message .= "Content-Transfer-Encoding: quoted-printable\r\n";
$message .= "Content-Disposition: attachment;\r\n";
$message .= " filename=\"$filename\"\r\n";
$message .= "\r\n";
$message .= chunk_split(base64_encode($data));
$message .= "\r\n";
$message .= "--".$mime_boundary."\r\n";
}
mail($email_address, $email_subject, $message, $headers);
Which works fine, except that an extra file is also attached (called "Part 1.4").
Is there a way to not have this added?
Cheers,
Dan.
IIRC the last part separator must be --something unique--, i.e. in your case
$message .= "--".$mime_boundary."--\r\n";
But mime mail is more or less a solved problem ( i.e. for an application developer it's boring when done correctly and reeeeally annoying when done wrong ;-) ). Do yourself a favor and use something like Swiftmailer or any other descend mailing library/class.

Categories