Php 有没有办法使用Zend Mail验证SMTP配置?
我一直在用PHP编写一个发送邮件的类,我决定使用Zend框架。此类使用用户的SMTP配置发送邮件。目前,我正在使用提供的用户凭据检查用户的SMTP配置,并向“虚拟”电子邮件地址发送一封“虚拟”电子邮件,捕获出错时可能抛出的Php 有没有办法使用Zend Mail验证SMTP配置?,php,magento,zend-mail,Php,Magento,Zend Mail,我一直在用PHP编写一个发送邮件的类,我决定使用Zend框架。此类使用用户的SMTP配置发送邮件。目前,我正在使用提供的用户凭据检查用户的SMTP配置,并向“虚拟”电子邮件地址发送一封“虚拟”电子邮件,捕获出错时可能抛出的ZendException类。这是一种可怕的方法,原因很多: SMTP用户因涉嫌“垃圾邮件”(GMail)最终被禁止 低效和耗时 用户邮箱中存在失败的电子邮件传递尝试 下面是我现在正在做的测试SMTP配置是否有效的示例: public function validSMTP
ZendException
类。这是一种可怕的方法,原因很多:
- SMTP用户因涉嫌“垃圾邮件”(GMail)最终被禁止
- 低效和耗时
- 用户邮箱中存在失败的电子邮件传递尝试
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中的内容。提前感谢所有回答的人 我在中没有看到任何东西表明有一种方法可以只测试SMTP身份验证而不实际尝试发送邮件。也许当你调用
setOptions
或setConnectionConfig
时,它会尝试登录,如果失败,就会抛出异常,但我怀疑不会
不要每次发送邮件时都执行验证检查,而是将配置数据和验证结果保存在文件中。然后在进行SMTP检查之前,读取文件并检查配置设置是否相同。如果是,只需返回保存的结果
这解决了问题列表中的前两项,并且只有在配置无效的情况下,他们才会在第一次收到回退电子邮件。我决定手动检查,我将在下面发布最终结果,以便将来对某人有所帮助。感谢@Barmar帮助我搜索文档,并得出Zend_Mail框架不支持此功能的结果。不幸的是,以下方法仅适用于SSL,因为TLS涉及在EHLO命令之后进行加密(以及更多)。如果配置有效,则返回true,否则返回字符串形式的无效原因
public function validSMTP () {
// Initialize client configuration, for this example
$config = [
"hostname" => "smtp.gmail.com",
"port" => 465,
"protocol" => "ssl",
"username" => "USERNAME",
"password" => "PASSWORD",
];
// Add the connection url based on protocol
$config ["url"] = $config ["protocol"] . "://" . $config ["hostname"];
// Open a new socket connection to the SMTP mail server
if ( !( $socket = fsockopen ( $config ["url"], $config ["port"], $en, $es, 15 ) ) ) {
// Could not establish connection to host
return "Connection failed, check hostname/port?";
}
// Make sure that the protocol is correct
if ( $this->status_match($socket, '220') === false ) {
// Probably a wrong protocol (SSL/TLS)
return "Connection failed, check protocol?";
}
// Send hello message to server
fwrite ( $socket, "EHLO " . $config ["hostname"] ."\r\n" );
if ( $this->status_match ( $socket, "250" ) === false ) {
// If Hello fails, say config is invalid
return "Invalid SMTP configuration";
}
// Request to login
fwrite ( $socket, "AUTH LOGIN\r\n" );
if ( $this->status_match ( $socket, "334" ) === false ) {
return "Invalid SMTP configuration";
}
// Send the username
fwrite ( $socket, base64_encode ( $config [ "username" ] ) . "\r\n" );
if ( $this->status_match ( $socket, "334" ) === false ) {
// If failed, warn that invalid username password was passed
return "Invalid username/password combination.";
}
// Send the password
fwrite ( $socket, base64_encode ( $config [ "password" ] ) . "\r\n" );
if ( $this->status_match ( $socket, "235" ) === false ) {
// If failed, warn that invalid username password was passed
return "Invalid username/password combination.";
}
// Close the socket connections
fclose ( $socket );
// Return true, if everything above passed
return true;
}
private function status_match ( $socket, $expected ) {
// Initialize the response string
$response = '';
// Get response until nothing but code is visible
while ( substr ( $response, 3, 1) != ' ' ) {
// Receive 250 bytes
if ( !( $response = fgets ( $socket, 256 ) ) ) {
// Break if nothing else to read
break;
}
}
// If the status code is not what was expected
if ( !( substr ( $response, 0, 3 ) == $expected ) ) {
// Return false
return false;
}
// Otherwise return true
return true;
}
我是通过以下例子得出这个结论的:嘿,你可能误解了我的问题。我正在尝试确定用户的SMTP配置是否有效。所以基本上我想用他们的凭据登录SMTP服务器,但不发送任何邮件。没有要检查的内容。您的脚本尝试发送邮件并检查是否成功。我建议你保留它,但是缓存结果,这样你就不必每次都这么做了。这很好,但我想完全避免。在Zend API中肯定有这样做的方法,对吗?如果没有,我将不得不求助于手动检查,方法是使用用户提供的配置打开一个套接字,然后传递EHLO和AUTH LOGIN命令。我很感激你的回答,我也会尝试整合这一点。“肯定有办法做到这一点”。读我的第一段。我扫描了Zend文档,没有看到任何提示。如果你看到了我没有看到的东西,那么你应该能够自己回答这个问题。很抱歉,我的意思是,这将是一个直观的功能,可以在框架内使用。我想我得手动检查了。感谢您的帮助和时间:)顺便说一句,您的配置选项与中的示例不同。getConfiguration方法是我的类中的自定义方法,它只返回一个与用户配置相关的数组。除此之外,我看不出什么是无效的。这对我很有效。也许是因为我在Magento中使用Zend_邮件而有所不同?