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);
}
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);
}
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..