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 = '[email protected]'; //username $mail->Password = 'mypassword'; //password $mail->SMTPSecure = 'ssl'; $mail->SMTPDebug = 2; $mail->Port = 465; //smtp port $mail->setFrom('[email protected]','Artisans Web'); $mail->addAddress('[email protected]', '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
@gmail.com.SMTPOptionsbefore you try to send the mail.