Home Programming PHP Mail Example

Search

PHP Mail Example Print
Written by Chris Gountanis   

This code demonstrates the PHP mail function. It grabs all information passed by the submitting form and places it in the HTML based email. Advanced PHP mail header usage is also shown.

  

PAGE CODE: PHPMAIL.PHP

<?php

// start message body variable

$message = '<b>Start body of message here...<b><br><br>' ;

 

// start table which holds form variables if found

$message .= '<table border="1" cellspacing="5">';

$message .= '<tr><th>Variable</th><th>Value</th></tr>' ;

 

// loop all submitted form variables

foreach($_POST as $variable => $value) {

$message .= '<tr><td>' . $variable . '</td>';

$message .= '<td>' . $value . '</td>';

}

 

// end table for form variables

$message .= '</table>';

 

// include a link to debug client ip address

$message .= '<br><hr><a href="http://ws.arin.net/whois/?queryinput=' ;

$message .= $_SERVER['REMOTE_ADDR'] . '">' . $_SERVER['REMOTE_ADDR'] . '</a>';

 

// fix possible exploits

$message = str_replace("\n.", "\n..", $message);

 

// populate the email to variable

$to = ' This e-mail address is being protected from spambots. You need JavaScript enabled to view it ';

 

// populate the email subject variable

$subject = 'Email Acknowledgment';

 

// populate the email headers variable which is needed to dynamically assign who the email is from

$headers = 'MIME-Version: 1.0' . "\r\n".

'Content-type: text/html; charset=iso-8859-1' . "\r\n" .

'From: Mailing List < This e-mail address is being protected from spambots. You need JavaScript enabled to view it >' . "\r\n" .

'Bcc: This e-mail address is being protected from spambots. You need JavaScript enabled to view it ' . "\r\n" .

'X-Mailer: PHP/' . phpversion() . "\r\n";

 

// using php mail function send email with populated variables

mail($to, $subject, $message, $headers);

?>

Last Updated on Saturday, 09 February 2008 20:59