I am testing an auto reply multipart email using PHP. The problem is when I receive the email in my hotmil, the whole content (including html and the random hashes) appears as raw text. Here is my code:
$cname = "test";
$to = "me#myemail.com";
$autoReplyTo = "me#myemail.com";
$autoReplySubject = "Enquiry";
$mime_boundary = md5(date('U'));
$autoReplyheaders = "From: XXXXX <" . $to . ">" . "\r\n" .
"MIME-Version: 1.0" . "\r\n" .
"Content-Type: multipart/alternative; boundary=$mime_boundary" .
"Content-Transfer-Encoding: 7bit". "\r\n";
$plain_text = "Dear " . $cname . ".\r\n";
$plain_text = "Thank you for contacting us.\r\n";
$plain_text .= "We are currently processing your query and will contact you shortly. We appreciate the time and interest you have shown in our company.\r\n";
$plain_text .= "Sales Team\r\n";
$plain_text .= "Note: This is an auto-generated email, please do not reply.\r\n";
$html_text = '<html><head><title>AUTO REPLY</title></head><body>';
$html_text .= '<p><img src="http://www.xxxxxx.xx/images/logo.png" /></p>';
$html_text .= '<p>Dear '.$cname.',<br />
Thank you for contacting us.<br />
We are currently processing your query and will contact you shortly.<br />
We appreciate the time and interest you have shown in our company.</p>';
$html_text .= '<p><b>Sales Team</b></p>';
$html_text .= '<p><i>Note: This is an auto-generated email, please do not reply.</i></p>';
$html_text .= '</body></html>';
$autoReplyMessage = "Auto reply" . "\r\n\r\n".
"--" . $mime_boundary.
"Content-Type: text/plain; charset=\"iso-8859-1\"".
"Content-Transfer-Encoding: 7bit". "\r\n\r\n".
$plain_text.
"--" . $mime_boundary.
"Content-Type: text/html; charset=\"iso-8859-1\"".
"Content-Transfer-Encoding: 7bit". "\r\n\r\n".
$html_text.
"--".$mime_boundary."--";
mail($autoReplyTo, $autoReplySubject, $autoReplyMessage, $autoReplyheaders);
What am I doing wrong?
This may or may not be the only issue, but you are missing line breaks after the Content-Type headers in the plain and HTML sections:
$autoReplyMessage = "Auto reply" . "\r\n\r\n".
"--" . $mime_boundary.
"Content-Type: text/plain; charset=\"iso-8859-1\"\r\n".
// ----------------------------------------------^^^^^
// Added \r\n
"Content-Transfer-Encoding: 7bit". "\r\n\r\n".
$plain_text. "\r\n".
//---------^^^^^^^^^
// Added another linebreak before MIME boundary
"--" . $mime_boundary.
"Content-Type: text/html; charset=\"iso-8859-1\"\r\n".
// ----------------------------------------------^^^^^
// Added \r\n
"Content-Transfer-Encoding: 7bit". "\r\n\r\n".
$html_text."\r\n\r\n".
// ---------^^^^^^^^^^
"--".$mime_boundary."--";
Rather than attempt to craft multipart/mime messages manually, lots of us here on SO would recommend a mailing library like PHPMailer which handles this much more easily. Building messages manually tends to be quite error-prone, and subject to inconsistencies between implementations by the SMTP server, or differences between native platform linebreaks.
Related
I am trying to generate an email on signing up to website, I am using PHP to create and send the email. My domain has both SPF and DKIM running on it. However when i spam check my email i get this:
[SPF] srv61.hosting24.com does not allow your server 31.220.105.111 to use face159#srv61.hosting24.com
[Sender ID] srv61.hosting24.com does not allow your server 31.220.105.111 to use face159#srv61.hosting24.com
Your message is not signed with DKIM
I have contacted my hosting and they confirm that all the authentication systems are working on there end, so i must be doing something wrong in the PHP code but for the life of me i can't find what here is how i generate the email:
//Emails link to voucher
//create a boundary for the email. This
$boundary = uniqid('np');
$subject = "The Forum - Little Thank You";
//headers - specify your from email address and name here
//and specify the boundary for the email
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From: TheForum#freewifisystems.co.uk" . "\r\n";
$headers .= "To: " . $email . "\r\n";
$headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\r\n";
//here is the content body
$message = "This is a MIME encoded message.";
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-type: text/plain;charset=utf-8\r\n\r\n";
//Plain Email
$content2 = "plain text email here";
//Plain text body
$message .= "Hello,\nThis is a text email as HMTL is dissable, Please enable HTML to continue. \n\nRegards,\nThe Forum";
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-type: text/html;charset=utf-8\r\n\r\n";
//Html body
$message .= $content3;
$message .= "\r\n\r\n--" . $boundary . "--";
//invoke the PHP mail function
mail($email, $subject, $message, $headers);
?>
$email and $content3 are set via SQL and are setting correctly the email forms and is well formatted.
I created a codes using php its working already with attachment but its only .doc i want to change it to .pdf i tried to change the application/msword to application/pdf and the file name from PO.doc to PO.pdf but its corrupted when i receive it on my email. Can you suggest me what should i do? Below is my codes for that. Thank you.
<?php
$headers = "From:<noreply#example.com>";
$to = 'email#example.com';
$subject = 'Purchase Order';
$txt .="
<html>
<body>
<p><b> PO Number:</b> $purchasenumber</p>
<p><b> Style Code:</b> $styleCode</p>
<p><b> Generic Number:</b> $gennum</p>
<p><b> Vendor Name:</b> $vendname</p>
<p><b> Planned Delivery Date:</b> $pdelivdate</p> <br/> <br/>";
// Always set content-type when sending HTML email
$message = "MIME-Version: 1.0" . "\r\n";
// $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$message .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$fileatt_name2 = "PurchaseOrder.doc";
$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}\"";
$data2 = chunk_split(base64_encode($txt));
$message = "{$mime_boundary}\n" .
"Content-Type: text/plain; charset=iso-8859-1; format=flowed\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message .= "{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
// Add file attachment to the message
$message .= "--{$mime_boundary}\n" .
"Content-Type: application/msword;\n" . // {$fileatt_type}
" name=\"{$fileatt_name2}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileatt_name2}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data2 . "\n\n" .
"--{$mime_boundary}--\n";
// Send the message
$send = mail($to, $subject, $message, $headers);
?>
This is not as simple as changing the extension (.doc, .pdf etc.) to convert a file from one type to the other. A more specialized process is needed. What you tried is analogous to taking an apple and dressing it up as a pear. It may look like a pear at first, but when taking a bite you'd discover it is actually an apple.
If you want to convert the .doc to a PDF you can convert it using programs like Microsoft Word (although I'm sure that Libre- or Open-office can do this too). If a more automated manner is desired, consider implementing other solutions like livedocs or phpdocs although the latter is quite pricy.
Depending on the platform (Linux, Windows, OSX) other options might be available as was answered in questions like this one.
For some reason in php when sending a "multipart/alternative" email it seems to double-up new lines messing up the formatting for plain-text clients.
Here's my code:
$boundary = uniqid('np');
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= "Content-Type: multipart/alternative;charset=utf-8;boundary=" . $boundary . "\r\n";
$headers .= "From: GamingOnLinux.com Notification <noreply#gamingonlinux.com>\r\n" . "Reply-To: noreply#gamingonlinux.com\r\n";
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-Type: text/plain;charset=utf-8\r\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n";
$message .= $plain_message;
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-Type: text/html;charset=utf-8\r\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n";
$message .= "$html_message";
$message .= "\r\n\r\n--" . $boundary . "--";
// Mail it
mail($to, $subject, $message, $headers);
So it ends up coming out like this:
--np53494d20d247e
Content-Type: text/plain;charset=utf-8
Content-Transfer-Encoding: 7bit
When there should be no line between them?
You are currently using MS-Windows style line endings (\r\n). Use Unix style line endings instead (\n) and most likely your issue is solved.
Different systems traditionally encode line ending different. But the unixoid version is the one which probably can be considered the "internet native style", since the internet (thus all network exchange stuff) was developed based on unixoid systems. Actually things were more complex, this obviously is a simplification. Therefore most (at least all sane) clients like email programs should be able to handle and disply such line ending correctly, whilst the MS-Windows style looks "funny" to many of them.
I am trying to send a multipart mail that contains both html and plain text. This is also one of the ways to get through spam filters and to allow more people to read the mail in case of not supporting HTML. After spending long hours googling, I have found some examples. I made my code, which sends the mail but it displays the text with the html tags, code, string etc.
<?php
$boundary=md5(uniqid(rand()));
$header .= "From:My Name<something#something.com>\n";
$header .= "Reply-To: something#something.com \n";
$header .= 'MIME-Version: 1.0'."\r\n";
$header .= 'Content-type: multipart/alternative;boundary=$boundary '."\n";
$adres = "something#gmail.com";
$subject = "subject";
$message = "This is multipart message using MIME\n";
$message .= "--" . $boundary . "\n";
$message .= "Content-type: text/plain;charset=iso-8859-1\n";
$message .= "Content-Transfer-Encoding: 7bit". "\n\n";
$message .= "Plain text version\n\n";
$message .="--" . $boundary . "\n";
$message .="Content-type: text/html;charset=iso-8859-1\n";
$message .= "Content-Transfer-Encoding: 7bit". "\n\n";
$message .="<html>
<body>
<center>
<b>HTML text version</b>
</center>
</body>
</html>\n\n";
$message .= "--" . $boundary . "--";
if(mail($adres, $subject, $message, $header))
{
print'message sent';
}
else
{
print'message was not sent';
}
?>
This is the result:
This is multipart message using MIME
--c071adfa945491cac7759a760ff8baeb
Content-type: text/plain;charset=iso-8859-1
Content-Transfer-Encoding: 7bit
Plain text version
--c071adfa945491cac7759a760ff8baeb
Content-type: text/html;charset=iso-8859-1
Content-Transfer-Encoding: 7bit
<html>
<body>
<center>
<b>HTML text version</b>
</center>
</body>
</html>
--c071adfa945491cac7759a760ff8baeb--
As you can see it displays the coding instead of the message alone. I have tried many solutions posted like:
adding/removing \r\n;
changing \r\n to \n;
changing content type from alternative to mixed;
I am learning PHP and all I know is all I have read and done so far. I have still much to learn so please if you could tell me where is the problem. I would be very thankful.Best regards.
The line:
$header .= 'Content-type: multipart/alternative;boundary=$boundary '."\n";
Has the wrong quotes, so $boundary won't be expanded. Change to:
$header .= "Content-type: multipart/alternative;boundary=$boundary\n";
And like I said in the comments, in the message headers and the content section headers you should be using \r\n as the line break since that's what is defined in the RFC. Most MTAs will allow simply \n, but some will choke on the message, and some spam filters will count every RFC violation as a point towards your spam score.
Using something like PHPMailer is a much better option because it formats everything perfectly by default, and abides by just about every single obscure, boring RFC.
I think you need quotes around the boundary string.
try this:
$header .= 'Content-type: multipart/alternative; boundary="' . $boundary . '"\r\n';
Try this example https://github.com/breakermind/PhpMimeParser/blob/master/PhpMimeClient_class.php
$m = new PhpMimeClient();
// Add to
$m->addTo("email#star.ccc", "Albercik");
$m->addTo("adela#music.com", "Adela");
// Add Cc
$m->addCc("zonk#email.au");
// Add Bcc
$m->addBcc("boos#domain.com", "BOSS");
// Add files inline
$m->addFile('photo.jpg',"zenek123");
// Add file
$m->addFile('sun.png');
// create mime
$m->createMime("Witaj!",'<h1>Witaj jak się masz? <img src="cid:zenek123"> </h1>',"Wesołych świąt życzę!","Heniek Wielki", "hohoho#domain.com");
// get mime
// $m->getMime();
// Show mime
echo nl2br(htmlentities($m->getMime()));
Here is the complete script without errors:
<?php
error_reporting(-1);
ini_set("display_errors", "1");
$mailto = "email#enter-domainname-here.com";
$subject = "subject";
$boundary=md5(uniqid(rand()));
$header = "From:Info<".$mailto.">\n";
$header .= "Reply-To: ".$mailto."\n";
$header .= "MIME-Version: 1.0"."\n";
$header .= "Content-type: multipart/alternative; boundary=\"----=_NextPart_" . $boundary . "\"";
$message = "This is multipart message using MIME\n";
$message .= "------=_NextPart_" . $boundary . "\n";
$message .= "Content-Type: text/plain; charset=UTF-8\n";
$message .= "Content-Transfer-Encoding: 7bit". "\n\n";
$message .= "Plain text version\n\n";
$message .="------=_NextPart_" . $boundary . "\n";
$message .="Content-Type: text/html; charset=UTF-8\n";
$message .= "Content-Transfer-Encoding: 7bit". "\n\n";
$message .="<html>
<body>
<center>
<b>HTML text version</b>
</center>
</body>
</html>\n\n";
$message .= "------=_NextPart_" . $boundary . "--";
if(#mail($mailto, $subject, $message, $header))
{
print'message sent';
}
else
{
print"message was not sent";
}
?>
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