Sending E-mail with Headers

You want to set additional email headers to cc (carbon copy) or bcc (blind cc) people.

Use the additional_headers argument of the mail().

<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 />
	cc:<br />
	<input type="text" name="cc" size="50" maxlength="80" value="hso@voyager.deanza.edu" /><br />
	bcc:<br />
	<input type="text" name="bcc" 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";
	$msgHeader .= "cc:{$_POST['cc']}\r\n";
	$msgHeader .= "bcc:{$_POST['bcc']}\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