Sending E-mail

The built-in PHP mail() function makes sending email simple. It returns TRUE if the message was delivered to the mails erver succesfully, FALSE otherwise.

mail (to, subject, message, additional_headers, additional_parameters)

Parameters Meaning
to The receiver(s). If multiple, must be comma-separated.
subject Subject of the message.
message The actual text of the message.
additional_headers (optional) If specified, it will be passed in the header of the message.
additional_paramters (optional) Used to pass additional parameters to the mail server. The use of this feature depends on the particular mail server version.

<html>
<head>
<title>PHP</title>
<meta Name="Author" Content="Hann So">
</head>
<body>
<p>
<?php
if (isset($_POST['submit'])) {
		process_form();
	}
	else {
		display_form();// display form for the first time
	}

function display_form() {
	echo <<<HTML
	<form action = "$_SERVER[SCRIPT_NAME]" method="post">
	Name:<br />
	<input type="text" name="name" size="50" maxlength="80" value="Hann" /><br />
	E-mail:<br />
	<input type="text" name="email" size="50" maxlength="80" value="hso@voyager.deanza.edu" /><br />
	Comments: <br />
	<textarea name="comments" rows="5" cols="30">Testing</textarea>
	<input type="submit" name="submit" value="Submit" />
	</form>
HTML;
}

function process_form() {
	$body = "name: {$_POST['name']}\n\nComments: {$_POST['comments']}";
	$body = wordwrap($body, 70);
	// Send email
	mail('hso@voyager.deanza.edu', 'Feedback', $body, "From:{$_POST[email]}");

	echo "<p>Thank you for your feedback.</p>";

	echo "<br /><a href=\"$_SERVER[SCRIPT_NAME]\">Send again?</a>";

}
?>
</p>
</body>
</html>

View the effect


E-Mail | Introduction | Sending E-mail | Sending an HTML Message | Sending E-mail with Headers
© 2008: Hann So
email: hso@voyager.deanza.edu