Managing Email

If you want to write PHP scripts that handle email, there are a few things you need to understand first. This article, the first of two parts, introduces you to these concepts. It is excerpted from chapter 8 of the Zend PHP Certification Study Guide, written by Zend Technologies (Sams; ISBN: 0672327090).

Terms You’ll Need to Understand

  • sendmail wrapper

  • SMTP

  • Headers

  • MIME encoding

  • SMTP (Windows only)

  • smtp_port (Windows only)

  • sendmail_from (Windows only)

  • sendmail_path

Techniques You’ll Need to Master
  • Mail functions

  • URL functions

How Email Is Delivered

If you are going to be writing and deploying PHP scripts that generate and send email messages, you need to know something about how email gets delivered around the Internet. This will help you better understand and support your customers when problems arise. Figure 8.1 shows the main components of the email architecture.


Figure 8.1  How email is delivered.

Here are the standard terms that you will come across at some point or another.

MTA—Mail Transport Agent

When email is sent from organization to organization, it is sent from email server to email server. The software that runs on your email server and handles sending and receiving email from around the Internet is called the Mail Transport Agent (MTA for short). Examples of Mail Transport Agents are

  • sendmail

  • postfix

  • qmail

  • Microsoft Exchange

  • Lotus Notes

Mail transport agents talk to each other using the SMTP network protocol.

SMTP—Simple Mail Transport Protocol

The Simple Mail Transport Protocol (SMTP) is the standard network-layer protocol for transmitting an email message across the Internet.

Servers normally listen for incoming SMTP connections on port 25.

{mospagebreak title=MX Records}

When an MTA has an email message to send to another MTA, it has to convert the address in the To:, Cc:, or Bcc: line into an IP address. Everything after the @ sign in the address is taken to be the email domain. This is normally something such as @php.net.

The email domain isn’t the real name of a server. It looks like a real name (and has to follow the same rules), but it isn’t. It’s actually a special kind of DNS alias.

To receive email for your email domain, you have to add an MX record for that email domain to your DNS server.


Note - If you don’t set up an MX record in your DNS server, the MTA will look for a matching A record instead.


MUA—Mail User Agent

The Mail User Agent (MUA) is the jargon name for an email client.

Examples of Mail User Agents are

  • Outlook Express

  • Evolution

  • KMail

  • pine

  • mutt

  • Hotmail

A PHP script that sends email is also a type of Mail User Agent.

Mail User Agents read email directly from files on disk, via network protocols such as POP3 or IMAP, or via proprietary network protocols (as used by Microsoft Exchange).

Mail User Agents normally send their email by connecting to a Mail Transport Agent over the network via the SMTP network protocol. Some UNIX-based Mail User Agents might send their email instead by executing a sendmail wrapper program.

When a Mail User Agent connects to an MTA via SMTP to send email, it might use SASL to authenticate the user.

SASL—Simple Authentication and Security Layer

The Simple Authentication and Security Layer (SASL) is a tried and trusted way to bolt user-authentication onto existing network protocols. SMTP has been extended (via the SMTP AUTH command) to support SASL.

If an MTA has been configured to require authentication, only MUAs with built-in support for the SMTP AUTH command will be able to connect to send email.

{mospagebreak title=Other Emerging Technologies}

Although email is as old as the Internet itself, the rise of unsolicited bulk email (commonly called spam), the increasing number of modern viruses that transmit themselves via email, and the fraudulent use of genuine email addresses for criminal intent mean that we are at the start of a period of great change in how email will be delivered in the future.

Junk email filters have become very popular. They can be added both to the MTA and/or the MUA. It’s a fair bet that most of the email that these filters mark as ‘junk’ never gets read by a human. Junk email filters aren’t perfect; genuine email that looks very like real junk email will also get marked as junk. When you roll out a PHP application that sends email, you should perform some tests with your customer to make sure that your email will get past whatever junk email filter your customer uses.

Because of repeated security holes in MUAs, the more tech-savvy businesses and users do not accept delivery of HTML email. It’s very tempting to send HTML email—such emails look so much nicer than plain-text email. You should ensure that any PHP application you write that sends email always gives the user the option to choose between plain-text email and HTML email. You should never only support HTML email.

If you write and sell PHP software that works with email, it’s important that you keep abreast of the new technologies that are always coming out. When capturing requirements from your customer, always make sure that you’ve agreed what email technologies the customer has adopted—or is planning to adopt within six months of the end of your project. The customer will (somewhat naively) expect your PHP application to work perfectly with whatever changes he plans to make to his email infrastructure—and will blame you if it doesn’t.

