Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8855695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:11:42+00:00 2026-06-14T14:11:42+00:00

I am trying to use PHPMailer to send a gmail email. I followed this

  • 0

I am trying to use PHPMailer to send a gmail email. I followed this post

In order to do this, I set up a function shown below:

function sendEmail($email, $name) {
    $mail = new PHPMailer();
    $mail->IsSMTP(); // send via SMTP
    //IsSMTP(); // send via SMTP I commented it cos it gives an error
    $mail->SMTPAuth = true; // turn on SMTP authentication
    $mail->Username = 'email@gmail.com'; // Changed my email
    $mail->Password = "password";// Changed my password
    $mail->From = 'email@gmail.com';
    $mail->FromName = 'FROM NAME';


    $mail->AddAddress($email);

    $mail->IsHTML(true); // send as HTML
    $mail->Subject = "Subject";
    $mail->Body = "Body";

    if (!$mail->Send()) {
        return false;
    } else {
        return true;
    }
}

Unfortunately, it keeps on returning false. Can you please tell me whats wrong with the code?

Edit: The error which I am getting is shown below:

SMTP -> ERROR: Failed to connect to server: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

SMTP Error: Could not connect to SMTP host.

UPDATED CODE:

 $Mail = new PHPMailer();
    $Mail->IsSMTP(); // Use SMTP
    $Mail->Host = "smtp.gmail.com"; // Sets SMTP server
    $Mail->SMTPDebug = 2; // 2 to enable SMTP debug information 
    $Mail->SMTPAuth = TRUE; // enable SMTP authentication
    $Mail->SMTPSecure = "tls"; //Secure conection
    $Mail->Port = 587; // set the SMTP port
    $Mail->Username = EMAIL; // SMTP account username
    $Mail->Password = PASS; // SMTP account password
    $Mail->Priority = 1; // Highest priority - Email priority (1 = High, 3 = Normal, 5 = low)
    $Mail->CharSet = 'UTF-8';
    $Mail->Encoding = '8bit';
    $Mail->Subject = 'SUB';
    $Mail->ContentType = 'text/html; charset=utf-8\r\n';
    $Mail->From = EMAIL;
    $Mail->FromName = 'FROM NAME';
    $Mail->WordWrap = 900; // RFC 2822 Compliant for Max 998 characters per line

    $Mail->AddAddress($email); // To:
    $Mail->isHTML(TRUE);
    $Mail->Body = "Hi";
    $Mail->AltBody = "Hi";
    $Mail->Send();
    $Mail->SmtpClose();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:11:43+00:00Added an answer on June 14, 2026 at 2:11 pm

    Here is a working example:

    <?php
    function SendMail( $ToEmail, $MessageHTML, $MessageTEXT ) {
      require_once ( 'class.phpmailer.php' ); // Add the path as appropriate
      $Mail = new PHPMailer();
      $Mail->IsSMTP(); // Use SMTP
      $Mail->Host        = "smtp.gmail.com"; // Sets SMTP server
      $Mail->SMTPDebug   = 2; // 2 to enable SMTP debug information
      $Mail->SMTPAuth    = TRUE; // enable SMTP authentication
      $Mail->SMTPSecure  = "tls"; //Secure conection
      $Mail->Port        = 587; // set the SMTP port
      $Mail->Username    = 'MyGmail@gmail.com'; // SMTP account username
      $Mail->Password    = 'MyGmailPassword'; // SMTP account password
      $Mail->Priority    = 1; // Highest priority - Email priority (1 = High, 3 = Normal, 5 = low)
      $Mail->CharSet     = 'UTF-8';
      $Mail->Encoding    = '8bit';
      $Mail->Subject     = 'Test Email Using Gmail';
      $Mail->ContentType = 'text/html; charset=utf-8\r\n';
      $Mail->From        = 'MyGmail@gmail.com';
      $Mail->FromName    = 'GMail Test';
      $Mail->WordWrap    = 900; // RFC 2822 Compliant for Max 998 characters per line
    
      $Mail->AddAddress( $ToEmail ); // To:
      $Mail->isHTML( TRUE );
      $Mail->Body    = $MessageHTML;
      $Mail->AltBody = $MessageTEXT;
      $Mail->Send();
      $Mail->SmtpClose();
    
      if ( $Mail->IsError() ) { // ADDED - This error checking was missing
        return FALSE;
      }
      else {
        return TRUE;
      }
    }
    
    $ToEmail = 'Name@example.com';
    $ToName  = 'Name';
    
    $Send = SendMail( $ToEmail, $MessageHTML, $MessageTEXT );
    if ( $Send ) {
      echo "<h2> Sent OK</h2>";
    }
    else {
      echo "<h2> ERROR</h2>";
    }
    die;
    ?>
    

    I tried this script and had no problem sending several messages.

    UPDATED:

    This is the typical response from Gmail on success:

    SMTP -> FROM SERVER:220 mx.google.com ESMTP 20sm6345523qek.6
    SMTP -> FROM SERVER: 250-mx.google.com at your service, [181.155.13.39]
                         250-SIZE 35882577
                         250-8BITMIME
                         250-STARTTLS
                         250 ENHANCEDSTATUSCODES
    SMTP -> FROM SERVER:220 2.0.0 Ready to start TLS
    SMTP -> FROM SERVER: 250-mx.google.com at your service, [181.155.13.39]
                         250-SIZE 35882577
                         250-8BITMIME
                         250-AUTH LOGIN PLAIN XOAUTH XOAUTH2
                         250 ENHANCEDSTATUSCODES
    SMTP -> FROM SERVER:250 2.1.0 OK 20sm6345523qek.6
    SMTP -> FROM SERVER:250 2.1.5 OK 20sm6345523qek.6
    SMTP -> FROM SERVER:354 Go ahead 20sm6345523qek.6
    SMTP -> FROM SERVER:250 2.0.0 OK 1353474062 20sm6345523qek.6
    SMTP -> FROM SERVER:221 2.0.0 closing connection 20sm6345523qek.6
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am trying to use phpMailer to send confirmation messages to users via email.
I am trying to use phpmailer to send out a newsletter, but I keep
Hello I'm trying to use PHPMailer to send emails via SMTP. So, on my
I am trying to send email through gmail using PHPMailer_V5.1. Getting the following error,
I am trying to send an email from PHP using PHPmailer() . I have
I am trying use this example http://www.sajithmr.me/jrecorder/example2.html for recording audio and send it to
I was trying use a set of filter functions to run the appropriate routine,
I am need paint my image. I'm trying use JQuery in here this link:
Resolved: After installing PHPMailer it now works. I am trying to use MIME E-mail
I am trying to send mail in PHP . I use same code for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.