Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-26-2008, 05:07 PM   PM User | #1
syosoft
Regular Coder

 
Join Date: Sep 2006
Location: Vermont, USA
Posts: 154
Thanks: 0
Thanked 6 Times in 6 Posts
syosoft is an unknown quantity at this point
Thumbs up HTML & Text E-mail Made simple

Outline
This function allows you to very easily send out an email that has both html and text. The benefit to this should be obvious, but for those who dont know, this will enable users to receive a well formatted email in the format of their choice and not be forced to see HTML tags if they like to view all email as text.

Code:
function send_email($to='', $from='', $subject='', $html_content='', $text_content='', $headers='') { 
	# Setup mime boundary
	$mime_boundary = 'Multipart_Boundary_x'.md5(time()).'x';

	$headers  = "MIME-Version: 1.0\r\n";
	$headers .= "Content-Type: multipart/alternative; boundary=\"$mime_boundary\"\r\n";
	$headers .= "Content-Transfer-Encoding: 7bit\r\n";

	$body	 = "This is a multi-part message in mime format.\n\n";

	# Add in plain text version
	$body	.= "--$mime_boundary\n";
	$body	.= "Content-Type: text/plain; charset=\"charset=us-ascii\"\n";
	$body	.= "Content-Transfer-Encoding: 7bit\n\n";
	$body	.= $text_content;
	$body	.= "\n\n";

	# Add in HTML version
	$body	.= "--$mime_boundary\n";
	$body	.= "Content-Type: text/html; charset=\"UTF-8\"\n";
	$body	.= "Content-Transfer-Encoding: 7bit\n\n";
	$body	.= $html_content;
	$body	.= "\n\n";

	# Attachments would go here
	# But this whole email thing should be turned into a class to more logically handle attachments, 
	# this function is fine for just dealing with html and text content.

	# End email
	$body	.= "--$mime_boundary--\n"; # <-- Notice trailing --, required to close email body for mime's

	# Finish off headers
	$headers .= "From: $from\r\n";
	$headers .= "X-Sender-IP: $_SERVER[SERVER_ADDR]\r\n";
	$headers .= 'Date: '.date('n/d/Y g:i A')."\r\n";

	# Mail it out
	return mail($to, $subject, $body, $headers);
}
__________________
Active PHP/MySQL application developer available for immediate work.
syosoft.com mavieo.com - Remote Web Site Administration Suite - Reseller Ready
syosoft is offline   Reply With Quote
Old 06-27-2008, 10:11 AM   PM User | #2
saxx
New to the CF scene

 
Join Date: Jun 2008
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
saxx is an unknown quantity at this point
Thanks alot man! I've been looking for something simple like this for my flash site
saxx is offline   Reply With Quote
Old 08-19-2008, 10:00 PM   PM User | #3
student101
Regular Coder

 
student101's Avatar
 
Join Date: Nov 2007
Posts: 610
Thanks: 80
Thanked 13 Times in 13 Posts
student101 is on a distinguished road
Post Add a reply-to version

First off this is some really neat code, Thank you!
I felt that I needed to add my 2 cents of the reply-to part.

Code:
function send_email($to='', $from='', $subject='', $html_content='', $text_content='', $headers='') { 

# Setup mime boundary
$mime_boundary = 'Multipart_Boundary_x'.md5(time()).'x';

$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/alternative; boundary=\"$mime_boundary\"\r\n";
$headers .= "Content-Transfer-Encoding: 7bit\r\n";
$replyto .= "reply-to: email-address-here";

$body = "This is a multi-part message in mime format.\n\n";

# Add in plain text version
$body.= "--$mime_boundary\n";
$body.= "Content-Type: text/plain; charset=\"charset=us-ascii\"\n";
$body.= "Content-Transfer-Encoding: 7bit\n\n";
$body.= $text_content;
$body.= "\n\n";

# Add in HTML version
$body.= "--$mime_boundary\n";
$body.= "Content-Type: text/html; charset=\"UTF-8\"\n";
$body.= "Content-Transfer-Encoding: 7bit\n\n";
$body.= $html_content;
$body.= "\n\n";

# Attachments would go here
# But this whole email thing should be turned into a class to more logically handle attachments, 
# this function is fine for just dealing with html and text content.

# End email
$body.= "--$mime_boundary--\n"; # <-- Notice trailing --, required to close email body for mime's

# Finish off headers
$headers .= "From: $from\r\n";
$headers .= "X-Sender-IP: $_SERVER[SERVER_ADDR]\r\n";
$headers .= 'Date: '.date('n/d/Y g:i A')."\r\n";
$replyto .= "reply-to: email-address-here";
# Mail it out
return mail($to, $subject, $body, $headers);
}
__________________
Thanks for your support!
Update MySQL with checkboxes | Tell A Friend | Delete MySQL with checkboxes

Give thanks & resolve when done :thumbsup:
student101 is offline   Reply With Quote
Old 09-07-2008, 06:18 AM   PM User | #4
jlhaslip
Regular Coder

 
Join Date: Feb 2007
Location: Canada
Posts: 924
Thanks: 10
Thanked 56 Times in 55 Posts
jlhaslip is on a distinguished road
Shouldn't the reply-to header be as follows:
Code:
$headers .= "Reply-To: email-address-here \r\n";
jlhaslip is offline   Reply With Quote
Old 09-07-2008, 07:54 AM   PM User | #5
student101
Regular Coder

 
student101's Avatar
 
Join Date: Nov 2007
Posts: 610
Thanks: 80
Thanked 13 Times in 13 Posts
student101 is on a distinguished road
That would add 2(two) of the same mail addresses in the To box.
Edit: Your method, that is
__________________
Thanks for your support!
Update MySQL with checkboxes | Tell A Friend | Delete MySQL with checkboxes

Give thanks & resolve when done :thumbsup:

Last edited by student101; 09-07-2008 at 07:58 AM..
student101 is offline   Reply With Quote
Old 09-07-2008, 11:43 PM   PM User | #6
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,601
Thanks: 2
Thanked 397 Times in 390 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
Quote:
Originally Posted by student101 View Post
That would add 2(two) of the same mail addresses in the To box.
Edit: Your method, that is
No, that's not what the Reply-To header is for. You set it to the desired return address that the recipient's mail client should use when they hit "Reply". It should almost always be set to the same address as the "From" header. The only reason it wouldn't be was if you were sending from an address that isn't monitored for replies, or sending an e-mail for someone else. jlhaslip is correct about the case.

Last edited by Inigoesdr; 09-08-2008 at 04:37 AM..
Inigoesdr is offline   Reply With Quote
Old 09-07-2008, 11:49 PM   PM User | #7
student101
Regular Coder

 
student101's Avatar
 
Join Date: Nov 2007
Posts: 610
Thanks: 80
Thanked 13 Times in 13 Posts
student101 is on a distinguished road
ok cool.
__________________
Thanks for your support!
Update MySQL with checkboxes | Tell A Friend | Delete MySQL with checkboxes

Give thanks & resolve when done :thumbsup:
student101 is offline   Reply With Quote
Old 06-30-2011, 06:20 AM   PM User | #8
kimb
New to the CF scene

 
Join Date: Jun 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
kimb is an unknown quantity at this point
Some clients refused to show emails as html when I used the code as provided.

Replacing the \r\n with \n solved the problem.

If anyone has similar problems hopefully this is helpful.
kimb is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:49 AM.


Advertisement
Log in to turn off these ads.