Solved

Sending Multi-Part e-Mail With PHP

Posted on 2011-09-29
3
307 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 82

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Explore the encryption capabilities built into Google Apps and how these features can help you meet privacy policy and regulatory compliance, but are not a full solution. Understand and compare the most popular email encryption services for Google A…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
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.

747 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now