2

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 = "validmail@gmail.com";  // SMTP username
$mail->Password = "workingpassword"; // SMTP password

$mail->From = "validmail@gmail.com";
$mail->FromName = "Mailer";
$mail->AddAddress("josh@example.net", "Josh Adams");
$mail->AddAddress("ellen@example.com");                  // name is optional
$mail->AddReplyTo("info@example.com", "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: validmail@gmail.com
AnchovyLegend
  • 12,139
  • 38
  • 147
  • 231
  • 2
    Hard to tell why it's not working. Have you tried `$mail->SMTPDebug = 1;` – Khôi Jun 11 '12 at 14:29
  • Yes I have. This is the error it displayed : SMTP -> ERROR: Failed to connect to server: Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP? (12640394) – AnchovyLegend Jun 11 '12 at 14:34

7 Answers7

8

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;
Community
  • 1
  • 1
technoTarek
  • 3,218
  • 2
  • 21
  • 25
  • Thanks for the response, dubugging definitely helped some. This is the error it the smtp debug code added to the screen: SMTP -> ERROR: Failed to connect to server: Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP? (12640394) – AnchovyLegend Jun 11 '12 at 14:29
  • 5
    Per the other Q, it sounds like you have to enable extension=php_openssl.dll. – technoTarek Jun 11 '12 at 14:38
3

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

Mike Mackintosh
  • 13,917
  • 6
  • 60
  • 87
1

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

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

Khôi
  • 2,133
  • 11
  • 10
1

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**';
Matiss
  • 5,079
  • 2
  • 27
  • 27
1
<?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   = "xxxxx@gmail.com";  // GMAIL username
      $mail->Password   = "********";            // GMAIL password
      $mail->AddAddress('sendto@gmail.com', 'abc');
      $mail->SetFrom('xxxxxx@gmail.com', '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.

dehrg
  • 1,721
  • 14
  • 17
0
ping smtp.gmail.com

For some reason, google redirects SMTP requests to gmail-smtp-msa.l.google.com

PING gmail-smtp-msa.l.google.com (74.125.133.108): 56 data bytes
64 bytes from 74.125.133.108: icmp_seq=0 ttl=43 time=72.636 ms
64 bytes from 74.125.133.108: icmp_seq=1 ttl=43 time=68.841 ms
64 bytes from 74.125.133.108: icmp_seq=2 ttl=43 time=68.500 ms

So you should put the final destination in your config, as PHPMailer does not follow redirections. Also you may try to send emails without SMTPSecure by keeping that field blank.

$mail->Host = 'gmail-smtp-msa.l.google.com';  
$mail->SMTPAuth = true;                            
$mail->Username = 'email';   
$mail->Password = 'password';    
$mail->SMTPSecure = '';   
$mail->Port = 25;  
Digelim
  • 98
  • 2
  • 9
0

If your email in gsuite check two steps: https://support.google.com/a/answer/6260879?hl=en https://accounts.google.com/DisplayUnlockCaptcha

Php Mail Settings; PHPMailer version 5.2.22 Check class.phpmailer.php,

  var $Host = 'smtp.gmail.com';
  var $Port = 465;
  var $Helo ='domain.net';
  public $SMTPSecure = 'ssl';
  public $SMTPAutoTLS = true;
  public $SMTPAuth = true;
  public $SMTPOptions = array();
  public $Username = 'gmail-username';//or domain credentials on google mail
  public $Password = 'gmail-password';
  public $Timeout = 10;
  public $SMTPDebug = true;

mail send php

<?php
require('PHPMailer/class.phpmailer.php');
require('PHPMailer/class.smtp.php');

$headers = "Content-Type: text/html; charset=UTF-8";    
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet="SET NAMES UTF8";
$mail->SMTPDebug  = 0;
$mail->CharSet="utf8";
$mail->Debugoutput = 'html';
$mail->host       = "smtp.gmail.com";
$mail->port       = 465;
$mail->smtpauth   = true;
$mail->smtpsecure = 'ssl';
$mail->username   = "gmail-username"; //or domain credentials on google mail
$mail->password   = "gmail-password";
$mail->SetFrom('from-email', 'from-name');
$mail->AddAddress('to-address', 'to-address');
$mail->Body = 'your-text'; //emailbody
if(!$mail->Send()) 
        {
            mysql_close();
        echo "<script>alert('Error: " . $mail->ErrorInfo."');</script>";
        } 
        else 
        {
        mysql_close();
        echo "<script>alert('success');</script>";
        }

?>

This is work for me. Hope it helps.