{mospagebreak title=Preparing PHP}

Before you can send email from your PHP script, you must first ensure that your copy of PHP has been correctly configured.

If You Are Using PHP on UNIX

To send email from a PHP script running on UNIX, you must have a sendmail-compatible MTA installed on the same server that your PHP script runs on.

On UNIX systems, PHP sends email by running the command-line program

sendmail. sendmail is the de facto standard MTA for UNIX systems.

If you are using an alternative to sendmail, it must provide a sendmail wrapper. A sendmail wrapper is a drop-in replacement for the sendmail command-line program. It must accept the -t and -i command-line switches at the very least.

When PHP is compiled, the configure script searches for the sendmail command in

 /usr/bin:/usr/sbin:/usr/etc:/etc:/usr/ucblib:/usr/
lib

If configure cannot find the sendmail command, then sendmail support will be permanently disabled. The following PHP functions will either be missing entirely, or will always return an error:

  • mail()—will be missing

  • ezmlm_hash()—will be missing

  • mb_send_mail()—will always return false

When this happens, you must install a sendmail wrapper, and then recompile PHP.

Once PHP is compiled with sendmail support enabled, whenever your script sends email, PHP will use the sendmail command discovered by the configure script. If you ever need to override this default, set the sendmail_path in php.ini to point to the sendmail command that you want PHP to use.

sendmail_path = '/usr/local/bin/sendmail'
If You Are Using PHP on Windows or Netware

Although not documented in the PHP Manual, if you set the sendmail_path setting in your php.ini file, PHP will attempt to send email via the sendmail_wrapper—behaving exactly as if PHP were running on UNIX. This can catch you out, so remember to look for it when troubleshooting email problems.

If you do not have a sendmail wrapper available, PHP on Windows talks to the mail transport agent (MTA) directly via the SMTP network protocol. PHP needs to be configured to tell it where to find your MTA:

  • The SMTP setting in php.ini must be set to the hostname (or IP address) of the email server that your MTA is running on. The default value is localhost. You will probably want to change that.

  • The smtp_port setting in php.ini must be set to the port that your MTA is listening on. The default value is 25. You probably will not need to change that.


Note - It isn’t documented in the PHP Manual, but PHP on Novell Netware uses the same code for email support as PHP on Windows.



Caution - It is currently not possible to get PHP on UNIX to talk directly to the MTA via SMTP.


PHP on Windows does not support SASL. If your MTA is configured to require authentication, you will need to change the security on your MTA to enable PHP to send emails through successfully.

On UNIX, the MTA will automatically say that the email is from whichever user your PHP script is running as.

This can’t be done on Windows because PHP is connecting to the MTA over the network via SMTP. Instead, PHP will work out who the email is from by looking in these places in this order:

  • the from: header line passed to the mail() function

  • the sendmail_from setting in the php.ini file

PHP will display an error and refuse to send the email if it cannot determine who the email is from.

If you compile your own version of PHP on Windows, and you are going to use it to send emails, it’s important that you build PHP with the Perl-compatible regular expression (PCRE) library included. PHP on Windows relies on the PCRE functions to make sure that headers have the correct end of line character. If you build PHP on Windows without PCRE support, you might find that MTAs refuse to accept your emails for delivery.

With PHP correctly configured, you can now send email from your PHP scripts.

{mospagebreak title=Sending Email}

Use the PHP function mail() to send an email message from a PHP script.

The first parameter of the mail() function is the email address to send the email message to.

Assuming that you are running your PHP script on the server that is the MTA for the example.com email domain, and that there is a local user called fred, all of these are valid email addresses:

  • fred

    The MTA thinks you are trying to send an email to the local user fred.

  • fred@example.com

    This is the normal form for an email address, and the one that you are probably most familiar with.

  • fred @ example.com

    The MTA will automatically collapse the whitespace in the email address.

    Although perfectly legal, email addresses with whitespace are seldom seen today.

  • "Fred Bloggs" <fred@example.com>

    The MTA will automatically extract the fred@example.com from between the angular brackets.

    The entire string will be added as is to the From: line of the email message.

    Note that the double quotes are important—do not leave them out.

Sending an Email to More Than One Recipient

Add additional addresses to the to parameter. Separate them by a comma and a space:

fred, joe@example.com, "Jane Doe"
<jane.doe@example.com>

If you want to cc: or bcc: an email to someone, do this by adding additional headers to your email.

Please check back next week for the conclusion of this article.

[gp-comments width="770" linklove="off" ]

antalya escort bayan antalya escort bayan Antalya escort diyarbakir escort