2

I am failing to send email in my application hosted on appfog i am using the following code which works fine on localhost but fail on appfog. JPhpMailer extend class.PhpMailer.php

                    $mailer = new JPhpMailer(true);
                    $mailer->IsSMTP();
                    $mailer->Mailer = "smtp";
                    //$mailer->SMTPSecure == 'tls'; 
                    $mailer->Host = 'ssl://smtp.gmail.com';
                    $mailer->Port = '465';
                    $mailer->SMTPAuth = true;
                    //$mailer->SMTPSecure = true; 
                    $mailer->Username = 'me@gmail.com';
                    $mailer->Password = 'zzzzzzz';
                    $mailer->SetFrom($to['from'], $to['from_name']); 
                    $mailer->AddAddress($to['to'],$to['to_name'] ); 
                    $mailer->Subject = $to['subject'];
                    $mailer->Body = $to['body'];
                    $mailer->Send();

here is the line that in phpMailer that fails to execute`if ($tls) { if (!$this->smtp->StartTLS()) { throw new phpmailerException($this->Lang('tls')); }

         //We must resend HELO after tls negotiation
        $this->smtp->Hello($hello);
      }

       $connection = true;
      if ($this->SMTPAuth) {
         if (!$this->smtp->Authenticate($this->Username, $this->Password)) {
         **strong text throw new phpmailerException($this->Lang('authenticate')); **            }
      }
     }
   $index++;
    if (!$connection) {
       throw new phpmailerException($this->Lang('connect_host'));
     }
Eliethesaiyan
  • 2,327
  • 1
  • 22
  • 35

6 Answers6

6

The code below is working for me :

require("phpmailer/class.phpmailer.php");

$mail = new PHPMailer();

$mail->IsSMTP();                                      // set mailer to use SMTP
$mail->SMTPAuth = true;     // turn on SMTP authentication
$mail->SMTPSecure = "tls";
$mail->Host = "smtp.gmail.com";  // specify main and backup server
$mail->Port = 587;
$mail->Username = "myemail@gmail.com";  // SMTP username
$mail->Password = "mypass"; // SMTP password

$mail->From = "myemail@gmail.com";
$mail->FromName = "myname";
$mail->AddAddress("myaddress@gmail.com", "myname");

$mail->WordWrap = 50;                                 // set word wrap to 50 characters
$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";
  • Sry port should be `$mail->Port = 587; or $mail->Port = 465;` for gmail. updated in my code. –  Jan 14 '13 at 06:54
  • Hey @Sumit,i found out that i had to have ssl installed on appfog(enabled in my plan) which i dont have by now...so switched back to use mail instead of SMTP!Sight! – Eliethesaiyan Jan 16 '13 at 11:25
4

I encountered the same problem. To get it working, I had to go to myaccount.google.com -> "connected apps & sites", and turn "Allow less secure apps" to "ON" (near the bottom of the page).

Hope it helps some one

enter image description here

Developer
  • 3,857
  • 4
  • 37
  • 47
1

After signing up to appfog, I was able to get PHPMailer working with the following.

I was unable to find JPHPMailer, although I suspect that isn't the cause of your issue but the fact that you were putting ssl://smtp.gmail.com as the host.

ini_set('display_errors', 1);
error_reporting(E_ALL);

include('class.phpmailer.php');
$mailer = new PHPMailer(true);
$mailer->IsSMTP();
$mailer->SMTPSecure = 'ssl';
$mailer->Host = 'smtp.gmail.com';
$mailer->Port = 465;
$mailer->SMTPAuth = true;
$mailer->Username = 'me@gmail.com';
$mailer->Password = 'password';
$mailer->SetFrom('me@gmail.com', 'Name'); 
$mailer->AddAddress('you@gmail.com'); 
$mailer->Subject = 'This is a test';
$mailer->Body = 'Test body';
$mailer->Send();

Hope this helps?

Gavin
  • 6,284
  • 5
  • 30
  • 38
  • thx Gavin...i just tried its still crashing on if(!$this->SmtpConnect().. i dont what is causing and i chcanged the mailer host to smtp.gmail.com and added SMPTSecure to ssl but i didnt change any other thing – Eliethesaiyan Jan 11 '13 at 11:50
  • Do you have `ini_set('display_errors', 1); error_reporting(E_ALL);` at the top of your script? It's possible that an exception is being thrown and you can see the error that is causing it. PHPMailer returned a few errors with authentication etc when I tried the first time. – Gavin Jan 11 '13 at 11:54
  • 1
    i did it and it gave me this error SMTP Error: Could not connect to SMTP host.in all the post i have seen they say this is because ssl is not enabled on your server(appfog),did you do anything to make it work @Gavin – Eliethesaiyan Jan 13 '13 at 03:18
  • @Spidaboy7 - Unfortunately not, all I done was created a new PHP instance and deployed the code above, with `class.phpmailer.php` and `class.smtp.php`. I did set it up on EU West I think. I can recreate it if you want? – Gavin Jan 14 '13 at 13:01
  • I've created a new instance (no configuration other than uploading three files) and it worked straight away. Do you have two factor authentication set on your gmail account? I do and I have to supply my application password and not my "normal" password. – Gavin Jan 14 '13 at 13:10
  • @Gavin..strange...do you have ssl enabled in you application?(using free plan by now)....i ve read many problems similar issue and most them were that ssl wasnt enabled on their host..so i gave up.in stead of SMTP i am using mail function of PHP – Eliethesaiyan Jan 16 '13 at 11:27
  • @Spidaboy7 - SSL is only available to paid solutions if I recall correctly. I simply signed up to Appfog to test my code on the same host that you were using. If you could let me know your configuration (i.e. AWS EU West) then I can replicate it step by step. Either way, seems using `Mail` is a suitable workaround for now :) – Gavin Jan 18 '13 at 07:43
1

Print your PHPMailer object and check PORT on object and you given PORT

echo "<pre>", print_r($mailer, true);
exit;
UWU_SANDUN
  • 1,123
  • 13
  • 19
0

Step 1:- Go to https://myaccount.google.com/security#signin then App passwords generate app password.

Step 2:- Paste that 16 digit password $mailer->Password

Arshid KV
  • 9,631
  • 3
  • 35
  • 36
0

In most cases you need to create a 2-Step Verification and sign in with an App password.

Small tip: you should carefully read phpmailer's debug message. There could be a proper link to an answer of the problem.

I had this one: https://support.google.com/mail/answer/7126229?visit_id=1-636190350518295662-3485238940&rd=2#cantsignin

Mark
  • 13
  • 3