Sending an HTML Message

You want to send an HTML message.

Use mail(). The difference between sending a plain text and an HTML text is in the message header. Headers are meta-information passed to the mail server to tell it how to treat the message. Because the email includes HTML text, images, sound, etc. it is formatting using the MIME format. So this type of email includes three extra headers:
  1. MIME-Version
  2. Content-Type
  3. Content-Transfer-Encoding

<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() {
	$to ='sohann@fhda.edu';
	$subject = 'feedback';

	// Set a sender through the message header
	$msgHeader = "From:{$_POST['email']}\r\n";

	// These two lines a re required
	$msgHeader .= "MIME-Version: 1.0\n";
	$msgHeader .= "Content-type: text/html; charset=us-ascii\n";

	$message = "
	<html>
	<head>
	<title>Sending an HTML Message</title>
	</head>
	<body>
	<h2 align=center>name: {$_POST['name']}<br /><br />Comments: {$_POST['comments']}</h2>
	</body>
	</html>
	";


	// Send email
	mail($to, $subject, $message, $msgHeader);

	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