0

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

17
  • Usename probably doesn't need @gmail.com. Commented Feb 13, 2018 at 6:32
  • yes, thankyou for responce. Commented Feb 13, 2018 at 6:33
  • You should set SMTPOptions before you try to send the mail. Commented Feb 13, 2018 at 6:33
  • 1
    Did you try reading the web page in the error message? It has a section explaining that error message. Commented Feb 13, 2018 at 6:37
  • 1
    What web hosting service are you using? If it's GoDaddy, the web page has a section about them as well. Commented Feb 13, 2018 at 6:38

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for response, yes I Did
I found answer. that was PHPmailer have problem from github. i took from my friend its working. Thank you for support guys

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.