This is the code am using to send an email with a doc file attached to it,
am receiving the email but the content in the email is displaying in a encoded text format
<?php
$to = $_POST['to'];
$from = $_POST['from'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$fileatt = $_FILES['upload_file']['tmp_name'];
$fileatt_type = $_FILES['upload_file']['type'];//application/msword
$fileatt_name = $_FILES['upload_file']['name'];
$headers = "From: $from";
if (is_uploaded_file($fileatt)) {
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
$data = chunk_split(base64_encode($data));
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
}
mail($to, $subject, $message, $headers);
?>
email am receiving is like this
message:This is a multi-part message in MIME format. --==Multipart_Boundary_x788f8ac40875c8c7a6540be6654be9c8x Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit --==Multipart_Boundary_x788f8ac40875c8c7a6540be6654be9c8x Content-Type: application/msword; name="File_name.doc" Content-Disposition: attachment; filename="File_name.doc" Content-Transfer-Encoding: base64 s pcEAW4AJBAAA+BK/AAAAAAAAEAAAAAAACAAAMhMAAA4AYmpiaqz6rPoAAAAAAAAAAAAAAAAAAAAA AAAJBBYAuEAAAM6QAQDOkAEAFwsAAAAAAAAaAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD//w8AAAAA AAAAAAD//w8AAAAAAAAAAAD//w8AAAAAAAAAAAAAAAAAAAAAALcAAAAAAEgHAAAAAAAASAcAAIsU AAAAAAAAixQAAAAAAACLFAAAAAAAAIsUAAAAAAAAixQAADQAAAAAAAAAAAAAAP////8AAAAAvxQA AAAAAAC/FAAAAAAAAL8UAABoAAAAJxUAAEwAAABzFQAAbAAAAL8UAAAAAAAAj0UAAMYCAADfFQAA FgAAAPUVAAAQAAAABRYAAAAAAAAFFgAAAAAAAAUWAAAAAAAAJBcAACYAAABKFwAADAAAAFYXAAAI AAAADkUAAAIAAAAQRQAAAAAAABBFAAAAAAAAEEUAAAAAAAAQRQAAAAAAABBFAAAAAAAAEEUAACQA AABVSAAAogIAAPdKAAA8AAAANEUAABUAAAAAAAAAAAAAAAAAAAAAAAAAixQAAAAAAABeFwAAAAAA AAAAAAAAAAAAAAAAAAAAAAAkFwAAAAAAACQXAAAAAAAAXhcAAAAAAABeFwAAAAAAADRFAAAAAAAA AAAAAAAAAACLFAAAAAAAAIsUAAAAAAAABRYAAAAAAAAAAAAAAAAAAAUWAAAfAQAASUUAABYAAADc GgAAAAAAANwaAAAAAAAA3BoAAAAAAABeFwAADAEAAIsUAAAAAAAABRYAAAAAAACLFAAAAAAAAAUW AAAAAAAADkUAAAAAAAAAAAAAAAAAANwaAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAXhcAAAAAAAAORQAAAAAAAAAAAAAAAAAA3BoAAAAAAADcGgAA pgEAAJY+AAAwAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKkIAAAAAAAAFFgAAAAAAAP////8AAAAA0NGIQpry zAEAAAAAAAAAAL8UAAAAAAAAahgAACIAAADGPwAAJAAAAAAAAAAAAAAA+kQAABQAAABfRQAAMAAA AI9FAAAAAAAA6j8AAEACAAAzSwAAAAAAAIwYAABQAgAAM0sAAEgAAAAqQgAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAADNLAAAAAAAAAAAAAAAAAACLFAAAAAAAACpCAADQAgAAXhcAAAAAAABeFwAAAAAAANwa AAAAAAAAXhcAAAAAAABeFwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAXhcA AAAAAABeFwAAAAAAAF4XAAAAAAAANEUAAAAAAAA0RQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAA== --==Multipart_Boundary_x788f8ac40875c8c7a6540be6654be9c8x--
headers:From: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="==Multipart_Boundary_x788f8ac40875c8c7a6540be6654be9c8x"
So can anyone help me with a perfect code
Thank u in advance
Find out the reason yourself by really debugging the issue at hand instead of guessing:
Dump the messages content you generate into a text file.
Dump any valid messages content you received in an email client into a file (the messages SOURCE code).
Compare both files and take your answer from the difference.
That works, really.
PHP doesn't support default utf-8 encoding, you need to pass the relevant encoding function as given in the following to your headers
Replace your code
Content-Type: text/plain; charset=\"iso-8859-1\"\n
to
"Content-Type:text/html;charset=utf-8"
Related
When I send file with eg. title: zażółć gęślą jaźń.pdf it don't pass(I dont get any email) but when i send eg. toSend.pdf all works. Both attachment contain "zażółć gęślą jaźń" and email body have no problem with utf-8
I'm trying to pass file by POST( ) and process it in php to make email attachment form it.
$toEmail = 'myEmail#myDomain';
$from = 'email#realDomain';
$fromName = 'Formularz kariera';
// Subject
$emailSubject = 'CV dla '.$wybor;
// Message
$htmlContent = '<h3> importantcontent zażółć gęślą jaźń</h3>';
$headers = "From: ".$fromName." <".$from.">";
if(!empty($uploadedFile) && file_exists($uploadedFile)){
// 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
$message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $htmlContent . "\n\n";
// Preparing attachment
if(is_file($uploadedFile)){
$message .= "--{$mime_boundary}\n";
$fp = #fopen($uploadedFile,"rb");
$data = #fread($fp,filesize($uploadedFile));
#fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"".basename($uploadedFile)."\"\n" .
"Content-Description: ".basename($uploadedFile)."\n" .
"Content-Disposition: attachment;\n" . " filename=\"".basename($uploadedFile)."\"; size=".filesize($uploadedFile).";\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
$message .= "--{$mime_boundary}--";
$mail = mail($toEmail, '=?utf-8?B?'.base64_encode($emailSubject).'?=', $message, $headers, /*$returnpath,*/ "-f email#realDomain");
// Delete attachment file from the server
#unlink($uploadedFile);
I dug through the internet in search of a solution but I have found none. I hope that's becouse Im noob in php
Code is good(enought) and problem was POSTFIX's SMTPUTF8 enabled on server ...
to fix i just.
postconf "smtputf8_enable = no"
AVE server logs!
Is there any issue with my below codes?
I'm trying to add an image as an attachment to HTML mail send it from my form. Users have an option to add images and on click of submit button it will be uploaded and sent.
But an error occurs on click of submit and within seconds it automatically redirects to success page, but no email is sent from the form. If there any mistakes in my code could someone clean the code and point out the mistakes I have done?
Thanks
require_once "Mail.php";
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$mime_boundary}\n";
// preparing attachments
$file = fopen($filename,"rb");
$data = fread($file,filesize($filename));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"".$fname."\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$fname\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}--\n";
$Subject = Trim(stripslashes($_POST['Subject']));
$Name = Trim(stripslashes($_POST['Name']));
$filename = "/uploads/" . $_FILES["file"]["name"];
$Email = Trim(stripslashes($_POST['Email']));
$EmailMe = 'shmshd12#gmail.com';
$ip = $_SERVER['REMOTE_ADDR'];
$filename="/uploads/file.jpeg";
$fname="file.jpeg";
$Messages = Trim(stripslashes($_POST['Message']));
$smtp = Mail::factory('smtp', array(
'host' => 'host',
'port' => '##',
'auth' => true,
'username' => 'mail#example.com',
'password' => '*********',
));
$Body = "<html> html email</html>";
$Body1 = "<html> html email</html>";
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
$headers .= "From: Roberta <request#robertalollobrigida.ga> \r\n";
$headers .= "Reply-To: < reply#example.com > \r\n";
$headers .= "Return-Path: user#example.com";
$headers .= "X-Mailer: PHP \r\n";
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$mime_boundary}\n";
// preparing attachments
$file = fopen($filename,"rb");
$data = fread($file,filesize($filename));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"".$fname."\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$fname\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}--\n";
$success = mail($Email, $Subject, $message, $headers, "-f " . $from);
mail($EmailMe, $Subject, $message, $ip, $headers, "-f " . $from);
if (success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=/thanks\">";
}
else{
echo "<meta http-equiv=\"refresh\" content=\"0;URL=/error\">";
}
i think its better to use PHPMailer
ot withot look to this example from php.net
There differenece in body, headers of email (with attachment, without
attachment), see this complete example below: work great for me (LINUX
, WIN) and (Yahoo Mail, Hotmail, Gmail, ...)
<?php
$to = $_POST['to'];
$email = $_POST['email'];
$name = $_POST['name'];
$subject = $_POST['subject'];
$comment = $_POST['message'];
$To = strip_tags($to);
$TextMessage =strip_tags(nl2br($comment),"<br>");
$HTMLMessage =nl2br($comment);
$FromName =strip_tags($name);
$FromEmail =strip_tags($email);
$Subject =strip_tags($subject);
$boundary1 =rand(0,9)."-"
.rand(10000000000,9999999999)."-"
.rand(10000000000,9999999999)."=:"
.rand(10000,99999);
$boundary2 =rand(0,9)."-".rand(10000000000,9999999999)."-"
.rand(10000000000,9999999999)."=:"
.rand(10000,99999);
for($i=0; $i < count($_FILES['youfile']['name']); $i++){
if(is_uploaded_file($_FILES['fileatt']['tmp_name'][$i]) &&
!empty($_FILES['fileatt']['size'][$i]) &&
!empty($_FILES['fileatt']['name'][$i])){
$attach ='yes';
$end ='';
$handle =fopen($_FILES['fileatt']['tmp_name'][$i], 'rb');
$f_contents =fread($handle, $_FILES['fileatt']['size'][$i]);
$attachment[]=chunk_split(base64_encode($f_contents));
fclose($handle);
$ftype[] =$_FILES['fileatt']['type'][$i];
$fname[] =$_FILES['fileatt']['name'][$i];
}
}
/***************************************************************
Creating Email: Headers, BODY
1- HTML Email WIthout Attachment!! <<-------- H T M L ---------
***************************************************************/
#---->Headers Part
$Headers =<<<AKAM
From: $FromName <$FromEmail>
Reply-To: $FromEmail
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="$boundary1"
AKAM;
#---->BODY Part
$Body =<<<AKAM
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="$boundary1"
This is a multi-part message in MIME format.
--$boundary1
Content-Type: text/plain;
charset="windows-1256"
Content-Transfer-Encoding: quoted-printable
$TextMessage
--$boundary1
Content-Type: text/html;
charset="windows-1256"
Content-Transfer-Encoding: quoted-printable
$HTMLMessage
--$boundary1--
AKAM;
/***************************************************************
2- HTML Email WIth Multiple Attachment <<----- Attachment ------
***************************************************************/
if($attach=='yes') {
$attachments='';
$Headers =<<<AKAM
From: $FromName <$FromEmail>
Reply-To: $FromEmail
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="$boundary1"
AKAM;
for($j=0;$j<count($ftype); $j++){
$attachments.=<<<ATTA
--$boundary1
Content-Type: $ftype[$j];
name="$fname[$i]"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="$fname[$j]"
$attachment[$j]
ATTA;
}
$Body =<<<AKAM
This is a multi-part message in MIME format.
--$boundary1
Content-Type: multipart/alternative;
boundary="$boundary2"
--$boundary2
Content-Type: text/plain;
charset="windows-1256"
Content-Transfer-Encoding: quoted-printable
$TextMessage
--$boundary2
Content-Type: text/html;
charset="windows-1256"
Content-Transfer-Encoding: quoted-printable
$HTMLMessage
--$boundary2--
$attachments
--$boundary1--
AKAM;
}
/***************************************************************
Sending Email
***************************************************************/
$ok=mail($To, $Subject, $Body, $Headers);
echo $ok?"<h1> Mail Sent</h1>":"<h1> Mail not SEND</h1>";
?>
This php script works just fine sending my pdf file by e-mail.
The problem is the script doesn't send any message as specified in $mainMessage.
Why do that problem occur, with the script only sending the pdf file without any message?
// Settings
$name = "Name";
$email = "someome#anadress.com";
$to = "$name <$email>";
$from = "email#email.com";
$subject = "Here is your attachment";
$mainMessage = "Hi, here's the file.";
$fileatt = "test.pdf";
$fileatttype = "application/pdf";
$fileattname = "newname.pdf";
$headers = "From: $from";
// File
$file = fopen($fileatt, 'rb');
$data = fread($file, filesize($fileatt));
fclose($file);
// This attaches the file
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$message = "This is a multi-part message in MIME format.\n\n" .
"-{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$mainMessage . "\n\n";
$data = chunk_split(base64_encode($data));
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatttype};\n" .
" name=\"{$fileattname}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileattname}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"-{$mime_boundary}-\n";
// Send the email
if(mail($to, $subject, $message, $headers)) {
echo "The email was sent.";
}
else {
echo "There was an error sending the mail.";
}
Try using \r\n instead of your \ns. Also, I would suggest using a library for sending emails, such as PHPMailer or SwiftMailer, instead.
EDIT: There appears to be an extra (or missing) " when you declare your charset (at "Content-Type: text/plain; charset=\"iso-8859-1\n" - it outputs Content-Type: text/plain; charset="iso-8859-1).
you didn't notice that you need to complete this line:
$message = "This is a multi-part message in MIME format.\r\n" .
"--{$mime_boundary}\r\n" .
"Content-Type: text/plain; charset=utf-8 \r\n" .
"Content-Transfer-Encoding: 7bit\r\n" .
$mainMessage . "\r\n\r\n";
you are missing one "-" in "-{$mime_boundary}\r\n".
it should be as the example I wrote :3
I am trying to make a html file upload to send via email with php. Here is the code snippet:
$attachment = chunk_split(base64_encode(file_get_contents($_FILES['file']['tmp_name'])));
$filename = $_FILES['file']['name'];
$boundary =md5(date('r', time()));
$headers = "From: webmaster#example.com\r\nReply-To: webmaster#example.com";
$headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\"";
$body="This is a multi-part message in MIME format.
--_1_$boundary
Content-Type: multipart/alternative; boundary=\"_2_$boundary\"
--_2_$boundary
Content-Type: text/plain; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit
test
--_2_$boundary--
--_1_$boundary
Content-Type: application/octet-stream; name=\"$filename\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--_1_$boundary--";
mail('email#example.com', 'Leidige stillinger', $body, $headers) or die("NO");
I get the email but with a junk of text, looks like the $boundary generates that big junk of text. Or I am doing this all wrong, first I have to upload the file somewhere in the server then send it via email
I've always preferred hand-rolling the MIME encoding as well, like you're doing, instead of using a library. You're close. Try this:
// to, from, subject, message body, attachment filename, etc.
$to = "to#to.com";
$from = "from#from.com";
$subject = "subject";
$message = "this is the message body";
$filename="/home/user/file.pdf"; //location of file - path and filename
$fname="file.jpeg"; //name of file for display purposes
$headers = "From: $from";
// 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
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$mime_boundary}\n";
// preparing attachments
$file = fopen($filename,"rb");
$data = fread($file,filesize($fname));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$fname\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$fname\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}\n";
$ok = #mail($to, $subject, $message, $headers, "-f " . $from);
I know there are lot posts related to PHP mail function. But seriously i couldn't figure out what's wrong with my code below:
$subject = "New article submitted.";
$message = "You've received this e-mail through your website's write an article form: \n";
$message .= "Name: {$data['username']} \n";
$message .= "E-mail: {$data['email']} \n";
$message .= "\nRegards,\nAakriti \n";
$headers = "From: {$data['name']} <{$data['email']}> \n";
// Boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
// Add a multipart boundary above the plain message
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain;\n charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n" .
"--{$mime_boundary}\n";
// preparing attachments
for($i=0;$i < count($_FILES['articledata']['name']); $i++) {
// store the file information to variables for easier access
$tmp_name = $_FILES['articledata']['tmp_name'][$i];
$type = $_FILES['articledata']['type'][$i];
$name = $_FILES['articledata']['name'][$i];
$size = $_FILES['articledata']['size'][$i];
$file = fopen($tmp_name,"rb");
$attachment = fread($file,filesize($tmp_name));
fclose($file);
$attachment = chunk_split(base64_encode($attachment));
print_r($attachment);
//$attachment = chunk_split(base64_encode(file_get_contents($tmp_name)));
$message .= "Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
"Content-Transfer-Encoding: base64\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$name}\"\n\n" .
$attachment . "\n" .
"--{$mime_boundary}\n";
$message .= "--\n\n";
}
$headers .= "Reply-To: <{$data['email']}>";
if (mail($recipient,$subject,$message,$headers)) {
echo "<p>Thank you! Your mail was successfully sent to the webmaster. Thank you for your time.</p>";
} else {
echo "<p>Sorry, there was an error and your mail was not sent. Please find an alternative method of contacting the webmaster.</p>";
}
It just doesn't work as intended. Attachment is comes out as some binary text. Take a look below:
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="==Multipart_Boundary_xf6a3dd3e1c0c1f9233b8c66ce2a73e3ex"Reply-To: <test03#gmail.com>
This is a multi-part message in MIME format.
--==Multipart_Boundary_xf6a3dd3e1c0c1f9233b8c66ce2a73e3ex
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
You've received this e-mail through your website's write an article form:
Name: Johnson
E-mail: test#gmail.com
Regards,
Aakriti
--==Multipart_Boundary_xf6a3dd3e1c0c1f9233b8c66ce2a73e3ex
Content-Type: image/jpeg;
name="599307_322143481199958_2058932593_n.jpg"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="599307_322143481199958_2058932593_n.jpg"
/9j/4AAQSkZJRgABAgAAAQABAAD//gAEKgD/4gIcSUNDX1BST0ZJTEUAAQEAAAIMbGNtcwIQAABt
bnRyUkdCIFhZWiAH3AABABkAAwApADlhY3NwQVBQTAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
9tYAAQAAAADTLWxjbXMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAApkZXNjAAAA/AAAAF5jcHJ0AAABXAAAAAt3dHB0AAABaAAAABRia3B0AAABfAAAABRyWFla
AAABkAAAABRnWFlaAAABpAAAABRiWFlaAAABuAAAABRyVFJDAAABzAAAAEBnVFJDAAABzAAAAEBi
VFJDAAABzAAAAEBkZXNjAAAAAAAAAANjMgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB0ZXh0AAAA
AEZCAABYWVogAAAAAAAA9tYAAQAAAADTLVhZWiAAAAAAAAADFgAAAzMAAAKkWFlaIAAAAAAAAG+i
AAA49QAAA5BYWVogAAAAAAAAYpkAALeFAAAY2lhZWiAAAAAAAAAkoAAAD4QAALbPY3VydgAAAAAA
AAAaAAAAywHJA2MFkghrC/YQPxVRGzQh8SmQMhg7kkYFUXdd7WtwegWJsZp8rGm/fdPD6TD////b
AEMABgQEBQQEBgUFBQYGBgcJDgkJCAgJEg0NCg4VEhYWFRIUFBcaIRwXGB8ZFBQdJx0fIiMlJSUW
HCksKCQrISQlJP/bAEMBBgYGCQgJEQkJESQYFBgkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQk
JCQkJCQkJCQkJCQkJCQkJCQkJCQkJP/CABEIAS8B4AMAIgABEQECEQH/xAAbAAACAwEBAQAAAAAA
AAAAAAAABQIDBAEGB//EABgBAQEBAQEAAAAAAAAAAAAAAAABAgME/8QAGAEBAQEBAQAAAAAAAAAA
AAAAAAECAwT/2gAMAwAAARECEQAAAfqgAAAHAPOabh2Vwm7+QWo06vqZa9w57WxTTLsFDRJ
UPDATE: Finally got it fixed after breaking my head for hours..
there were couple of problems:
As pointed out by asachanfbd mime
boundary was not properly nested.
This was one is silly & yet it was the main culprit - extra line breaks at below line of code:
$message .= "\nRegards,\nAakriti \n";
$headers = "From: {$data['name']} <{$data['email']}> \n";
Thanks everyone!
Not much of an answer to your question, but
you could have a look on PHPMailer and on its example.
It is pretty easy to add attachments with PHPMailer.
In a mail mime boundary is used to enclose different part of the mail. In your implementation you did not enclosed body of the mail in required mime type. Also keep in mind that mime boundary for the mail needs to be same for entire mail.
You can use these set of functions for easy implementation.
http://pastebin.com/AbZjzYdv