Solved

Sending Multi-Part e-Mail With PHP

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: Rob
Notes 8.5 Archiving Steps and Tips This article covers setting up a Notes archive, and helps understand some of the menu choices making setting up and maintaining a Notes archive file easier.
These days socially coordinated efforts have turned into a critical requirement for enterprises.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Many of my clients call in with monstrous Gmail overloading issues with Outlook. A quick tip is to turn off the All Mail and Important folders from synching. Here is a quick video I made to show you how to turn off these and other folders in Gmail s…

863 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

23 Experts available now in Live!

Get 1:1 Help Now