Sending multipart email from php, only download shows not the actual message - php

I'm struggling with sending my multipart email with plain text version as well as with HTML. Unfortunately when I receive it in Gmail it shows as downloadable message only as shown below:
The code is pasted below and although I went over like 100 questions here on SO I'm still stuck.
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From: donotreply#someaddress.com\r\n";
$headers .= "Subject: Test mail\r\n";
$headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\r\n";
$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 text version
$message .= $plainTextMessage;
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-type: text/html;charset=utf-8\r\n\r\n";
// html version here
$message .= $htmlMessage;
$message .= "\r\n\r\n--" . $boundary . "--";
return #mail($to, $subject, $message, $headers);

Related

how to send multipart/alternative email with PHP correctly

I'm trying to use the built-in PHP mail function to send multipart messages that contain a html and a plain text version of my message. I've been playing around with different encoding types but, I keep running into problems. Originally I set Content-Transfer-Encoding to Binary but, that resulted in exclamation points being placed every 78 characters. I also tried base64 but I believe that base64 is overkill for what I am doing.
All I'm doing is sending basic HTML, no encoded images, files, or attachments. I'd prefer an encoding method that would still allow the source code to be human readable.
I heard that Quoted-Printable is what I'm looking for but, when I attempted to send messages using that encoding type the result ending up looking really weird. I noticed a bunch of " symbols sprinkled throughout the message source code.
Here is the code I'm using:
$to = "to#test.com";
$subject = "test subject";
$boundary = uniqid('np');
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From: from-address#test.com\r\n";
$headers .= "Reply-To: reply-address#test.com\r\n";
$headers .= "Return-Path: return-path#test.com\r\n";
$headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\r\n";
$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";
$message .= "Content-Transfer-Encoding: Quoted-Printable\r\n";
$message .= $plainTextMessage;
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-Type: text/html; charset=UTF-8\r\n";
$message .= "Content-Transfer-Encoding: Quoted-Printable\r\n";
$message .= $HTMLmessage;
$message .= "\r\n\r\n--" . $boundary . "--";
$ok = mail($to,$subject,$message,$headers);
What the heck am I doing wrong here?
Hi try the following code,
$to = "to#test.com";
$subject = "test subject";
$plainTextMessage = "Hi all";
$HTMLmessage = "<b>Hi all</b>";
//$boundary = uniqid('np');
$boundary = md5(uniqid(time()));
$headers .= "From: from-address#test.com\r\n";
$headers .= "Reply-To: reply-address#test.com\r\n";
$headers .= "Return-Path: return-path#test.com\r\n";
$headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\r\n";
$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";
$message .= "Content-Transfer-Encoding: Quoted-Printable\r\n";
$message .= $plainTextMessage;
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-Type: text/html; charset=UTF-8\r\n";
$message .= "Content-Transfer-Encoding: Quoted-Printable\r\n";
$message .= $HTMLmessage;
$message .= "\r\n\r\n--" . $boundary . "--";
$ok = mail($to,$subject,$message,$headers);
May it will help
No need for a third party library or an external mail forwarding host. This is what I use and it works like a charm. It also fixes some potential security holes by forcing headers using the $from address that can otherwise reveal your system user :
private function sendMail($to, $from, $fromName, $subject, $text, $html)
{
$boundary = uniqid('np');
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From: ".$fromName." <".$from.">" . "\r\n";
$headers .= "X-Sender: ".$fromName." <".$from.">\n";
$headers .= "Return-Path: <".$from.">\n";
$headers .= "To: ".$to."\r\n";
$headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\r\n";
// 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 text body
$message .= $text;
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-type: text/html;charset=utf-8\r\n\r\n";
// Html body
$message .= $html;
$message .= "\r\n\r\n--" . $boundary . "--";
// Send mail
mail('', $subject, $message, $headers, '-f ' . $from);
}

PHP mail - Spam Problems

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.

PHP: Sending an html plus plain text email I got mail with empty body

This is php code
$boundary = uniqid("np");
$headers = "MIME-Version: 1.0\n" ;
$headers .= "From: $from\n";
$headers .= "To: $to\n";
if (!is_null($reply_to)) $headers .= "Reply-To: $reply_to \n";
$headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\n";
//here is the content body
$message = "This is a MIME encoded message.";
$message .= "\n\n--" . $boundary . "\n";
$message .= "Content-type: text/plain;charset=utf-8\n\n";
//Plain text body
$message .= "Hello,\nThis is a text email, the text/plain version.
\n\nRegards,\nYour Name";
$message .= "\n\n--" . $boundary . "\n";
$message .= "Content-type: text/html;charset=utf-8\n\n";
$message .= "Hello, <BR> This is a text email, the <bold>html version</bold>.";
$message .= "<BR>Regards";
$message .= "<BR>Your Name";
$message .= "\n\n--" . $boundary . "--";
$sendmail = mail($to, $title, $message, $headers);
$headers variable contains:
MIME-Version: 1.0
From: Italia S.p.a. <root#domain.it>
To: My Name <my_name#domain.it>
Reply-To: My Alt Name <my_alt_name#domain.it>
Content-Type: multipart/alternative;boundary=np536b88fc27327
$message variable contains:
This is a MIME encoded message.
--np536b88fc27327
Content-type: text/plain;charset=utf-8
Hello,
This is a text email, the text/plain version.
Regards,
Your Name
--np536b88fc27327
Content-type: text/html;charset=utf-8
Hello, <BR> This is a text email,
the <bold>html version</bold>.<BR>Regards<BR>Your Name
--np536b88fc27327--
Problem is: when sending using mail($from, $to, $message, $headers) mail is delivered, but it's empty
Header should be..
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
Check PHP mail() function.
Email headers must be separated by \r\n.

Plain text version of email is not working

So I have the script sending a multipart email of plain text and html, some reason the plaintext version is not working, but I can see that it is sending when I show original in gmail. It just shows the html though when I click message garbled?
// headers
$headers = "MIME-Version: 1.0\r\n";
$headers .= 'From: From Name <'. FROM_EMAIL. ">\r\n";
$headers .= 'To: '.$member->member_info['memem']. "\r\n";
$headers .= 'Bcc: receipts#gmail.com' . "\r\n";
$headers .= "Subject: Registration Receipt\r\n";
$headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\r\n";
// Additional headers
$message . "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-type: text/plain;charset=utf-8\r\n\r\n";
$message .= $receipt_txt;//plain text
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-type: text/html;charset=utf-8\r\n\r\n";
$message .= $receipt_html;//html
$message .= "\r\n\r\n--" . $boundary . "--";
mail( $member->member_info['memem'], 'Registration Receipt', $message, $headers );
Crap. I found it. "$message . "\r\n\r\n--" . $boundary . "\r\n";" should be "$message .= "\r\n\r\n--" . $boundary . "\r\n";" I feel dumb :(

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