PHPMailer 如何用 SMTPOptions 为 SMTP TLS 连接配置自定义 SSL 连接参数 PHPMailer 如何用 SMTPOptions 为 SMTP TLS 连接配置自定义 SSL 连接参数【免费下载链接】PHPMailerThe classic email sending library for PHP项目地址: https://gitcode.com/GitHub_Trending/ph/PHPMailer当你用 PHPMailer 通过 SMTP 发信并启用 TLS465 端口的 SMTPS 或 587 端口的 STARTTLS时底层的 TLS 握手行为比如校验对端证书、指定 CA 证书文件、限制校验深度默认由 PHP 的流上下文决定。如果需要向这条连接传入自定义的 SSL 参数PHPMailer 提供了SMTPOptions属性它是一个数组在通过 SMTP 连接时会被原样传给stream_context_create()。仓库中的示例 examples/ssl_options.phps 展示了完整的用法本文按这个示例梳理出可执行的配置路径。SMTPOptions 在源码中的位置与作用在 src/PHPMailer.php 中该属性的定义是/** * Options array passed to stream_context_create when connecting via SMTP. * * var array */ public $SMTPOptions [];它的工作链路在源码中可以确认PHPMailer::smtpConnect()在未显式传入参数时会把实例上的$this-SMTPOptions作为 options 使用见 src/PHPMailer.phpSMTP::connect()接收这个 options 数组注释说明它是 “An array of options for stream_context_create()”见 src/SMTP.php实际建连时执行stream_context_create($options)再把生成的上下文交给stream_socket_client()打开host:port见 src/SMTP.php。因此SMTPOptions的数组结构必须与stream_context_create()兼容。changelog.md 记录该属性于 Version 5.2.10May 4th 2015加入“Expose stream_context_create options via new SMTPOptions property”所以 6.x 版本均可使用。两个前提条件需要注意示例文件说明 SMTP 依赖精确时间时区必须设置好没有 php.ini 权限时可用date_default_timezone_set()examples/ssl_options.phps。使用 STARTTLS 或 SMTPS 加密时PHPMailer 会检查 OpenSSL 扩展通过OPENSSL_ALGO_SHA256常量判断缺失时抛出 “extension missing openssl” 异常见 src/PHPMailer.php。另外SMTP::getSMTPConnection()中有一个回退分支当stream_socket_client不可用时改用fsockopen注释明确说它 “should work in more places, but is missing some features”——回退路径不会传入流上下文见 src/SMTP.php。也就是说SMTPOptions只在走stream_socket_client路径时生效。准备环境推荐用 Composer 安装README.mdcomposer require phpmailer/phpmailervendor目录和vendor/autoload.php由 Composer 生成不属于 PHPMailer 本身。5.2 分支已停止维护官方建议 PHP 5.5 使用 6.x 版本。配置 TLS 连接与 SMTPOptions以下代码整理自 examples/ssl_options.phps示例值主机、证书路径、账号密码需要替换为你自己的环境?php use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\SMTP; //SMTP needs accurate times, and the PHP time zone MUST be set date_default_timezone_set(Etc/UTC); require ../vendor/autoload.php; $mail new PHPMailer(); //Tell PHPMailer to use SMTP $mail-isSMTP(); //Enable SMTP debugging //SMTP::DEBUG_OFF off (for production use) //SMTP::DEBUG_CLIENT client messages //SMTP::DEBUG_SERVER client and server messages $mail-SMTPDebug SMTP::DEBUG_CONNECTION; //Set the hostname of the mail server示例值替换为你的 SMTP 主机 $mail-Host smtp.example.com; //Set the SMTP port number: // - 465 for SMTP with implicit TLS, a.k.a. RFC8314 SMTPS or // - 587 for SMTPSTARTTLS $mail-Port 465; //Set the encryption mechanism to use: // - SMTPS (implicit TLS on port 465) or // - STARTTLS (explicit TLS on port 587) $mail-SMTPSecure PHPMailer::ENCRYPTION_SMTPS; //Custom connection options //Note that these settings are INSECURE $mail-SMTPOptions array( ssl [ verify_peer true, verify_depth 3, allow_self_signed true, peer_name smtp.example.com, cafile /etc/ssl/ca_cert.pem, ], ); //Whether to use SMTP authentication $mail-SMTPAuth true; $mail-Username usernameexample.com; $mail-Password yourpassword; $mail-setFrom(fromexample.com, First Last); $mail-addAddress(whotoexample.com, John Doe); $mail-Subject PHPMailer SMTP options test; $mail-msgHTML(file_get_contents(contents.html), __DIR__); if (!$mail-send()) { echo Mailer Error: . $mail-ErrorInfo; } else { echo Message sent!; }关键点说明ssl键下的每个选项都是传给流上下文的 SSL 参数。示例中出现了verify_peer、verify_depth、allow_self_signed、peer_name、cafile五个选项其中peer_name与Host一致cafile指向一份 CA 证书文件示例值/etc/ssl/ca_cert.pem需要换成你实际存在的证书路径。示例中特意标注了警告这组设置含allow_self_signed true是 “INSECURE” 的原注释为 “Note that these settings are INSECURE”。生产环境是否保留这些值需要由你按邮件服务商的证书情况自行决定文档没有给出进一步的安全配置建议。如果服务端只开放 587 端口把Port改为 587、SMTPSecure改为 STARTTLS 对应值即可465 对应 SMTPSimplicit TLS两者二选一见 examples/ssl_options.phps 的注释。端口和加密方式也可在Host上用ssl:///tls://前缀或端口号单独指定smtpConnect()会对 Host 做解析见 src/PHPMailer.php。验证连接与发送结果验证方式分三层均出自仓库文档调试输出SMTPDebug控制调试级别——SMTP::DEBUG_OFF生产环境关闭、SMTP::DEBUG_CLIENT仅客户端消息、SMTP::DEBUG_SERVER客户端与服务端消息示例使用SMTP::DEBUG_CONNECTION查看连接过程见 examples/ssl_options.phps。连接成功判定SMTP::connect()会读取服务端的公告回复响应码为220时返回true554时会发送 QUIT其他非 220 响应则关闭连接并返回false见 src/SMTP.php。调试输出中可以看到Connection: opening to host:port, ... options...与SERVER - CLIENT: ...这类行。发送结果示例以send()的返回值判断——失败时输出Mailer Error: . $mail-ErrorInfo成功时输出Message sent!见 examples/ssl_options.phps。限制与注意事项SMTPOptions只在stream_socket_client可用时生效PHPMailer 回退到fsockopen的路径“missing some features”不传入流上下文src/SMTP.php。STARTTLS / SMTPS 依赖 OpenSSL 扩展缺失时抛异常而不是静默降级。示例中的整组ssl参数被原作者标注为不安全配置仅作为演示如何传参是否照抄取决于你的信任链需求。SMTPOptions的数组必须能被stream_context_create()接受src/PHPMailer.php 的 PHPDoc 明确其为 “Options array passed to stream_context_create when connecting via SMTP”。如果需要更多连接行为的细节可继续阅读 src/SMTP.php 中connect()的完整实现完整可运行示例见 examples/ssl_options.phps。【免费下载链接】PHPMailerThe classic email sending library for PHP项目地址: https://gitcode.com/GitHub_Trending/ph/PHPMailer创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考