How to Send Mail in PHP

(1 vote, average 5.00 out of 5)

The Basics

PHP provides a handy function for mailing from a server.  On a basic level, all you need to do is call the function mail with three parameters, which has the following syntax.

bool mail(string $to, string $subject, string $message [, string $headers][, string $additonal_params]]) 

These parameters are pretty straight forward.  Below is an example of how to send a very simple email message with no headers.

1
2
3
4
$to = "example@wisc.edu";
$subject = "This is an example email.";
$message = "Here is my message.  Hopefully this email works!";
mail($to, $subject, $message);

Some notes about this basic usage:

  • To specify a newline in the message, use the '\n' character.
  • To specify multiple recipients, simply seperate each address by a comma in your $to string.
  • Being able to mail from php requires that your sendmail properties are set in php.  Typically this is done for you -- if you are starting your own server, however, you may have to initialize these variables by yourself.

Using Headers

Of course, modern email messages consist of much more than a subject, message, and receiver.  We may want to specify who the message was from, the date, the organization responsible for the email, etc.  PHP uses the $header string to specify all of these fields and more.  Each line of the header field (seperated by a '\n' character) represents a single entry in the header.  The syntax for a header entry is simply:

<Header Field>: value of the field

Below is an example of making a header in php and then calling mail with this header.

1
2
3
4
$header = "From: John Doe <john.doe@example.com>\n" .
      "To: The Receiver <receiver@example.com>\n" .
      "cc: Example Group <group@example.com>";
mail("john.doe@example.com", "Test Subject", "Hi\n\nHere is my message.", $header);

Some commonly used headers in PHP are:

From: The email address, and optionally the name of the authors.
To:
The email addresses of the recipients, and optionally their name.
bcc:
Blind Carbon Copy: Addresses to receive a copy of the email, but who do not appear in the recipient list.
cc:
Carbon Copy: Addresses to receive a copy of the email, but who are not directly referred to in the email.
Sender:
Address of the actual sender acting on behalf of the author listed in the From: field

 

Sending Attachments

Attaching a file in an email is something that is very common.  Unfortunately, attachments are not trivial in PHP.  A seperate article dedicated to this topic is currently in the works on The Tech Repo -- it will be linked here once it is completed.

Partner Links:
Last Updated on Thursday, 08 July 2010 16:43  
Related Articles

» How to View/Change Existing Startup Programs in Windows 7

If you wish to view or remove a list of programs which run at Windows startup, the following method is one of the easier ways to do so.Open the start menu and search for msconfig.  Click the msconfig.exe program. 2.  Click the "Startup" tab.  From here you can choose to disable or enable existing startup programs by unchecking or checking the box (respectively) next to the name.  Click Apply to save changes. 3.  Press the Boot tab and click the box for "Make all boot...

» Filter Spam and Messages Easily in Gmail

, Google's approach to email, has a little-known feature which makes it very easy to filter spam and easily manage email. Many people keep a "junk" email account which they use when signing up for websites and registration forms online. This way, when some of these websites spam their inbox with messages, it will go to their "junk" email account only and not their real one. Gmail has a way to eliminate the hassle of checking this separate account and wading through all of the useless emails in...

» PHP - Read in a file

The following function demonstrates how to read in a local file with PHP. You must first use the function to open the file handle. This tells PHP where to look to find this file. After that, it is simply a matter of iterating over the file until there are no more lines. The function tests if it has reached the end of the file yet - returning true if it has and false if there is still more to be read. Once we know there is another line to be read in, reads in the line for us into a slot of...