PHPMailer not working: Message could not be sent

37,013

Solution 1

Have you looked at and tried the info from this Q?

PHPMailer: SMTP Error: Could not connect to SMTP host

In particular, does this provide any additional info?

$mail->SMTPDebug = 1;

Solution 2

smtp.gmail.com requires that you use SSL and port 587 or 465.

See their configuration page: http://support.google.com/mail/bin/answer.py?hl=en&answer=13287

Solution 3

Are you running PHP on Windows? Then this might help:

http://www.devcha.com/2010/01/php-fsockopen-unable-to-connect-ssl.html

Solution 4

Got the same error and the problem was that i was trying to sent email from "boy333@in**" account pretending as "girl333@in**". I just changed

$mail->From = 'girl333@in**'

to username i was actually connecting. So I changed to:

$mail->From = 'boy333@in**'

The idea is that these two fields is with the same usernames.

$mail->Username   = "boy333";
$mail->From = 'boy333@in**';

Solution 5

<?php

require_once('class.phpmailer.php');
include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded


    $nameField = $_POST['name'];
    $emailField = $_POST['email'];
    $messageField = $_POST['message'];
    $phoneField = $_POST['contactno'];
    $cityField = $_POST['city'];

$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch

$mail->IsSMTP(); // telling the class to use SMTP

$body .= $nameField;

try {
     //$mail->Host       = "mail.gmail.com"; // SMTP server
      $mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
      $mail->SMTPAuth   = true;                  // enable SMTP authentication
      $mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
      $mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
      $mail->Port       = 465;   // set the SMTP port for the GMAIL server
      $mail->SMTPKeepAlive = true;
      $mail->Mailer = "smtp";
      $mail->Username   = "[email protected]";  // GMAIL username
      $mail->Password   = "********";            // GMAIL password
      $mail->AddAddress('[email protected]', 'abc');
      $mail->SetFrom('[email protected]', 'def');
      $mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
      $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
      $mail->MsgHTML($body);
      $mail->Send();
      echo "Message Sent OK</p>\n";
      header("location: ../test.html");
} catch (phpmailerException $e) {
      echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
      echo $e->getMessage(); //Boring error messages from anything else!
}

?>

Go to google settings and enable 'less secure' applications. It worked for me.

Share:
37,013
AnchovyLegend
Author by

AnchovyLegend

Updated on March 17, 2020

Comments

  • AnchovyLegend
    AnchovyLegend about 4 years

    I am trying to create a contact form on my website using PHPMailer. I am having some trouble setting it up. I am trying to use G-mail as my smtp host. I was wondering if anyone can help troubleshoot this?

    This is my mailer code:

    <?php
    require("class.phpmailer.php");
    require("class.smtp.php");
    
    $mail = new PHPMailer();
    
    $mail->IsSMTP();   
    $mail->SMTPAuth = true;     // turn on SMTP authentication      
    $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail        
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = 467;  
    
    $mail->Username = "[email protected]";  // SMTP username
    $mail->Password = "workingpassword"; // SMTP password
    
    $mail->From = "[email protected]";
    $mail->FromName = "Mailer";
    $mail->AddAddress("[email protected]", "Josh Adams");
    $mail->AddAddress("[email protected]");                  // name is optional
    $mail->AddReplyTo("[email protected]", "Information");
    
    $mail->WordWrap = 50;                                 // set word wrap to 50 characters
    
    
    // $mail->AddAttachment("/var/tmp/file.tar.gz");         // add attachments
       // $mail->AddAttachment("/tmp/image.jpg", "new.jpg");    // optional name
        $mail->IsHTML(true);                                  // set email format to HTML
    
    $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. <p>";
       echo "Mailer Error: " . $mail->ErrorInfo;
       exit;
    }
    
    echo "Message has been sent";
    ?>
    

    The error message:

    Message could not be sent.
    Mailer Error: The following From address failed: [email protected]