Home » Mail » Mail » Manual
An interface for sending EMails
Introduction
Introduction – How to send a mail and the mailer backends
How To send a mail
Mail supports different types of backends to send email. So two steps are necessary to send an email.
- Step 1 Create a new instance of a specific Mail-Backend with the factory() method.
- Step 2 Send the mail using the send() method.
The mailer backends
Mail supports three types of backends:
-
mailSends a mail using PHP's built-in mail() function. -
sendmailSends a mail using a sendmail program. -
smtpSends a mail directly connecting to a smtp server.
Mail::factory()
Mail::factory() – creates a mailer instance
Synopsis
require_once 'Mail.php';
object &factory (
string $backend
,
array $params = array()
)
Description
Creates a instance of a backend-specific mailer class.
Parameter
-
string $backend- the name of the backend"mail","smtp","sendmail" -
array $params- a array of backend specific parameters. List of parameter for the backends-
mail-
If safe mode is disabled,
$paramswill be passed as the fifth argument to the PHP mail() function. If$paramsis an array, its elements will be joined as a space-delimited string.
-
If safe mode is disabled,
-
sendmail-
$params["sendmail_path"]- The location of the sendmail program on the filesystem. Default is/usr/bin/sendmail. -
$params["sendmail_args"]- Additional parameters to pass to the sendmail. Default is-i.
-
-
smtp-
$params["host"]- The server to connect. Default islocalhost. -
$params["port"]- The port to connect. Default is25. -
$params["auth"]- Whether or not to use SMTP authentication. Default is FALSE. -
$params["username"]- The username to use for SMTP authentication. -
$params["password"]- The password to use for SMTP authentication. -
$params["localhost"]- The value to give when sending EHLO or HELO. Default islocalhost -
$params["timeout"]- The SMTP connection timeout. Default is NULL (no timeout). -
$params["verp"]- Whether to use VERP or not. Default is FALSE. -
$params["debug"]- Whether to enable SMTP debug mode or not. Default is FALSE. Mail internally usesNet_SMTP::setDebug. -
$params["persist"]- Indicates whether or not the SMTP connection should persist over multiple calls to the send() method. -
$params["pipelining"]- Indicates whether or not the SMTP commands pipelining should be used.
-
-
Return value
object - a specific Mail instance
or a PEAR_Error object on failure.
Throws
| Error code | Error message | Reason | Solution |
|---|---|---|---|
| NULL |
"Unable to find class for driver
xxx"
|
Mailer backend class was not found. |
Check the $backend parameter, if correct
reinstall and/or update your Mail package.
|
Note
This function should be called statically.
See
Mail::send()
Mail::send() – sends a mail
Synopsis
require_once 'Mail.php';
mixed send (
mixed $recipients
, array $headers
, string $body
)
Description
Sends a mail. The send() method is provided by the object returned from factory()
Parameter
-
mixed $recipients- an array or a string with comma separated recipients. -
array $headers- an associative array of headers. The header name is used as key and the header value as value. If you want to override the envelope sender of the email, set the Return-Path header and that value will be used instead of the value of the From: header. -
string $body- the body of the email.
Return value
boolean - TRUE or
a PEAR_Error object on failure.
Throws
| Mailer driver | Error code | Error message | Reason | Solution |
|---|---|---|---|---|
| sendmail | NULL | "No from address given." |
The $headers array requires
at least a from entry.
|
Add a From header:
|
| sendmail | NULL | "From address specified with dangerous characters." |
The from entry in the $headers array
contains one ore more characters which could be non-RFC compliant.
|
Check the given from address for characters like: spaces or
; or & or
` (backtick).
|
| sendmail | NULL |
"sendmail [path to sendmail]
not executable"
|
The path to sendmail program is not correct. No sendmail executable found there. |
Check the $param['sendmail_path'] entry
in your
Mail::factory() call. If you use
another mailer then sendmail, ie. qmail,
check installation of the mailer. Normally it should includes
a sendmail wrapper.
|
| sendmail | NULL |
"sendmail returned error code
code"
|
Sendmail returns a error, which must be handled by use. | See the documention of your mailer program. |
| smtp | PEAR_MAIL_SMTP_ERROR_CREATE |
"Failed to create a Net_SMTP object" | Failure in class creation. | Reinstall/update the Net_SMTP package. |
| smtp | PEAR_MAIL_SMTP_ERROR_CONNECT |
"Failed to connect to
host:port"
|
Connect to SMTP server failed. |
Check $param['port'] and
$param['host'] entries
in your
Mail::factory() call.
|
| smtp | PEAR_MAIL_SMTP_ERROR_AUTH |
"method authentication failure"
|
Authentication failed. |
Check $param['auth'],
$param['username'] and
$param['password'] entries
in your
Mail::factory() call.
Ensure to use the correct authentication method
for the SMTP server.
|
| smtp | PEAR_MAIL_SMTP_ERROR_FROM |
"No From: address has been provided" |
The $headers array requires
at least a from entry.
|
Add a From header:
|
| smtp | PEAR_MAIL_SMTP_ERROR_SENDER |
"Failed to set sender: from"
|
Setting the sender address failed. | Check the RFC-compliances of the sender address and the server connnectivity. |
| smtp | PEAR_MAIL_SMTP_ERROR_RECIPIENT |
"Failed to add recipient: recipient
"
|
Sending of recipient address failed. | Check the RFC-compliances of the recipient address and the server connnectivity. |
| smtp | PEAR_MAIL_SMTP_ERROR_DATA |
"Failed to send data" | Body of the mail message could not send | Check the RFC-compliances of the message body and the server connnectivity. |
Note
This function can not be called statically.
Example
<?php
include('Mail.php');
$recipients = 'joe@example.com';
$headers['From'] = 'richard@example.com';
$headers['To'] = 'joe@example.com';
$headers['Subject'] = 'Test message';
$body = 'Test message';
$params['sendmail_path'] = '/usr/lib/sendmail';
// Create the mail object using the Mail::factory method
$mail_object =& Mail::factory('sendmail', $params);
$mail_object->send($recipients, $headers, $body);
?>
See
RFC822 - Introduction
RFC822 - Introduction – email address validation
Description
This class performs email address checking according to the RFC822 specification.
Note that the class only checks for a proper format of the indicated email address. This means it is not guaranteed that the email address itself exists or is owned by the particular user. You may also want to send the user an email, and force them to respond.
Mail_RFC822::parseAddressList()
Mail_RFC822::parseAddressList() – extract the parts of a list of email addresses
Synopsis
require_once 'Mail/RFC822.php';
array parseAddressList (
string $address = ''
,
string $defaultDomain = 'localhost'
,
boolean $nestGroups
= null
,
boolean $validate
= null
)
Description
Extracts the given addresses into their parts.
Parameter
-
string $address- the address(es) to validate -
string $defaultDomain- the default domain to use in case of absence in the given email address. -
boolean $nestGroups- whether to return the structure with groups nested for easier viewing. -
boolean $validate- whether to validate atoms. Turn this off if you need to run addresses through before encoding the personal names, for instance.
Return value
array -
a nested array of anonymous objects.
If $nestGroups set to FALSE, you
can jump over the next paragraph.
Every array entry contains an object per group. This object has two attributes:
-
groupname- the name of the group -
addresses- an array of all addresses of a group
The addresses array consists of an
array of anonymous objects for each address. This object
comes with the following attributes:
-
personal- the name of the address owner -
comment- an array, an entry for each comment per address -
mailbox- the name of the mailbox, the part before the @ -
host- the name of the server, the part after the @
Throws
| Error code | Error message | Reason | Solution |
|---|---|---|---|
| NULL | every | The given address string is not RFC822 compliant | The error code contains a description of the error. |
Note
This function can be called statically.
This class checks the string only. It does not check for the existence of an email address.
Example
Extract some addresses
<?php
$address = 'My group: "Richard" <richard@localhost>;, ted@example.com (A comment)';
$addresses = Mail_RFC822::parseAddressList($address, 'phpguru.org', TRUE);
print_r($addresses);
?>