The source of email.php (click to demo the file) viewed 1020 times.
If I wrote this code, then it is licensed under the GPL. If someone else wrote it, then please ask them if you want to use the code.
<html><head>
<title>Sending E-Mail - Weav - WS Sig</title>
</head><body>
<h3>WS Sig - Sending An E-Mail via PHP</h3>
<?php
if ( (!$emailtoname) || (!$emailtoaddr) ){
// Printing the form
?>
<p>This is a quick script to show how to send a normal text email with PHP, plus to see what the output looks like. Once you receive it, I recommend you look at the raw email headers to see what's going on.</p>
</p>If you wish to run it, please enter your <b><u>real</u></b> email address. I promise we don't keep them, but there is no point in writing a false one.</p>
<form action="<?php print ($PHP_SELF); ?>" method="post">
<p>What is your name?<br>
<input type="text" name="emailtoname">
<p>What is your e-mail address?<br>
<input type="text" name="emailtoaddr">
<p>Please enter a brief comment:<BR>
<textarea cols="30" rows="4" name="comment"></textarea>
<p><input type="submit" value="Send E-Mail">
</form>
<?php
}
else {
//ERROR CHECKING
// Don't worry about figuring this regular expression out quite yet...
// It will test for address@domainname and address@ip
$regexp = "^[0-9a-z_\.-]+@(([0-9]{1,3}\.){3}[0-9]{1,3}|([0-9a-z][0-9a-z-]*[0-9a-z]\.)+[a-z]{2,3})$";
if (!eregi( $regexp, $emailtoaddr )){
echo "<p>The e-mail address you entered was incomplete or it contained invalid characters.<br>
<b>$emailtoaddr</b>";
exit();
}
$emailtohostname = substr( $emailtoaddr, ( strrpos( $emailtoaddr, "@" ) +1 ));
// Check's to see if the server has a functioning mail server.
if (!checkdnsrr( $emailtohostname, "MX" )) {
die("The following e-mail address is not valid: \"<b>$emailtoaddr</b>\"<br>Because it's server didn't respond.");
}
// Lets predefine some variables. Be sure to change the from address!
$emailto = "$emailtoname <$emailtoaddr>";
$emailfromaddr = "someone@somewhere.com";
$emailfromname = "Rod Apeldoorn";
$emailsubject = "PHP Mail Test";
// The body can be as long as you wish, and any combination of text and variables
$emailbody = "This is a test of the PHP mail system.\nIt looks like everything worked!";
$emailbody .= "\n\nYou left the following comment:\n\n$comment";
// Here we are forming one large header line
// Every header must be followed by a \n except the last
$emailheaders = "From: $emailfromname <$emailfromaddr>\n";
$emailheaders .= "Reply-To: $emailfromaddr\n";
// I write myself in here as a BCC so I can make sure no one is abusing the script
$emailheaders .= "Bcc: $emailfromaddr\n";
$emailheaders .= "X-Mailer: PHP/" . phpversion() . "\n";
// Note the Sender-IP. I write this one in just as a small security percaution
$emailheaders .= "X-Sender-IP: $REMOTE_ADDR";
// Because I predefined all of my variables, this mail() function looks nice and clean hmm?
// Commented out mail command
//mail( $emailto, $emailsubject, $emailbody, $emailheaders);
echo "<p>Sending e-mail to: $emailtoaddr<br>
Along with a bcc to: $emailfromaddr";
}
?>
</body>
</html>
If you want to have a look at the source code, chose a file from this list:
To colour code your own PHP paste it here: