简体   繁体   中英

Trying to send email from gmail using smtp.gmail.com

I can't send Email from my gmail account by php Here is code. I am trying to send email from gmail using smtp.gmail.com, but it's giving error

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
    )
);

This is the error:

2018-02-13 06:12:41 SMTP ERROR: Failed to connect to server: (0) SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

Have you enabled less secure app access on your Gmail account? https://support.google.com/accounts/answer/6010255?hl=en

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM