Sending an HTML email from a PHP script

From Knowledge Kitchen
Jump to navigation Jump to search


This example sends an HTML email in PHP. The key difference between this and sending a plain text email is in the Content-Type header.


<?php
 
 $to = "Bob Johnson <bob@mailinator.com>";
 $from = "barack.obama@whitehouse.gov";
 $fromName = "Barack Obama";
 $subject = "Yo Bob!";
 $message = <<<END
 <html>
 	<body>
 		<h1>Hello from the President</h1>
 		<img style="float:right;" src="http://foodriot.com/wp-content/uploads/2013/06/bacon.jpg" />
 		<p>
 			Bacon ipsum dolor sit amet boudin rump beef ribs, ground round short ribs salami sirloin t-bone chuck venison pork belly pancetta turducken. Meatball prosciutto capicola, ball tip brisket pastrami ground round beef ribs tail rump meatloaf short loin pork chop. Shankle salami t-bone, andouille tenderloin ground round pork chuck. Kielbasa turkey biltong, tenderloin meatloaf boudin ham hock. Frankfurter short ribs flank, shank doner hamburger t-bone bacon.";
 		</p>
 	</body>
 </html>
 END;
 
 $headers = <<<END
 From: {$fromName} <{$from}>
 Reply-to: {$from}
 X-Mailer: PHP/5
 MIME-Version: 1.0
 Content-Type: text/html; charset=utf-8
 END;
 
 mail($to, $subject, $message, $headers);
 
 ?>


What links here