Sr. Developer, Backend
Professional Technical Resources
US-OR-Eugene

Justtechjobs.com Post A Job | Post A Resume

Sending Email from your PHP Applications
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:
<?php
   $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

emailbody;
   $success = mail($to,$from,$title,$body,
              "From:$from\r\nReply-To:support@example.com");
?>
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.
[ Next Page ]


Comments:
Mail in InboxMansoor05/25/08 01:06
Bulk mailerjoseph05/20/08 08:18
capturing quiz answer to send to recepientrush09/20/07 22:16
email being detected as spamnick07/20/07 21:46
Email with PHP in Qmail SystemGeethika07/06/07 01:13
sdhkdfsdddfdsfsd05/14/07 12:43
RE: sending email with image and text using PHPhenok05/02/07 02:09
sending mail to multiple recipients in phpHiranya05/01/07 01:33
RE: regarding mail functionTest04/27/07 03:57
send emailfiza12/13/05 22:54
HTML in MailMeasum09/14/05 01:40
RE: main not being deliveredSam08/03/05 01:30
regarding mail functionlampsunil08/02/05 08:36
Displaying MySQL data Chuck Cory07/23/05 12:48
main not being deliveredchris07/12/05 07:05
sending email with image and text using PHPsankar06/27/05 02:49
Request HelpDimas06/02/05 11:05
about PHP and Linuxtalal manaa05/18/05 10:09
How to support email in PHPAbir05/13/05 02:17
Purchasing Managementwale05/12/05 11:32
problemthaman05/04/05 10:19
its really hard to understsndsandeep04/27/05 16:06
extending execution timeEric04/24/05 23:54
mail functiongaurav04/22/05 06:22
Mail_Queue A Great AlternativeRobert04/19/05 02:12
 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.