【发布时间】:2016-11-11 13:31:45
【问题描述】:
我一直在用 PHP 编写一个发送邮件的类,我决定使用 Zend 框架。此类使用用户的 SMTP 配置发送邮件。截至目前,我正在使用提供的用户凭据检查用户的 SMTP 配置,并向“虚拟”电子邮件地址发送“虚拟”电子邮件,并捕获可能引发错误的 ZendException 类。这是一种可怕的方法,原因有很多:
- SMTP 用户最终因涉嫌“垃圾邮件”(GMail) 而被禁止
- 效率低且耗时
- 在用户邮箱中尝试发送电子邮件失败
以下是我现在测试 SMTP 配置是否有效的示例:
public function validSMTP () {
// Get the user's SMTP configuration
$config = $this->getConfiguration ();
// Create a new Zend transport SMTP object
$transport = new Zend_Mail_Transport_Smtp ( $config ["hostname"], [
"auth" => "login",
"ssl" => $config ["protocol"],
"port" => $config ["port"],
"username" => $config ["from"],
"password" => $config ["password"]
]);
// Create a new message and send it to dummy email
$mail = new Zend_Mail ("UTF-8");
$mail->setBodyText ( "null" );
$mail->setFrom ( $config ["from"] );
$mail->addTo ( "dev@null.com" );
$mail->setSubject ( "Test" );
// Attempt to send the email
try {
// Send the email out
$mail->send ( $transport );
// If all is well, return true
return true;
}
// Catch all Zend exceptions
catch ( Zend_Exception $exception ) {
// Invalid configuration
return false;
}
}
所以我的问题是:有更好的方法吗? Zend_Mail 有这样的内置功能吗?我查看了所有内容,但找不到 Zend_Mail 内置的任何内容。提前感谢任何回答的人!
【问题讨论】:
-
顺便说一句,您的配置选项与docs.zendframework.com/zend-mail/transport/smtp-authentication/… 中的示例不同。
-
getConfiguration 方法是我类中的自定义方法,它只返回一个与用户配置相关的数组。除此之外,我看不出什么是无效的。它适用于我。也许是因为我在 Magento 中使用 Zend_Mail 而不同?