使用最新版本的PHPMailer有一种非常简单的方法。
require_once 'phpmailer/class.phpmailer.php';
require_once 'phpmailer/class.smtp.php';
$mail = new PHPMailer(true);
$mail->SMTPAuth = true;
$mail->Username = 'email@example.com';
$mail->Password = 'my_awesome_password';
$mail->Host = 'smtp.example.com';
$mail->Port = 465;
// This function returns TRUE if authentication
// was successful, or throws an exception otherwise
$validCredentials = $mail->SmtpConnect();
Run Code Online (Sandbox Code Playgroud)
您可能需要更改端口号并根据目标SMTP服务启用SSL。例如,Gmail 需要 SSL连接。
要启用SSL,请添加 $mail->SMTPSecure = 'ssl';
注意:PHPMailer 在失败时引发异常。例如,用户名/密码不匹配可能会引发异常。为避免错误消息遍及整个页面打印,请将函数包装在try / catch块中。
$validCredentials = false;
try {
$validCredentials = $mail->SmtpConnect();
}
catch(Exception $error) { /* Error handling ... */ }
Run Code Online (Sandbox Code Playgroud)
这并非不可能。
您可以自行对 SMTP 交互进行编程,并检查 SMTP 协议中是否确认凭据已被接受。对于这种方法,您必须与 SMTP 服务器进行套接字级通信(例如使用 [PHP Sockets] 服务1)。
要了解 SMTP 协议的工作原理,请尝试手动使用 Telnet 进行操作。
http://technet.microsoft.com/en-us/library/aa995718(v=exchg.65).aspx