Please note, this is a STATIC archive of website www.thewebhelp.com from 28 Sep 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

PHP function to send html email

This easy to use function will allow you to send multipart/html email from your website.

The "multipart" means you can send both a html and a text version of your message, the receiver will see one of the versions depending on the program/application used to read the email.

When calling the function you define:

  • receiver name/email
  • email subject
  • plain text version of the email
  • html version of the email
  • sender name/email

<?php

if(!function_exists("tnt_multipart_mail")){
	function tnt_multipart_mail($email_receiver, $email_subject, $email_text, $email_html, $email_from){
		
		// bountry is a string that will delimit the text from html version of the message
		$email_boundary = uniqid('', true);
		
		// set the return (Bounce) address
		$return_path = $email_from;
		// if sender is formatted as "Name <[email protected]>" then take sender from inside the <> (last occurance)
		if(preg_match('/<([^<]*)>$/', $email_from, $receiver_matches)){
			$return_path = $receiver_matches[1];
		}

		// note that ORDER is important, e.g. content-type AFTER mime (says SpamAssassin score)
		$email_headers  = "From: ".$email_from."\n"; 
		$email_headers .= "MIME-Version: 1.0\n";
		$email_headers .= "Content-Type: multipart/alternative;\n";
		$email_headers .= " boundary=\"$email_boundary\"\n";
									
		// start building the text AND html parts of the message body
		$email_message = "";
		$email_message .= "--$email_boundary\n";
		$email_message .= "Content-Type: text/plain; charset=UTF-8\n";
		$email_message .= "\n";
		$email_message .= $email_text."\n";
		//
		$email_message .= "--$email_boundary\n";
		$email_message .= "Content-Type: text/html; charset=UTF-8\n";
		$email_message .= "\n";
		$email_message .= $email_html."\n";
		$email_message .= "--$email_boundary--";
		
		mail($email_receiver, $email_subject, $email_message, $email_headers, "-f$return_path");

	}
}

// replace "[email protected]" with the name of your receiver
// replace "Sender Name <[email protected]>" with your name and email, you can also just use email without name
// replace the content of the plain text and html part of the email
tnt_multipart_mail("[email protected]", "testing html email", "hello (this is plain text)", "<strong>Hello (this is html)</strong>", "Sender Name <[email protected]>");

?>

Download the files

Download the function PHP file (1KB)

Rate this content; the results will appear next to article links in site:
You need flash player