Solved

Sending Multi-Part e-Mail With PHP

Posted on 2011-09-29
3
349 Views
Last Modified: 2012-05-12
Hello experts! I've got another one for ya!

I'm trying to build a function that will send a multi-part email

part 1 being a plain text version of the messages to be displayed to non HTML clients.

part 2 the HTML version.

can you please take a look at the below function let me know if you've got any ideas on how to make it wok.

function sendEmail($subject,$from,$to,$html_text,$plain_text){
$notice_text = "This is a multi-part message in MIME format.";
$semi_rand = md5(time());
$mime_boundary = "=MULTIPART_BOUNDARY_$semi_rand";
$mime_boundary_header = chr(34) . $mime_boundary . chr(34);

$body = "$notice_text
 
----$mime_boundary
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit

$plain_text

----$mime_boundary
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: 7bit

$html_text

----$mime_boundary----";

$headers = "From: " . $from . "\n";
$headers .= "ReplyTo: " . $from . "\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n";
$headers .= "boundary=" . $mime_boundary_header.'\n';
if (mail($to, $subject, $body, $headers)){
    echo "Email sent successfully. ";
	}else{
		echo "Email NOT sent successfully!";
	}
}
sendEmail($subject,$from,$to,$html_text,$plain_text);

Open in new window

0
Comment
Question by:Cybervanes
3 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 36816465
PHPmailer http://code.google.com/a/apache-extras.org/p/phpmailer/ already does that and the source code is available if you're interested.  It's frequently recommended.
0
 
LVL 2

Author Comment

by:Cybervanes
ID: 36816482
Yeah Ive looked into that... looking to learn how it works and build a specific function to suite my specific needs. thanks N E Way!
0
 
LVL 1

Accepted Solution

by:
ob2s earned 500 total points
ID: 36818851
Hi,

I changed a couple of things::
1. Fixed boundary delimiter problem
2. Added a boolean function parameter to allow a caller to specify either multipart/alternative or multipart/fixed content types, with the default being multipart/alternative -- which I'm guessing is what you want, based on your original post.

I ran a quick test and it seems to work for me.

Hope this helps.

<?php
function sendEmail($subject,$from,$to,$html_text,$plain_text,$alternative=true){
$notice_text = "This is a multi-part message in MIME format.";
$semi_rand = md5(time());
$mime_boundary = "=MULTIPART_BOUNDARY_$semi_rand";
$mime_boundary_header = chr(34) . $mime_boundary . chr(34);
$mime_subtype = empty($alternative) ? 'mixed' : 'alternative';

$body = "$notice_text

--$mime_boundary
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit

$plain_text

--$mime_boundary
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: 7bit

$html_text

--$mime_boundary--
";

$headers = "From: " . $from . "\n";
$headers .= "ReplyTo: " . $from . "\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/" . $mime_subtype . "; ";
$headers .= "boundary=" . $mime_boundary_header."\r\n";
if (mail($to, $subject, $body, $headers)){
    echo "Email sent successfully. ";
	}else{
		echo "Email NOT sent successfully!";
	}
}

$html_text = "
   <h1>Hi There</h1>
   <div>This is the message Body</div>
";
$plain_text = "Hi There\nThis is the message Body";

#sendEmail($subject,$from,$to,$html_text,$plain_text);
#sendEmail('Testing 1 2 3','me@example.com','you@example.com',$html_text,$plain_text);
?>

Open in new window

0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to fix Datetime in MySQL? 4 51
IP 10.0.1.2 / 255.0.0.0 61 56
PHP Mail error 3 28
what is the best way to connect my database to my new website using php and mysql ? 6 40
This is my first article on Expert Exchange on the Manual Method of Exporting Office 365 Mailboxes to PST format by using the eDiscovery mechanism of Office. Hope you will enjoy the article.
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
This Micro Tutorial will demonstrate the easy use of Gmail embedding images in your email so the recipient of your email can view them in context.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question