Cool PHP Mail Script

Created: September 17, 2014
Last Modified: January 2, 2017
Subscribe to Internet Tips and Tools Feed
f Share
-
G+ Share
-
Tweet
-
in Share
-
P in it
-

Here are two simple functions to send email in PHP.

  1. Using standard PHP - php_mail($to, $message, $headers_array)
  2. Using PEAR with PHP - pear_mail($to, $message, $headers_array)

The standard PHP and the PEAR functions will send both a plaintext and an html email with this script. Note: php.net says "If intending to send HTML or otherwise Complex mails, it is recommended to use the PEAR package". I have noticed myself, that if you send many emails at once using PHPs mail function without PEAR then most of the emails do not get sent. dlc_b

Download

Download
coolphpmail.zip
Downloaded 0 times.
This script is provided for free but please consider making a donation to help with server costs and other expenses.

then unzip coolphpmail.php. Edit line 26 with your name and email address to test the sending to. Upload coolphpmail.php to your server and point your browser to coolphpmail.php. The script will detect that you are running the script directly and send two emails to your email address. One email using standard PHP mail() function and the other email using PEAR mail functions.

Check your email and see which functions worked or if both worked. Then to use coolphpmail.php in your own scripts include it with:

include("coolphpmail.php");

and then call it in your script with:

php_mail($to, $message, $headers_array)

or

pear_mail($to, $message, $headers_array)

depending on which one worked.

The $headers_array should be created as follows:

	$headers = array('From' => $from_email, 
			'Reply-To' => $replyto_email,
			'Subject' => $subject,
			'Cc' => $cc_email,
			'Bcc' => $bcc_email);
