wikia / net_smtp2
SMTP协议的实现
Requires
Requires (Dev)
Suggests
- pear/auth_sasl: Install optionally via your project's composer.json
This package is auto-updated.
Last update: 2024-08-27 21:40:37 UTC
README
1 Net_SMTP2 包
1.1 用户文档
目录
1.1.1 依赖项
1.1.1.1 《code>PEAR_Exception 类
The Net_SMTP2 package uses the PEAR_Exception object for all of its error handling.
1.1.1.2 《code>Net_Socket 包
The Net_Socket package is used as the basis for all network communications.
1.1.1.3 《code>Auth_SASL 包
The Auth_SASL package is an optional dependency. If it is available, the Net_SMTP2 package will be able to support the DIGEST-MD5 and CRAM-MD5 SMTP authentication methods. Otherwise, only the LOGIN and PLAIN methods will be available.
1.1.2 错误处理
All of the Net_SMTP2 class's public methods throw a PEAR_Exception object if an error occurs. The standard way to check for a PEAR_Error object is by using try/catch blocks
try { $smtp->connect(); } catch (PEAR_Exception $e) { die($e->getMessage()); }
1.1.3 SMTP 认证
The Net_SMTP2 package supports the SMTP authentication standard (as defined by RFC-2554). The Net_SMTP2 package supports the following authentication methods, in order of preference
1.1.3.1 DIGEST-MD5
The DIGEST-MD5 authentication method uses RSA Data Security Inc.'s MD5 Message Digest algorithm. It is considered the most secure method of SMTP authentication.
注意: DIGEST-MD5 认证方法仅在 AUTH_SASL 包可用时才受支持。
1.1.3.2 CRAM-MD5
The CRAM-MD5 authentication method has been superseded by the DIGEST-MD5 method in terms of security. It is provided here for compatibility with older SMTP servers that may not support the newer DIGEST-MD5 algorithm.
注意: CRAM-MD5 认证方法仅在 AUTH_SASL 包可用时才受支持。
1.1.3.3 LOGIN
The LOGIN authentication method encrypts the user's password using the Base64 encoding scheme. Because decrypting a Base64-encoded string is trivial, LOGIN is not considered a secure authentication method and should be avoided.
1.1.3.4 PLAIN
The PLAIN authentication method sends the user's password in plain text. This method of authentication is not secure and should be avoided.
1.1.4 安全连接
如果PHP中启用了安全套接字传输,则可以建立与远程SMTP服务器的安全连接
$smtp = new Net_SMTP2('ssl://mail.example.com', 465);
此示例使用ssl://
传输连接到mail.example.com
的465端口(一个常见的SMTPS端口)。
1.1.5 发送数据
消息数据通过data()
方法发送。数据可以是一个字符串或一个打开的文件资源。
如果提供字符串,它将通过数据引号系统传递,并作为单个块发送到套接字连接。所有这些操作都是基于内存的,因此发送大消息可能会导致内存使用量增加。
如果提供打开的文件资源,data()
方法将逐行从文件中读取消息数据。每个数据块将被引号标注并单独发送到套接字连接,从而降低整体数据发送操作的内存开销。
可以通过将可选的第二参数作为data()
传递来单独指定头部数据,从而与消息体数据分开。当使用打开的文件资源提供消息数据时,这特别有用,因为它允许在运行时动态构建头部字段(如主题:)。
$smtp->data($fp, "Subject: My Subject");
1.1.6 数据引用
默认情况下,所有传出字符串数据都根据SMTP标准进行引号标注。这意味着所有原生的Unix(\n
)和Mac(\r
)换行符都被转换为Internet标准的CRLF(\r\n
)换行符。此外,由于SMTP协议使用单个前导点(.
)来表示消息数据的结束,因此原始数据字符串中的单个前导点将被“加倍”(例如,“..
”)。
当涉及大量数据时,这些字符串转换可能非常昂贵。例如,Net_SMTP2包不了解MIME部分(它只是将MIME消息视为一个大的字符字符串),因此它无法在搜索可能需要引号标注的字符时跳过非文本附件。
因此,可以扩展Net_SMTP2类来实现自己的自定义引号标注例程。只需基于Net_SMTP2类创建一个新的类并重新实现quotedata()
方法即可。
require 'Net/SMTP2.php'; class Net_SMTP2_custom extends Net_SMTP2 { function quotedata($data) { /* Perform custom data quoting */ } }
请注意,$data
参数将以引用的形式传递给quotedata()
函数。这意味着可以直接操作$data
。这也避免了将大的$data
字符串在quotedata()
方法之间复制和粘贴的开销。
1.1.7 服务器响应
Net_SMTP2包保留服务器的最后响应以供进一步检查。getResponse()
方法返回一个包含服务器响应代码的整数和响应参数的字符串的2元组(两个元素数组)。
在成功连接后,可以通过getGreeting()
方法获取服务器的问候字符串。
1.1.8 调试
Net_SMTP2包包含内置的调试输出例程(默认情况下禁用)。调试输出必须通过setDebug()
方法显式启用。
$smtp->setDebug(true);
默认情况下,调试消息将通过标准输出流发送。如果您需要更多对输出的控制,您可以可选地安装自己的调试处理程序。
function debugHandler($smtp, $message) { echo "[$smtp->host] $message\n"; } $smtp->setDebug(true, "debugHandler");
1.1.9 示例
1.1.9.1 基本用法
以下脚本演示了如何使用Net_SMTP2包发送简单的电子邮件消息。
require 'Net/SMTP2.php'; $host = 'mail.example.com'; $from = 'user@example.com'; $rcpt = array('recipient1@example.com', 'recipient2@example.com'); $subj = "Subject: Test Message\n"; $body = "Body Line 1\nBody Line 2"; /* Create a new Net_SMTP2 object. */ if (! ($smtp = new Net_SMTP2($host))) { die("Unable to instantiate Net_SMTP2 object\n"); } /* Connect to the SMTP server. */ try { $smtp->connect(); } catch (PEAR_Exception $e) { die($e->getMessage() . "\n"); } /* Send the 'MAIL FROM:' SMTP command. */ try { $smtp->mailFrom($from); } catch (PEAR_Exception $e) { die("Unable to set sender to <$from>\n"); } /* Address the message to each of the recipients. */ try { foreach ($rcpt as $to) { $smtp->rcptTo($to); } } catch (PEAR_Exception $e) { die("Unable to add recipient <$to>: " . $e->getMessage() . "\n"); } /* Set the body of the message. */ try { $smtp->data($subj . "\r\n" . $body); } catch (PEAR_Exception $e) { die("Unable to send data\n"); } /* Disconnect from the SMTP server. */ $smtp->disconnect();