• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

Net/H03-May-2022-1,436675

examples/H03-May-2022-4026

tests/H03-May-2022-159130

LICENSEH A D19-Mar-20211.3 KiB2419

README.rstH A D19-Mar-20219.4 KiB299214

README.rst

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