<?PHP
	/*  Cool PHP Mail Functions
		Version 1.0
		Created by Jeff Baker on 9/17/2014
		Copyright (C) 2014 Jeff Baker
		http://www.seabreezecomputers.com/phpmail/
		
		Two functions to send email in PHP
		1. Using standard PHP - php_mail($to, $message, $headers_array)
		2. Using PEAR with PHP - pear_mail($to, $message, $headers_array)
		
		The standard PHP and the PEAR functions will send both a plaintext and an html email
		with this script.
		Note: php.net says "If intending to send HTML or otherwise Complex mails, 
		it is recommended to use the PEAR package". I have noticed myself, that
		if you send many emails at once using PHPs mail function without PEAR
		then most of the emails do not get sent.
	*/
	$email_date = $date = date("F j, Y, g:i a");  // October 23, 2006, 5:15 PM
	$refer = $_SERVER['HTTP_REFERER']; // Referring webpage
	$ip = $_SERVER['REMOTE_ADDR']; // Sender IP address
	
	/* EDIT THE FOLLOWING VARIABLES */
	$from_email = "Your Name <name@email.com>";
	$replyto_email = "Reply Name <reply@email.com>";
	
	$to_email = "First Last <name@email.com>"; // Multiple: Jeff <jeff@email.com>, John <john@email.com>
	$cc_email = ""; // Send carbon copy to. Can also be multiple recipients with , separating
	$bcc_email = ""; // send blind carbon copy to. Can also be multiple recipients with , separating
	
	$subject = "Cool PHP Mail";
	
	$message = "<p><b>$subject</b></p>".
				"<p>Referer: <a href='$refer'>$refer</a><br />".
				"From: <a href='mailto:$from_email?subject=$subject'>$from_email</a><br />".
				"IP: <a href='http://whatismyipaddress.com/ip/$ip'>$ip</a><br />".
				"Date: $email_date<br />".
				"<p>This is a test of Cool PHP mail functions.</p>";				
				
	
	/* DO NOT EDIT BELOW THIS LINE */
	
	$headers = array('From' => $from_email, 
					'Reply-To' => $replyto_email,
					'Subject' => $subject,
					'Cc' => $cc_email,
					'Bcc' => $bcc_email);
					
	if (preg_match("/phpmail.php/i" ,$_SERVER['SCRIPT_NAME']))
	{				
		$headers['Subject'] = "Cool PHP Mail (Standard PHP mail function)"; 
	
		// Send using standard PHP mail() function
		php_mail($to_email, $message, $headers);
		
		$headers['Subject'] = "Cool PHP Mail (Pear PHP mail function)"; 
		
		// Send using Pear PHP mail->send() function
		pear_mail($to_email, $message, $headers);
	}	
	
	function php_mail($to, $message, $headers)
	{
		$cr = "\r\n";
		$message=stripslashes($message); // stripslashes if message is coming from POST form	
		// Create a html message
		$html_message=nl2br($message); // puts <br /> before all rn. Does not remove rn
		/* Create a plaintext message. The regex is replacing br and p tags with any number of 
			attributes or whitespace with $cr
		*/
		$text_message=preg_replace('/<br[^>]*>/i', "$cr", $message);
		$text_message=preg_replace('/<p[^>]*>/i', "$cr$cr", $text_message);
		$text_message=strip_tags($text_message); // Strip all html from plaintext message
		
		// Boundary between text and html can be any random text preceeded by -- in the email
		// Should be random enough that it is never in an email body
		$mime_boundary = "123coolphpmailboundary456";
		
		// Build php mail function headers
		/* mail headers are in the form:
			"From: jeff@email.comrnSubject: Hi Therern" etc.
		*/
		$email_headers = "";
		foreach ($headers as $key => $value) 
        	$email_headers .= $key . ": " . $value . "$cr";
        	
        // Build html headers
        $html_headers = "MIME-Version: 1.0" . "$cr".
        				"Content-Type: multipart/alternative; boundary="$mime_boundary"$cr".
        				"Content-Transfer-Encoding: 7bit$cr";
    
		// Create body with text_message and html_message separated by proper boundaries and headers	
		$body = "--$mime_boundary\n".
				"Content-Type: text/plain; charset="iso-8859-1"\n". 
				"Content-Transfer-Encoding: 7bit\n\n".
				$text_message."\n\n".
				"--".$mime_boundary."\n".
				"Content-Type: text/html; charset="iso-8859-1"\n". 
				"Content-Transfer-Encoding: 7bit\n\n".
				$html_message."\n\n".
				"--".$mime_boundary."--"."\n"; // Need to end with -- for mime email
		
		// send mail
		mail($to, $headers['Subject'], $body, $html_headers.$email_headers);	
		
		// For testing echo the message
		if (preg_match("/phpmail.php/i" ,$_SERVER['SCRIPT_NAME']))
			echo nl2br($html_headers.$email_headers.$body);
		
	} // end function php_mail($to, $message, $headers)


	function pear_mail($to, $message, $headers)
	{
		$cr = "\r\n";
		require_once ('Mail.php'); // this is required for pear mail
		require_once('Mail/mime.php'); // req'd by pear to send HTML email	
		
		$message=stripslashes($message); // stripslashes if message is coming from POST form	
		// Create a html message
		$html_message=nl2br($message); // puts <br /> before all rn. Does not remove rn
		/* Create a plaintext message. The regex is replacing br and p tags with any number of 
			attributes or whitespace with $cr
		*/
		$text_message=preg_replace('/<br[^>]*>/i', "$cr", $message);
		$text_message=preg_replace('/<p[^>]*>/i', "$cr$cr", $text_message);
		$text_message=strip_tags($text_message); // Strip all html from plaintext message
		
		$mime = new Mail_Mime(); // create a new instance of the Mail_Mime class 
		$mime->setTxtBody($text_message); // set our plaintext message 
		$mime->setHtmlBody($html_message); // set our HTML message 
		
		$body = $mime->get(); // builds the body of the message
		$hdrs = $mime->headers($headers); // builds the headers
		
		// create an instance of the mail backend
		$mail = &Mail::factory('mail'); 
		
		// send the mail
		$mail->send($to, $hdrs, $body); 
		
		// echo the response
		if (PEAR::isError($mail)) 
		{
			echo "Error: ",$mail->getMessage(), "<BR>";
		}
		else
		{
			if (preg_match("/phpmail.php/i" ,$_SERVER['SCRIPT_NAME']))
				echo "Your message has been sent!<BR>";
			
		}
		
		// For testing echo the message
		if (preg_match("/phpmail.php/i" ,$_SERVER['SCRIPT_NAME']))
			echo nl2br($hdrs.$body);
	
	} // end function php_mail($to, $message, $headers)

History

9/17/2014 - Version 1.0 - Cool PHP Mail Script created

Back to www.seabreezecomputers.com
Subscribe to Internet Tips and Tools Feed        

User Comments

There are 0 comments.

Displaying first 50 comments.