Chapter 12: Sending Email

Google

Now you can send a plain text email by calling a function and passing a few variables to it. The function call looks like this:

sendTextMessage($body, $name, $recipient);

You can get some more mileage out of this little function by passing a variable for the Subject from the outside and making it more generic, like this:

This is extremely simple because PHP's mail() function hides the code required to locate the mail program and feed all the parts of a message to it. Here's what the textbook description of the mail() function looks like:

boolean mail(string recipient, string subject, string body, string additional_headers);

'Boolean' means that the function does its job and returns either TRUE(1) or FALSE(0), meaning that it succeeded or failed. 'String' means that an argument should be a string. The last argument is technically optional.

You can generate email from inside a script in lots of ways. It can be in plain text, HTML or both. It can also include attachments. The FormMailer script included with this book does all of that. So there's not much point in covering the technical details here. Just use that script; you don't really need to know how it works.

<?php
function sendTextMessage($subject, $body, $name, $recipient) {
  if($recipient == "") {die();}
  $from = "steve@insanity.org";
  $additional_headers = "Content-Type: text/plain; charset=us-ascii\nFrom:
  $name <$from>\nReply-To: $from\nReturn-Path: $from";
  // Now you can send the message
  mail($recipient,$subject,$body,$additional_headers);
}
?>

Now the function call becomes:

sendTextMessage($subject, $body, $name, $recipient);


MIME types

Email messages can carry a lot more than just plain text. They can contain HTML code, file attachments, multimedia files (audio, video, etc.) and more. It's all a matter of creating the right headers for what you want to send. The first thing we need to explore is the Multipurpose Internet Mail Extensions or MIME types. There are literally hundreds of MIME types. The table on the next page shows a few of the more commonly used types:

Type/Subtype -- Purpose or file type
text/plain -- plain text
text/html -- HTML-formatted content
multipart/mixed -- Body parts are separate and independent. They must be kept in order.
multipart/alternative -- Body parts are different versions of the same message (text, HTML, etc.)
application/octet-stream --	Binary files that must be opened by an application (Excel, Word, etc.)
application/pdf -- Adobe® PDF file
application/zip -- ZIP compressed file
image/jpeg -- JPEG image file
image/gif -- GIF image file
audio/basic -- basic audio file
audio/mpeg -- MPEG audio file
video/quicktime -- QuickTime® video file
video/mpeg -- MPEG video file

Types and subtypes are combined using a forward-slash(/). For example, a plain-text message like in the code above uses "Content-Type: text/plain" as its MIME type. For an HTML message you would use "Content-Type: text/html."

For a complete list of the available MIME types, visit this page:

http://www.isi.edu/in-notes/iana/assignments/media-types/media-types

Previous Page   Table of Contents   Next Page

Copyright © 2004 Steve Humphrey