如何使用PHP中的Swiftmailer库使用STARTTLS和自签名证书发送邮件


How to send mail with STARTTLS and self-signed certificate using Swiftmailer Library in PHP

我想使用SwiftMailer类库在PHP脚本中发送邮件。

邮件服务器提供STARTTLS命令并使用自签名证书。

问题是SwiftMailer发送邮件似乎有问题。

如果我像这样简单地使用coee来初始化传输:

$transport = Swift_SmtpTransport::newInstance("server.example.com", 25, "tls")
->setUsername("user@example.com")
->setPassword("example");

此错误显示为:

Uncaught Swift_TransportException: Unable to connect with TLS encryption

我已经在这里和其他地方搜索了。我在StackOverflow上找到了这个线程:使用STARTTLS和自签名证书的PHP-Swiftmailer

但是当我把上面提到的方法添加到上面的代码中时,就像这样:

->setStreamOptions(array('ssl' => array('allow_self_signed' => true, 'verify_peer' => false)));

完整的块看起来像这样:

 $transport = Swift_SmtpTransport::newInstance("server.example.com", 25, "tls")
->setUsername("user@example.com")
->setPassword("example")
->setStreamOptions(array('ssl' => array('allow_self_signed' => true, 'verify_peer' => false)));

我得到这个错误:

Call to undefined method setStreamOptions

所以看起来上面提到的方法被删除或重命名了。我在SwiftMailer文档中没有发现任何关于这一点的信息。网上只提到了几个修补库的解决方案。我没有检查这些方法,因为我不想修改库代码。

我的SwiftMailer版本是实际的5.4.2。有没有一种方法可以在不修补库的情况下解决这个问题?如果有必要,必须如何做到这一点。

希望有人能帮助我。

致以最诚挚的问候

附言:希望我做得很好,我的英语可以理解。如果有问题,请提问。

我来晚了,但试着升级到最新的swiftmailer版本,它会解决你的问题。我有5.4.1版本,并切换到5.4.6以使其工作