1 / 13

PHP Bible

PHP Bible. Chapter 29: E-mail. Summary. Understanding e-mail Receiving e-mail with PHP Sending e-mail with PHP More e-mail applications E-mail gotchas. Understanding e-mail. E-mail is one of the killer apps of the Internet, and it's been around in basically the same form for many years

Download Presentation

PHP Bible

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP Bible Chapter 29: E-mail

  2. Summary • Understanding e-mail • Receiving e-mail with PHP • Sending e-mail with PHP • More e-mail applications • E-mail gotchas

  3. Understanding e-mail • E-mail is one of the killer apps of the Internet, and it's been around in basically the same form for many years • Components of a simple mail server include • TCP/IP server: maintains a list of several services for which it is responsible, and answers each request by authenticator • Mail Transfer Agent (MTA), a/k/a SMTP server: accepts e-mail from another SMTP server and delivers it to the correct addressee's mail spool, also collects outgoing e-mail and tries to send it to other SMTP servers • Mail spool: Directory of file where messages are temporarily stored • Mail User Agent: (used by the Unix systems to read mail in the spool) • Mail-retrieval program (POP/IMAP): allows for the remote retrieval of mail • Mailing list manager: helps send large volumes of e-mail automatically

  4. Receiving e-mail with PHP • Web-based e-mail clients – which download mail from the POP/IMAP server and relay outgoing mail through the SMTP server – are taking over a larger and larger slice of the market because of their convenience and platform independence • Webmail may also be the best way to ensure private communications when using shared or public computers • PHP is one of the tools that can be used to develop these most useful applications • If you want to create your own webmail client in PHP, you have two alternatives • Implement it from scratch: DON'T DO IT • Modifying others PHP: www.phpclasses.org, www.horde.org/imp, squirrelmail.org

  5. Cosmetic changes • Remember that many open source programs make it very easy to grab some code and slap a coat of makeup on it, or redesign the user interface altogether • If you want to design a mail page that looks like a paper letter, a TV screen, or the command deck of the starship Enterprise – grab a DHTML manual and go to it • This is particularly helpful to those who would like to put out a branded webmail client, as many ISPs do • Remember: It's not cool to implicitly take credit for work you haven't done – a little acknowledgment, in the form of a small logo or link, goes a long way in the open source community • More and more Web-based apps are using themes or skins, which are basically style sheets plus graphic elements, to allow the quickest changes to their look and feel

  6. Sending E-mail with PHP • Sending mail is where PHP really comes into its own with e-mail • Before you can send any mail from your server, you will need to tweak the configuration file a little • Only one real mail sending function exists in PHP: mail() • This function, which returns a Boolean, attempts to send one message using the data within the parentheses mail('receiver@receipthost.com','A sample subject',"Body of e-mail\r\nwith lines separated by the newline character."); • This is the default minimum format: address of recipient, subject line, body • In this case, PHP will automatically add a From: me@sendhost to each message header • CAUTION: Although e-mail has been around in substantially the same form for decades, there's still no universal format for messages that is guaranteed readable by all Message Transfer Agents (MTAs) or mail clients. Some require a carriage return with a new line (\r\n) instead of just a newline. Some will choke on multiple addresses separated by commas. Some will not deliver e-mails without a proper date

  7. Sending E-mail with PHP (cont.) • Of course you can also use variables instead of hard-coded values: $address = 'santa@claus.com'; $subject = 'All I want for Christmas'; $body = 'All I want for Christmas is my two front teeth Sincerely, Joey'; $mailsend = mail($address, $subject, $body); echo $mailsend; • The mail() function returns 1 (TRUE) when PHP believes it has successfully sent mail. This does not necessarily mean that the mail was actually sent or received • Multiple recipients all go into the address field, with commas separating them (may not be supported by all MTAs – if not, use CC: instead) $address_list[] = 'me@here.com'; $address_list[] = 'you@there.com'; foreach ($address_list as $current_address) $address .= $current_address, ', '; $address = substr($address,0,strlen($address)-2);

  8. Sending E-mail with PHP (cont.) • Additional header info can be added to your message to control the appearance, addresses, and format of your messages in the argument after the body $extra_header = "From: me@sendhost.com\r\nbcc: php@sendhost.com\r\n Content-type: text/plain\r\nX-mailer: PHP/".phpversion(); $mailsend = mail($address, $subject, $body, $extra_header); • The additional header field is somewhat odd because it crams in several types of information that would normally be given their own fields • Kinds of things you can include in the extra header include: • Your name • Your e-mail address • A reply-to or bounce-to address • X-mailer and version number • MIME version • Content-type • Charset • Content-transfer-encoding • Copy (cc:) and blind-copy (bcc:) recipients

  9. More fun with PHP e-mail • Besides using PHP to construct full-blown mail clients, it's quite common to use the mail() function to send occasional mail when a particular event occurs on your Web site (e.g. a customer purchases some goods from you) • Sending mail from a form is quite likely the single most popular application of PHP's mail() function • Reasons you'd want to use PHP's mail function instead of a mailto: HTML construct include: • A significant proportion of your audience uses public browsers or Webmail or both (mailto: won't work for these people) • You want to impose tighter syntax on the messages you send • You want to put the contact information into a database as well as send the message • You want to reduce unwanted messages (spam) – spiders cannot retrieve your e-mail address from your PHP script, but they can from the mailto: links • You want to obviate the need for SMTP relaying from your main mail account

  10. More fun with PHP e-mail (example) *** titlehelp.html *** <html> <head> <title>titlehelp.html</title> </head> <body> <center> <table width="550"> <tr bgcolor= #FF9933><td align="center"><BR><H3>The ThrillerGuide.com<BR>"What was the name of that thriller?"<BR> Form</H3></td></tr> <tr><td> Did you once read an unforgettable thriller, but now you can't remember the name? Fill out as many of the fields below as you can, press the button to submit, and we'll search our sources and e-mail you back. </td></tr></table> </center> <FORM METHOD=post ACTION="TitleHelp.php"> <P>First name: <input type="text" size=30 name="FirstName"> <P>Last name: <input type="text" size=30 name="LastName"> <P>Your Email Address: <input type="text" size=30 name="Email"> <P>In approximately what year did the action of the book occur? <input type="text" size=4 name="Year"> <P>Can you remember any settings from the book? <input type="text" size=30 name="Setting"> <P>The gender of the protagonist(s) was: <br>

  11. More fun with PHP e-mail (example) *** titlehelp.html (cont)*** <ul> <input TYPE="radio" NAME="Gender" VALUE=1>Female<br> <input TYPE="radio" NAME="Gender" VALUE=2>Male<br> <input TYPE="radio" NAME="Gender" VALUE=3>One of each<br> <input TYPE="radio" NAME="Gender" VALUE=4>Two males<br> <input TYPE="radio" NAME="Gender" VALUE=5>Two females<br> </ul> <P>When the book first came out, it was: <br> <ul> <input TYPE="radio" NAME="Status" VALUE=1>A bestseller<br> <input TYPE="radio" NAME="Status" VALUE=2>A critic's darling<br> <input TYPE="radio" NAME="Status" VALUE=3>Neither<br> <input TYPE="radio" NAME="Status" VALUE=4>I don't know<br> </ul> <P>Please tell us anything else you can remember about this title (plot, characters, settings, cover, movie versions, etc.): <br><textarea NAME="Other" ROWS=6 COLS=50></textarea> <P><input type="submit" name="Submit"> </body> </html>

  12. More fun with PHP e-mail (example) *** titlehelp.php *** <html><head><title>titlehelp.php</title></head> <body> <?php // If you wished, you could also save this information to a database $LastName = $_POST['LastName']; $FirstName = $_POST['FirstName']; $Year = $_POST['Year']; $Setting = $_POST['Setting']; $Gender = $_POST['Gender']; $Status = $_POST['Status']; $Other = $_POST['Other']; $formsent = mail('help@example.com', 'What was the name of that thriller?', "Request from: $LastName $FirstName\r\nYear: $Year\r\nSetting(s): $Setting\r\nProtagonist gender: $Gender\r\nBook status: $Status\r\nOther identifying characteristics: $Other", "From: $Email\r\nBounce-to: help@example.com"); if ($formsent) { echo "<P>Hi, $FirstName. We have received your request for help, and will try to respond within 24 hours. Thanks for visiting ThrillerGuide.com!"; } else ( echo "I'm sorry, there's a problem with your form. Please try again."; ) ?> </body></html>

More Related