February 19, 2019
Hot Topics:

Sending Email from your PHP Applications

  • February 4, 2005
  • By W. Jason Gilmore
  • Send Email »
  • More Articles »

Communicating with website users via email is crucial to the success of any online service. The ability to deliver registration confirmations and newsletters, provide a convenient and relatively secure password recovery tool, and keep clients updated with shipping status reports are just a few of the reasons for incorporating email-based features into your website infrastructure. In this tutorial, I'll show you how to incorporate email delivery capabilities into your PHP applications via both its native mail command and a great third-party extension called HTML Mime Mail. I'll conclude the article with a few pointers regarding how to most efficiently carry out bulk email delivery.

The mail() Function

Sending email via a PHP script is easy because PHP offers a native function called mail(). It accepts five parameters, three of which are required and the other two optional. The required parameters include the intended recipient, subject, and message. The optional parameters include additional mail headers and execution options for the mail delivery service. On Unix-based systems this service is by default Sendmail, although you can use other email services such as postfix or qmail, provided that you use sendmail wrappers available to each package. You can specify the location of this service by modifying the configuration directive sendmail_path. On Windows-based systems PHP requires the address of an SMTP server, specified with the SMTP configuration directive. The mail() function looks like this:

boolean mail(string to, string subject, string message 
[, string addl_headers [, string addl_params]])
Let's consider a simple example using the mail() function. The following script sends an email to jason@example.com:
   $to = "jason@example.com";
   $from = "support@example.com";
   $title = "Support subscription confirmation";
$body = <<< emailbody
Dear subscriber,

This email confirms your purchase of a 30 day
email support subscription. Please direct all 
requests to support@example.com.

Thank you,
The Example.com support staff

   $success = mail($to,$from,$title,$body,

When taking advantage of the addl_headers parameter, keep in mind you'll need to Note that if you neglect to include the From header, the email will be sent by the Web server daemon owner. When using the Apache Web server, it's standard practice to use the user nobody or create a new user named apache or www for specifically this purpose. Therefore the above email sender address might look something like apache@example.com or www@example.com. Therefore, be sure to set this header value to lessen the possibility of confusion or accidental spam filtering.

The mail() function works great when you want to send just a simple email. However what about sending to multiple recipients? Sending HTML mail? Also, what about sending attachments? While you could use mail(), additional headers, and some additional programming to perform such tasks, there's another readily available solution that I'd like to introduce.

HTML Mime Mail

HTML Mime Mail is a very useful PHP class created and maintained by Richard Heyes (http://www.phpguru.org/). Available for free download and use under the BSD license, it's a fantastic class for sending MIME-based email. Offering an intuitive OOP syntax for managing email submissions, it's capable of executing all of the email-specific tasks discussed thus far, in addition to features such as handling attachments, sending plain-text emails, and sending HTML emails with embedded images. In this article I'll show you how to take advantage of this great tool.

Installing Mime Mail

HTML Mime Mail is available for download from Richard Heyes' website:

Uncompress the file to a location preferably accessible by PHP's include_path setting. If this setting isn't available to you (for instance, you're using a shared server ISP), then uncompress it to a convenient location. Then include the class in your script using the REQUIRE_ONCE statement. In the following example I've placed the HTML Mime Mail directory in the same directory as my script:

For this tutorial I'm using HTML Mime Mail version 2.5.1, which was rewritten for PHP 5. Take note that if you're still running PHP 4, you'll need to use an earlier version of the class.

Sending an Email

HTML Mime Mail's object-oriented interface makes sending email quite intuitive. In this first example I'll assign the sender address, subject, body text, and finally submit the message for delivery:



    // Instantiate a new HTML Mime Mail object
    $mail = new htmlMimeMail5();

    // Set the sender address
    // Set the reply-to address

    // Set the mail subject
    $mail->setSubject("Test HTML Mime Mail");

    // Set the mail body text
    $mail->setText("This is the body of the test email.");

    // Send the email!

As you can see, HTML Mime Mail offers a much more convenient means for creating and sending email messages than PHP's native interface. But this is just a sampling of this useful class's capabilities. Read on to learn more.

Page 1 of 2

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date