Solved

Sending Multi-Part e-Mail With PHP

Posted on 2011-09-29
3
360 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

We are happy to announce a brand new addition to our line of acclaimed email signature management products – CodeTwo Email Signatures for Office 365.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 …
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…

617 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