Sending Email in PHP – Free PHP Tutorials

Many Web applications have a need to send email messages. For example, a contact form script typically processes a form submitted by a visitor and emails the form information to the Webmaster. Other common scenarios include “ tell a friend ” functions, as well as member registration and “ forgotten password ” functions that email information to members.

PHP includes built – in support for creating and sending email messages, which makes it very easy to send email from within your PHP scripts.


SENDING EMAIL

To send an email message in PHP, you use the mail() function. On Unix servers such as Linux and Mac OS X, PHP uses the operating system ’ s built – in mail transfer agent (MTA) to send the email.
On non – Unix servers such as Windows, PHP talks directly to an SMTP mail server (either on the server or on another machine).


At a minimum, mail() requires three arguments :

  1. A string containing the recipient ’ s email address (or a comma – separated list of email addresses if sending to multiple recipients).
  2. The email subject line, as a string.
  3. The message body, as a string mail() returns true if the mail was accepted for delivery by the mail server, or false if there was a problem. (Note that an email message might still eventually bounce, even if the mail server accepted it for delivery.)

For example, the following code sends a short email entitled
“ Hello ”, with a message body of “ Hi Jim, how are you? ”, to jim@example.com:

mail( “jim@example.com”, “Hello”, “Hi Jim, how are you?” );

You can also include the recipient ’ s real name in the recipient string, provided you follow it with the recipient ’ s email address in angle brackets.

For example:
mail( “Jim Smith < jim@example.com > ”, “Hello”, “Hi Jim, how are you?” );

To send a multi – line message, pass in a string that contains newline characters.

Here ’ s an example:
$message = “Hi Jim,

How are you?“;mail( “Jim Smith < jim@example.com > ”, “Hello”, $message );

Lines of text in an email message body should not exceed 70 characters in length. To ensure that your lines are of the correct length you can use PHP ’ s wordwrap() function:

$message = wordwrap( $message, 70 );


Specifying the Sender Address and Adding Headers

By default, when running on a Unix server such as Linux or Mac OS X, mail() usually sends messages from the Web server ’ s username, such as “ www ” or “ www – data ”.

If you want to send your email from a different “ from ” address, you can specify the address in a fourth argument to mail() , as follows:

mail( “Jim Smith < jim@example.com > ”, “Hello”, $message, “From: Bob Jones < bob@example.com > ” );

This fourth argument lets you specify additional headers to include in the mail message. In this case, just one header was added — the From: header — but you can add as many headers as you like.

Just make sure you separate each header by both a carriage return ( \r ) and line feed ( \n ) character; this is required by the specification for Internet email messages:

$extraHeaders = “From: Bob Jones < bob@example.com > \r\n” .
“Cc: Anna James < anna@example.com > \r\n” .
“X-Priority: 1 (Highest)\r\n” .
“X-Mailer: Matt’s PHP Script”;
mail( “Jim Smith < jim@example.com > ”, “Hello”, $message, $extraHeaders );

This code sets four headers: From: (the message sender), Cc: (an additional carbon – copied recipient), X – Priority: (a value from 1 to 5 indicating the importance of the message), and X – Mailer: (a header specifying the software that sent the message).

RFC 2822 defines the format of email messages, including all the different headers you can use in a
message. You can read it at: http://www.faqs.org/rfcs/rfc2822 .

X – Priority: and X – Mailer: are known as experimental headers and are not officially part of
RFC 2822 (though their usage is widespread).


Controlling the Return Path Email Address

If your script is running on a Unix Web server, you can pass additional command – line arguments to the MTA as a fifth argument to mail() . Often this is used to add a – f command – line argument in order to set the so – called envelope sender or return path email address:

mail( “Jim Smith < jim@example.com > ”, “Hello”, $message, “From: Bob Jones
< bob@example.com > ”, “-f bob@example.com” );

Most email messages contain two “ from ” headers: the From: header and the Return – Path: header.

The From: address is the one usually displayed when the email is viewed in a mail program, and the Return – Path: address is the one used to determine the “ real ” sender of the email for the purposes of sending back bounce messages, determining if the email might be spam, and so on.

Often the two headers contain the same email address.

However, when sending mail via a Web script the Return-Path: header is usually set to the Web server ’ s username.

This can be a problem if you want to receive bounce messages  and you don ’ t have access to the “ www ” mailbox on the server. By using the additional – f argument as just shown, you can set the Return – Path: address to be the same as the From: address .

There ’ s one caveat with using – f . If the Web server user isn ’ t trusted by the MTA, the MTA adds a warning header to the email message similar to the following:
X-Authentication-Warning: www.example.com: user set sender to bob@example.com

using -f. 

Though this isn ’ t usually shown to the recipient, it often results in the message being flagged as spam or otherwise treated as suspicious email.

To tell the MTA to trust the Web server user you usually need to add the Web server username to the /etc/mail/trusted – users file on the server. (If you don ’ t have access to this file, ask your Web hosting provider for assistance.)

How about if you ’ re running on a Windows server? In that case Windows doesn’ t use an MTA and instead talks directly to an SMTP server, you can easily set the return path in Windows via a php.ini setting called sendmail_from :

ini_set( “sendmail_from”, “bob@example.com” );
mail( “Jim Smith < jim@example.com > ”, “Hello”, $message, “From: Bob Jones
< bob@example.com > ” );

This is all about how to send emails in PHP.

Leave a Comment

Your email address will not be published. Required fields are marked *