I have been working on a class in PHP to send out mail and I decided to use the Zend framework. This class sends out mail using a user's SMTP configuration. As of right now I am checking a user's SMTP configuration by using the supplied user credentials, and sending out a "dummy" email to a "dummy" email address, and catching the ZendException class that may be thrown on error. This is a horrible method, because of many reasons:
- The SMTP user get's banned eventually for suspected "Spamming" (GMail)
- Inefficient and time consuming
- Failed email delivery attempt is in user's mailbox
The following is an example of what I am doing right now to test if a SMTP configuration is valid:
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 ( "[email protected]" );
$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;
}
}
So my question is: Is there a better way of doing this? Does Zend_Mail have a built in feature like this? I have looked all over and couldn't find anything build-in into Zend_Mail. Thank you in advance to whoever answers!
I decided to check manually, I will post the end result below so it may help someone in the future. Thanks to @Barmar for helping me search through the documentation and coming to the result that the Zend_Mail framework doesn't support such a feature. The below methods only work for SSL unfortunately since TLS involves encrypting after the EHLO command (and much more). If the configuration is valid, then it returns true, otherwise it returns the reason it is invalid in string form.
I came up with this by using the following example: http://schoudhury.com/blog/articles/send-email-using-gmail-from-php-with-fsockopen/