请注意,我使用的是谷歌应用帐户,而不是Gmail帐户.我正在尝试使用我的谷歌应用程序帐户使用PHP发送电子邮件.我可以使用端口587主机smtp.googlemail.com和启用SSL在.net应用程序中发送电子邮件.用户名是我的完整电子邮件地址.
require_once('PHPMailer_v5.1\class.PHPmailer.PHP');
try {
$mail = new PHPMailer();
$mail->Mailer = 'smtp';
$mail->SMTPSecure = 'tls';
$mail->Host = $host;
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = $from;
$mail->Password = $password;
$mail->AddAddress($to, $to_name);
$mail->From = $from;
$mail->FromName = $from_name;
$mail->Subject = $subject;
$mail->MsgHTML($body);
$mail->IsHTML(true);
$mail->Send();
} catch (PHPmailerException $e) {
echo $e->errorMessage();
} catch (Exception $e) {
echo $e->getMessage();
}
无法让这个工作,但我尝试了几种不同的变化.
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
// Error: Could not connect to SMTP host. This is expected as this isn't supported anymore.
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
// Takes forever, then I get "this stream does not support SSL/crypto PHPMailer_v5.1\class.smtp.PHP"
我不在乎如何,但我需要在这里使用gmail发送电子邮件.它可以与这个库或不同的库.
解决方法:
这是我们在网站上其他地方使用的,它工作正常.
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled required for Gmail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = GUSER;
$mail->Password = GPWD;
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
return false;
} else {
$error = 'Message sent!';
return true;
}
另外编辑 – 我们已经离开PHPMailer并开始使用SwiftMailer,它也适用于gmail(http://www.swiftmailer.org/wikidocs/v3/connections/smtp)