The following From address failed Called Mail() wi

2019-07-29 10:06发布

问题:

Hello guys I get this error:

The following From address failed: username_from@gmail.com : Called Mail() without being connected

<?php
require("PHPMailer/class.phpmailer.php");

$msg = 'hello world!';

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = 'username@gmail.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'ssl';
$mail->From = 'username_from@gmail.com';
$mail->FromName = 'My name';
$mail->AddAddress('other_user@gmail.com', '');
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = 'hello';
$mail->Body = $msg;

if(!$mail->Send()){
    echo "Mailer Error: " . $mail->ErrorInfo;
}
else{
    echo "Message has been sent";
}
?>

How can I fix it? Thanks! I send email from localhost running XAMPP in PHP using Gmail mail server.

回答1:

Possible reason for error:

  • you have to edit your gmail account permissions. Trying enabling the 'Access for less secure apps' Security setting.

  • may be,you're using an old version of PHPMailer - update it.

  • Don't use ssl/465 for gmail; use tls/587. Take a look at the gmail example code provided with PHPMailer.

  • You don't need to call singleTo if you're only adding one address anyway.
  • Try and set Host to smtp.gmail.com
  • you can add $mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only so you can see some debug statements?