| 1 |
lars |
1 |
======================
|
|
|
2 |
The Net_SMTP Package
|
|
|
3 |
======================
|
|
|
4 |
|
|
|
5 |
--------------------
|
|
|
6 |
User Documentation
|
|
|
7 |
--------------------
|
|
|
8 |
|
|
|
9 |
:Author: Jon Parise
|
|
|
10 |
:Contact: jon@php.net
|
|
|
11 |
:Date: $Date: 2010-01-24 13:46:00 -0800 (Sun, 24 Jan 2010) $
|
|
|
12 |
:Revision: $Revision: 293948 $
|
|
|
13 |
|
|
|
14 |
.. contents:: Table of Contents
|
|
|
15 |
.. section-numbering::
|
|
|
16 |
|
|
|
17 |
Dependencies
|
|
|
18 |
============
|
|
|
19 |
|
|
|
20 |
The ``PEAR_Error`` Class
|
|
|
21 |
------------------------
|
|
|
22 |
|
|
|
23 |
The Net_SMTP package uses the `PEAR_Error`_ class for all of its `error
|
|
|
24 |
handling`_.
|
|
|
25 |
|
|
|
26 |
The ``Net_Socket`` Package
|
|
|
27 |
--------------------------
|
|
|
28 |
|
|
|
29 |
The Net_Socket_ package is used as the basis for all network communications.
|
|
|
30 |
|
|
|
31 |
The ``Auth_SASL`` Package
|
|
|
32 |
-------------------------
|
|
|
33 |
|
|
|
34 |
The `Auth_SASL`_ package is an optional dependency. If it is available, the
|
|
|
35 |
Net_SMTP package will be able to support the DIGEST-MD5_ and CRAM-MD5_ SMTP
|
|
|
36 |
authentication methods. Otherwise, only the LOGIN_ and PLAIN_ methods will
|
|
|
37 |
be available.
|
|
|
38 |
|
|
|
39 |
Error Handling
|
|
|
40 |
==============
|
|
|
41 |
|
|
|
42 |
All of the Net_SMTP class's public methods return a PEAR_Error_ object if an
|
|
|
43 |
error occurs. The standard way to check for a PEAR_Error object is by using
|
|
|
44 |
`PEAR::isError()`_::
|
|
|
45 |
|
|
|
46 |
if (PEAR::isError($error = $smtp->connect())) {
|
|
|
47 |
die($error->getMessage());
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
.. _PEAR::isError(): http://pear.php.net/manual/en/core.pear.pear.iserror.php
|
|
|
51 |
|
|
|
52 |
SMTP Authentication
|
|
|
53 |
===================
|
|
|
54 |
|
|
|
55 |
The Net_SMTP package supports the SMTP authentication standard (as defined
|
|
|
56 |
by RFC-2554_). The Net_SMTP package supports the following authentication
|
|
|
57 |
methods, in order of preference:
|
|
|
58 |
|
|
|
59 |
.. _RFC-2554: http://www.ietf.org/rfc/rfc2554.txt
|
|
|
60 |
|
|
|
61 |
DIGEST-MD5
|
|
|
62 |
----------
|
|
|
63 |
|
|
|
64 |
The DIGEST-MD5 authentication method uses `RSA Data Security Inc.`_'s MD5
|
|
|
65 |
Message Digest algorithm. It is considered the most secure method of SMTP
|
|
|
66 |
authentication.
|
|
|
67 |
|
|
|
68 |
**Note:** The DIGEST-MD5 authentication method is only supported if the
|
|
|
69 |
AUTH_SASL_ package is available.
|
|
|
70 |
|
|
|
71 |
.. _RSA Data Security Inc.: http://www.rsasecurity.com/
|
|
|
72 |
|
|
|
73 |
CRAM-MD5
|
|
|
74 |
--------
|
|
|
75 |
|
|
|
76 |
The CRAM-MD5 authentication method has been superseded by the DIGEST-MD5_
|
|
|
77 |
method in terms of security. It is provided here for compatibility with
|
|
|
78 |
older SMTP servers that may not support the newer DIGEST-MD5 algorithm.
|
|
|
79 |
|
|
|
80 |
**Note:** The CRAM-MD5 authentication method is only supported if the
|
|
|
81 |
AUTH_SASL_ package is available.
|
|
|
82 |
|
|
|
83 |
LOGIN
|
|
|
84 |
-----
|
|
|
85 |
|
|
|
86 |
The LOGIN authentication method encrypts the user's password using the
|
|
|
87 |
Base64_ encoding scheme. Because decrypting a Base64-encoded string is
|
|
|
88 |
trivial, LOGIN is not considered a secure authentication method and should
|
|
|
89 |
be avoided.
|
|
|
90 |
|
|
|
91 |
.. _Base64: http://www.php.net/manual/en/function.base64-encode.php
|
|
|
92 |
|
|
|
93 |
PLAIN
|
|
|
94 |
-----
|
|
|
95 |
|
|
|
96 |
The PLAIN authentication method sends the user's password in plain text.
|
|
|
97 |
This method of authentication is not secure and should be avoided.
|
|
|
98 |
|
|
|
99 |
Secure Connections
|
|
|
100 |
==================
|
|
|
101 |
|
|
|
102 |
If `secure socket transports`_ have been enabled in PHP, it is possible to
|
|
|
103 |
establish a secure connection to the remote SMTP server::
|
|
|
104 |
|
|
|
105 |
$smtp = new Net_SMTP('ssl://mail.example.com', 465);
|
|
|
106 |
|
|
|
107 |
This example connects to ``mail.example.com`` on port 465 (a common SMTPS
|
|
|
108 |
port) using the ``ssl://`` transport.
|
|
|
109 |
|
|
|
110 |
.. _secure socket transports: http://www.php.net/transports
|
|
|
111 |
|
|
|
112 |
Sending Data
|
|
|
113 |
============
|
|
|
114 |
|
|
|
115 |
Message data is sent using the ``data()`` method. The data can be supplied
|
|
|
116 |
as a single string or as an open file resource.
|
|
|
117 |
|
|
|
118 |
If a string is provided, it is passed through the `data quoting`_ system and
|
|
|
119 |
sent to the socket connection as a single block. These operations are all
|
|
|
120 |
memory-based, so sending large messages may result in high memory usage.
|
|
|
121 |
|
|
|
122 |
If an open file resource is provided, the ``data()`` method will read the
|
|
|
123 |
message data from the file line-by-line. Each chunk will be quoted and sent
|
|
|
124 |
to the socket connection individually, reducing the overall memory overhead of
|
|
|
125 |
this data sending operation.
|
|
|
126 |
|
|
|
127 |
Header data can be specified separately from message body data by passing it
|
|
|
128 |
as the optional second parameter to ``data()``. This is especially useful
|
|
|
129 |
when an open file resource is being used to supply message data because it
|
|
|
130 |
allows header fields (like *Subject:*) to be built dynamically at runtime.
|
|
|
131 |
|
|
|
132 |
::
|
|
|
133 |
|
|
|
134 |
$smtp->data($fp, "Subject: My Subject");
|
|
|
135 |
|
|
|
136 |
Data Quoting
|
|
|
137 |
============
|
|
|
138 |
|
|
|
139 |
By default, all outbound string data is quoted in accordance with SMTP
|
|
|
140 |
standards. This means that all native Unix (``\n``) and Mac (``\r``) line
|
|
|
141 |
endings are converted to Internet-standard CRLF (``\r\n``) line endings.
|
|
|
142 |
Also, because the SMTP protocol uses a single leading period (``.``) to signal
|
|
|
143 |
an end to the message data, single leading periods in the original data
|
|
|
144 |
string are "doubled" (e.g. "``..``").
|
|
|
145 |
|
|
|
146 |
These string transformation can be expensive when large blocks of data are
|
|
|
147 |
involved. For example, the Net_SMTP package is not aware of MIME parts (it
|
|
|
148 |
just sees the MIME message as one big string of characters), so it is not
|
|
|
149 |
able to skip non-text attachments when searching for characters that may
|
|
|
150 |
need to be quoted.
|
|
|
151 |
|
|
|
152 |
Because of this, it is possible to extend the Net_SMTP class in order to
|
|
|
153 |
implement your own custom quoting routine. Just create a new class based on
|
|
|
154 |
the Net_SMTP class and reimplement the ``quotedata()`` method::
|
|
|
155 |
|
|
|
156 |
require 'Net_SMTP.php';
|
|
|
157 |
|
|
|
158 |
class Net_SMTP_custom extends Net_SMTP
|
|
|
159 |
{
|
|
|
160 |
function quotedata($data)
|
|
|
161 |
{
|
|
|
162 |
/* Perform custom data quoting */
|
|
|
163 |
}
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
Note that the ``$data`` parameter will be passed to the ``quotedata()``
|
|
|
167 |
function `by reference`_. This means that you can operate directly on
|
|
|
168 |
``$data``. It also the overhead of copying a large ``$data`` string to and
|
|
|
169 |
from the ``quotedata()`` method.
|
|
|
170 |
|
|
|
171 |
.. _by reference: http://www.php.net/manual/en/language.references.pass.php
|
|
|
172 |
|
|
|
173 |
Server Responses
|
|
|
174 |
================
|
|
|
175 |
|
|
|
176 |
The Net_SMTP package retains the server's last response for further
|
|
|
177 |
inspection. The ``getResponse()`` method returns a 2-tuple (two element
|
|
|
178 |
array) containing the server's response code as an integer and the response's
|
|
|
179 |
arguments as a string.
|
|
|
180 |
|
|
|
181 |
Upon a successful connection, the server's greeting string is available via
|
|
|
182 |
the ``getGreeting()`` method.
|
|
|
183 |
|
|
|
184 |
Debugging
|
|
|
185 |
=========
|
|
|
186 |
|
|
|
187 |
The Net_SMTP package contains built-in debugging output routines (disabled by
|
|
|
188 |
default). Debugging output must be explicitly enabled via the ``setDebug()``
|
|
|
189 |
method::
|
|
|
190 |
|
|
|
191 |
$smtp->setDebug(true);
|
|
|
192 |
|
|
|
193 |
The debugging messages will be sent to the standard output stream by default.
|
|
|
194 |
If you need more control over the output, you can optionally install your own
|
|
|
195 |
debug handler.
|
|
|
196 |
|
|
|
197 |
::
|
|
|
198 |
|
|
|
199 |
function debugHandler($smtp, $message)
|
|
|
200 |
{
|
|
|
201 |
echo "[$smtp->host] $message\n";
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
$smtp->setDebug(true, "debugHandler");
|
|
|
205 |
|
|
|
206 |
|
|
|
207 |
Examples
|
|
|
208 |
========
|
|
|
209 |
|
|
|
210 |
Basic Use
|
|
|
211 |
---------
|
|
|
212 |
|
|
|
213 |
The following script demonstrates how a simple email message can be sent
|
|
|
214 |
using the Net_SMTP package::
|
|
|
215 |
|
|
|
216 |
require 'Net/SMTP.php';
|
|
|
217 |
|
|
|
218 |
$host = 'mail.example.com';
|
|
|
219 |
$from = 'user@example.com';
|
|
|
220 |
$rcpt = array('recipient1@example.com', 'recipient2@example.com');
|
|
|
221 |
$subj = "Subject: Test Message\n";
|
|
|
222 |
$body = "Body Line 1\nBody Line 2";
|
|
|
223 |
|
|
|
224 |
/* Create a new Net_SMTP object. */
|
|
|
225 |
if (! ($smtp = new Net_SMTP($host))) {
|
|
|
226 |
die("Unable to instantiate Net_SMTP object\n");
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
/* Connect to the SMTP server. */
|
|
|
230 |
if (PEAR::isError($e = $smtp->connect())) {
|
|
|
231 |
die($e->getMessage() . "\n");
|
|
|
232 |
}
|
|
|
233 |
|
|
|
234 |
/* Send the 'MAIL FROM:' SMTP command. */
|
|
|
235 |
if (PEAR::isError($smtp->mailFrom($from))) {
|
|
|
236 |
die("Unable to set sender to <$from>\n");
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
/* Address the message to each of the recipients. */
|
|
|
240 |
foreach ($rcpt as $to) {
|
|
|
241 |
if (PEAR::isError($res = $smtp->rcptTo($to))) {
|
|
|
242 |
die("Unable to add recipient <$to>: " . $res->getMessage() . "\n");
|
|
|
243 |
}
|
|
|
244 |
}
|
|
|
245 |
|
|
|
246 |
/* Set the body of the message. */
|
|
|
247 |
if (PEAR::isError($smtp->data($subj . "\r\n" . $body))) {
|
|
|
248 |
die("Unable to send data\n");
|
|
|
249 |
}
|
|
|
250 |
|
|
|
251 |
/* Disconnect from the SMTP server. */
|
|
|
252 |
$smtp->disconnect();
|
|
|
253 |
|
|
|
254 |
.. _PEAR_Error: http://pear.php.net/manual/en/core.pear.pear-error.php
|
|
|
255 |
.. _Net_Socket: http://pear.php.net/package/Net_Socket
|
|
|
256 |
.. _Auth_SASL: http://pear.php.net/package/Auth_SASL
|
|
|
257 |
|
|
|
258 |
.. vim: tabstop=4 shiftwidth=4 softtabstop=4 expandtab textwidth=78 ft=rst:
|