首页 文章

尝试使用smtp.gmail.com从gmail发送电子邮件

提问于
浏览
0

I can't send Email from my gmail account by php 这是代码 . 我正在尝试使用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

1 回答

相关问题