skip to Main Content

I’ve created a contact form using PHP and am using PHPmailer class for sending mail using SMTP gmail settings. The form is working fine for other mail ids, but when I tried to change the settings to send mail via SMTP gmail, am getting error message as "SMTP Error: Could not connect to SMTP host. and SMTP -> ERROR: Failed to connect to server: Connection refused (111)".. Below is the code am using…

require 'PHPMailer/class.phpmailer.php';
 $mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'mmuxhssfdrqnsowp'; // Use the App Password you generated
$mail->SMTPSecure = 'tls';
$mail->From = $_POST['email'];
$mail->FromName = $_POST['name'];
$mail->AddAddress('[email protected]', 'HR');
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->AddAttachment($path);
$mail->Subject = 'Application for Programmer Registration';
$mail->Body = $message;
$mail->SMTPDebug = 2; 

Is there any mismatch in the settings to send mail using SMTP Gmail.

2

Answers


  1. Try with below code.

    require_once 'PHPMailer/class-phpmailer.php';
    $mail = new PHPMailer();
    
    $options["ssl"] = array(
        "verify_peer" => false,
        "verify_peer_name" => false,
        "allow_self_signed" => true,
    );
    $mail->SMTPDebug = false;
    $mail->isSMTP();
    $mail->Host = 'tls://smtp.gmail.com'; // This line was missing.
    $mail->SMTPAuth = true;
    $mail->Username = '[email protected]';
    $mail->Password = 'mmuxhssfdrqnsowp';
    $mail->SMTPSecure = 'tls';
    $mail->Port = '587';
    $mail->smtpConnect($options); // try with this line.
    $mail->setFrom($_POST['email'], $_POST['name']);
    $mail->AddAddress('[email protected]');
    $mail->isHTML(true);
    $mail->Subject = 'Application for Programmer Registration';
    $mail->Body = $message;
    if (!$mail->send()) {
        $finalResponse = 'Message could not be sent. Mailer Error: ' . $mail->ErrorInfo;
    } else {
        $finalResponse = 'success';
    }
    
    Login or Signup to reply.
  2. @venkat I can’t post comments, that is why I’m posting this as answer. Is your code working in local? If it is working, then as you mentioned please contact the go-daddy customer support. I have faced the similar kind of issue one of projects. They enable some setting and the mail started working.

    For TLS encryption the port number is 587, For SSl encryption the port number is 465

    For better understanding please check the link https://kinsta.com/blog/gmail-smtp-server/

    I’m using the below code, it is working for me.

    require("./PHPMailer-master/src/PHPMailer.php");
    require("./PHPMailer-master/src/SMTP.php");
    $mail = new PHPMailerPHPMailerPHPMailer();
    $mail->IsSMTP(); // enable SMTP
    $mail->SMTPDebug = 0; // debugging: 1 = errors and messages, 2 = messages only
    $mail->SMTPAuth = true; // authentication enabled
    $mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for Gmail
    $mail->Host = "smtp.gmail.com";
    $mail->Port = 587; // or 587
    $mail->IsHTML(true);
    $mail->Username = "[email protected]";
    $mail->Password = "mmuxhssfdrqnsowp";
    $mail->SetFrom("[email protected]");
    $mail->Subject = "Application for Programmer Registration";
    $mail->Body = $message;
    $mail->AddAddress("[email protected]", "HR");
    $file_to_attach = "../test-project/invite.ics";
    $mail->AddAttachment( $path , 'filename' );
    
    
    $headers = "From: Sendern";
    $headers .= 'Content-Type:text/calendar; Content-Disposition: inline; charset=utf-8;rn';
    $headers .= "Content-Type: text/plain;charset="utf-8"rn"; #EDIT: TYPO
    
    if (!$mail->Send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
        echo "Message has been sent";
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search