我尝试使用PHPMailer从localhost发送电子邮件(XAMPP安装)。
我的代码:
<?php
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer();
$body = "Message Body";
$mail->Mailer = "smtp";
$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 465;
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "myemailaddress@gmail.com"; // SMTP username
$mail->Password = "secret"; // SMTP password
//$mail->IsSMTP(); // telling the class to use SMTP
$mail->SetFrom('from@address.com', 'First Last');
$mail->Subject = "PHPMailer Test Subject via smtp, basic with authentication";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$mail->AddAddress('myemailaddress@gmail', 'First Last');
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
请注意,每当我在上面使用myemailaddress@gmail.com时,它都会替换为我的真实电子邮件地址,密码保密也是如此。
我收到以下错误
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
去上面提到的网站没有帮助。我试过以下:
SMTP Connect() failed. Message was not sent.Mailer error: SMTP Connect() failed