我无法通过php 从我的Gmail帐户发送电子邮件 这是代码。我正在尝试使用smtp.gmail.com从gmail发送电子邮件,但它提供了错误
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require_once 'vendor/autoload.php';
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = "smtp.gmail.com"; //gmail SMTP server
$mail->SMTPAuth = true;
$mail->Username = 'mymail@gmail.com'; //username
$mail->Password = 'mypassword'; //password
$mail->SMTPSecure = 'ssl';
$mail->SMTPDebug = 2;
$mail->Port = 465; //smtp port
$mail->setFrom('mymail@gmail.com','Artisans Web');
$mail->addAddress('mymail@gmail.com', 'User Name');
// $mail->addAttachment(__DIR__ . '/attachment1.png');
// $mail->addAttachment(__DIR__ . '/attachment2.jpg');
$mail->IsHTML(true);
$mail->Subject = 'Email Subject';
$mail->Body = 'Email Body';
if (!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
这是错误:
2018-02-13 06:12:41 SMTP错误:无法连接到服务器:(0)SMTP连接()失败。无法发送https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting消息.Mailer错误:SMTP connect()失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
答案 0 :(得分:1)
您是否在Gmail帐户上启用了安全性较低的应用访问权限? https://support.google.com/accounts/answer/6010255?hl=en