Приглашаем посетить
Иностранная литература (ino-lit.ru)

Hack 10. Send HTML Email

Previous
Table of Contents
Next

Hack 10. Send HTML Email

Hack 10. Send HTML Email Hack 10. Send HTML Email

Use multipart email messages to send email content in both plain text and HTML format.

Email is another interface to your web application. Ideally, you want that interface (and any other, be it a phone or a handheld device) to be as full-featured as the one you provide through a web server. While this isn't always possible, it's a good goal to keep in mind, and it will help push you to create better user interfaces.

This hack describes how to send email using a multipart construction, where one part contains a plain-text version of the email and the other part is HTML. If your customers have HTML email turned off, they will still get a nice email, even if they don't get all of the HTML markup.

Figure 2-13 shows some different forms of mail messages. On the lefthand side is the simplest form of mail, the text message. At the top of the email is the header, which defines the subject, whom the mail is from, whom the mail is going to, and so on. These are followed by a carriage return, and finally, the message text.

Figure 2-13. Different mail forms
Hack 10. Send HTML Email


The emails in the middle and on the right in Figure 2-13 are multipart messages. The header remains much the same as with the text message, with the exception that some information about the multiple parts is included. The text is then placed within a part, and the HTML is placed in another part.

This way, mailers can decide which part of the message they want to display. Email with HTML that contains graphics can include the referenced graphics as additional parts of the message, as shown in the righthand image.

2.9.1. The Code

Save the code in Example 2-17 as htmlemail.php.

Example 2-17. Sending multipart emails in HTML and text formats
	<?php
	$to = "to@email.com";
	$to_full = "Sally Cool";
	$from = "from@email.com";
	$from_full = "Joe Schmoe";
	$subject = "HTML Mail Test";

	$random_hash = "zzz582x";

	ob_start();
	?>
	To: <?php echo($to_full); ?> <<?php echo($to); ?>>
	From: <?php echo($from_full); ?> <<?php echo($from); ?>>
	MIME-Version: 1.0
	Content-Type: multipart/alternative;
				boundary="==Multipart_Boundary_<?php echo($random_hash); ?>" 
	<?php 
	$headers = ob_get_clean(); 
	ob_start(); ?>

	This is a multi-part message in MIME format.

	--==Multipart_Boundary_<?php echo( $random_hash ); ?>
	Content-Type: text/plain; charset="iso-8859-1"
	Content-Transfer-Encoding: 7bit

	This is the text of the message in a simple text format.

	--==Multipart_Boundary_<?php echo( $random_hash ); ?>
	Content-Type: text/html; charset="iso-8859-1"
	Content-Transfer-Encoding: 7bit

	<html>
	<body>
	<p>Here is something with <b>HTML</b> formatting. That can include all of the
	usual:</p>
	<ul>
	<li>Bulleted lists</li>
	<li>Tables</li>
	<li>Images (if you include them as attachments or external links)</li>
	<li>Character formatting</li>
	<li>…and more!</li>
	</ul>
	</body>
	</html>
	
	--==Multipart_Boundary_<?php echo( $random_hash ); ?>--
	
	<?php
	$message = ob_get_clean();
	
	$ok = @mail( $to, $subject, $message, $headers );
	
	echo( $ok ? "Mail sent\n" : "Mail failed\n" );
	?>

2.9.2. Running the Hack

Change the email addresses and names in the script. Then use the command-line PHP interpreter to run the script:

	% php htmlmail.php
	Mail sent
	%

On my OS X machine, the email comes up rapidly in the Mail application, as shown in Figure 2-14.

Figure 2-14. The Mail.app application showing the message
Hack 10. Send HTML Email


Double-clicking on the message shows the content of the message. You can see the HTML formatting with the bulleted list in Figure 2-15.

Figure 2-15. The formatted HTML message
Hack 10. Send HTML Email


All of the standard markup works as long as it's supported by the browser embedded in the mail application.

The only hitch comes from images or other file-based resources. You have two alternatives with images. The first is to reference an image on a remote web server. There are a couple of problems with that approach, though. First, it doesn't work for offline mail reading. Second, many mailers have remote image grabbing turned off because spammers use this mechanism to see whether you have opened their mail, confirming that they've reached a valid address (expect lots more spam to follow).

The second approach is to embed the image as an attachment in another part of the multipart message. This will work, but the email messages themselves will be larger because of the base-64-encoded images.

I've found it's best to just stick with standard text formatting and what can be done with CSS when mailing with HTML. Obviously it's not possible to do that if the image is critical to the content of the email. As you have seen in this chapter, though, you can do a lot with just HTML alone [Hack #8].

2.9.3. See Also


Previous
Table of Contents
Next