odbhut.shei.chhele
odbhut.shei.chhele

Reputation: 6224

Cannot send email using PHPMailer

I am trying to send an email using phpmailer. This is the code that I have written.

    $mail = new PHPMailer;

    $mail->isSMTP();
    $mail->Host = 'smtp.gmail.com';
    $mail->SMTPAuth = true;
    $mail->Username = '[email protected]';
    $mail->Password = '*********';
    $mail->Port = 25;

    $mail->From = '[email protected]';
    $mail->FromName = 'Shamir Towsif';
    $mail->addAddress('[email protected]', 'Shamir Towsif');
    $mail->addReplyTo('[email protected]', 'Information');

    $mail->isHTML(true);

    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    if(!$mail->send()) {
        echo "Message could not be sent.\n";
        echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
        echo 'Message has been sent';
    }

Here is the error that I am getting.

Message could not be sent. Mailer Error: SMTP connect() failed.

What am I doing wrong?

Upvotes: 0

Views: 2651

Answers (3)

Vasant Chhetri
Vasant Chhetri

Reputation: 11

Title: Sending Email From Server Using PhpMailer & Gmail

Here's how I solved a similar issue:

  1. Download PHPMAILER from github (https://github.com/PHPMailer/PHPMailer) to your computer.

  2. Upload it to your server as .zip file, then extract it by clicking the extract icon. Rename the folder to 'phpmailer' or any name you wish.

  3. Inside the mail sending .php file put the following codes:

    require 'phpmailer/src/Exception.php'; require 'phpmailer/src/PHPMailer.php'; require 'phpmailer/src/SMTP.php'; $mail = new PHPMailer\PHPMailer\PHPMailer(); $mail->isSMTP(); $mail->SMTPDebug = 2; $mail->Host = 'smtp.gmail.com'; $mail->Port = 587; $mail->SMTPSecure = 'tls'; $mail->SMTPAuth = true; $mail->Username = "[email protected]"; $mail->Password = "password"; $mail->setFrom('[email protected]', 'Name'); $mail->addAddress('[email protected]', 'Name'); $mail->Subject = 'Subject'; $mail->Body = 'This is a plain-text message body'; if (!$mail->send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { echo "Message sent!"; }

  4. Save the php file.

  5. Open your browser and sign in to your gmail(google) account that you used in the php script.

  6. Go to this link: https://support.google.com/accounts/answer/6009563

  7. Expand Your device or app might not support Google’s security standards.

  8. Click Less secure apps.

  9. Toggle Allow less secure apps: to ON (if it is OFF).

  10. Expand 2-Step Verification is not supported by the app

  11. Click the link: https://accounts.google.com/DisplayUnlockCaptcha

  12. Click Continue

  13. Open the email sending php file in the browser.

  14. Now the email will be sent.

This technique worked for me. I hope it'll work for you guys as well.

Happy Coding :-)

Upvotes: 1

Lynton Black
Lynton Black

Reputation: 179

Solved an almost identical problem, by adding these lines to the standard PHPMailer configuration. Works like a charm.

$mail->SMTPKeepAlive = true;   
$mail->Mailer = “smtp”; // don't change the quotes!

All of the standard settings for SMTP using TLS and default settings for ports etc were being followed. Eventually, I came across this code (from Simon Chen) while researching a solution here, https://webolio.wordpress.com/2008/03/02/phpmailer-and-smtp-on-1and1-shared-hosting/#comment-89

Upvotes: 0

Emerson Jair
Emerson Jair

Reputation: 128

I am facing a similar problem, but I think you should try adding this to your code:

$mail->Port = 587;
$mail->SMTPSecure = 'tls';

This is PHPMailer recommended settings for GMail, you can see an example in their Github page .

Upvotes: 2

Related Questions