<?php $fiddle = "Hello.";
echo "The word is " . $fiddle;
//concatenate string literal and variable
?>
the echoed text will be
The word is Hello.
mail().header_identifier: value
<?php
mail(
"echang@wyrd.hood.edu", //recipient
"Testing PHP mail function", //subject
"This is just a test.
Its purpose is to see if the mail gets through. \n" //message
. $_SERVER["HTTP_REFERER"]. "\n"
. $_SERVER["REMOTE_ADDR"]. "\n"
,
"From: chang@hood.edu\r\nCc: echang@cs.hood.edu");
//two additional headers combined in single string
?>
The example is mail_1.php. PLEASE do not click on it repeatedly.
<?php $fiddle = "Hello.";
echo "The word is $fiddle"; //double quotes
echo 'The variable is $fiddle'; //single quotes
?>
The echos will output will be
The word is Hello
The variable is $fiddle
The example is process_mail_form.php
The program takes the From address and the message from a form.
Carelessly written email applications can be used to send spam.
In particular, if the recipient is taken from the form rather than determined in the program code, a malicious user can save and modify the form to insert their own list of recipients. That user could also call the program directly and repeatedly via a script thousands of times.
But what if you want the user to be able to choose which of several recipients should receive the message?
The solution is to send a code
<h1>Simple mail application</h1>
<?php
$who = $_POST["who"];
if ($who == "1") {
$recipient = "president@mycompany.com";
}
else if ($who == "2") {
$recipient = "trashbin@mycompany.com";
}
else if ($who == "3") {
$recipient = "mailboy@mycompany.com";
}
else {
$recipient = " Improper code";
}
if ($recipient != "bad code") {
// go ahead and process the message
//actual processing code would be inserted here
}
else {
echo "Choose one of the listed recipients";
}
echo "<p>Selected:", $recipient,"</p>";
?>
<hr />
<form method="post"
action="coded_recipient.php">
Send to: <select name="who">
<option value="1">Company president</option>
<option value="2">Complaint department</option>
<option value="3">Mail room</option>
</select>
<br />
My email: <input name="email"
type="text" /><br />
Message:<br />
<textarea name="message"
rows="15"
cols="60">
</textarea><br />
<input type="submit" />
</form>
The example uses a very simple (i.e. primitive) method of associating codes with addresses. More sophisticated programming would handle them in many different ways, storing the values in various data structures or a database.
The example is coded_recipient.php.
Hood College Department of Computer Science: Course materials © 1997-2006 by Elizabeth Chang.