Solved

Sending Multi-Part e-Mail With PHP

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
It’s a strangely common occurrence that when you send someone their login details for a system, they can’t get in. This article will help you understand why it happens, and what you can do about it.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

785 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