2

I know this kind of question asked so many times in stackoverflow, but I looked at most of them and the problem still remains.

I'm using phpMailer to send mail from my site using a gmail account.
When I was using this in localhost, everything was fine and mails was sending correctly, But as long as I uploaded my site in remote server, problem revealed itself.

There's connection timeout(110) error when I'm using port 465 and smtp.gmail.com, when I'm trying to use port 587 and tls there's error too. Would you please let me know what's wrong?

The host php version is 5.4.

this is my code:

require_once ("../PHPMailer/class.phpmailer.php");

class mailSender
{

    protected $address;
    protected $username;
    protected $password;
    protected $cc;
    protected $bcc;

    protected $body;

    protected $mailer;


    public function __construct( $host, $port, $usn, $psw, $isSMTP = true )
    {

        $this->mailer = new PHPMailer(true);

        //$this->mailer->Host       = "mail.yourdomain.com"; // SMTP server
        $this->mailer->Host       = $host; //"smtp.gmail.com";      // sets GMAIL as the SMTP server
        $this->mailer->Port       = $port; //465;                   // set the SMTP port for the GMAIL server




        if( $isSMTP == true )
        {

            $this->mailer->IsSMTP();
            //$this->mailer->SMTPDebug  = 1;                     // enables SMTP debug information (for testing)
            $this->mailer->SMTPAuth   = true;                  // enable SMTP authentication
            $this->mailer->SMTPSecure = "ssl";                 // sets the prefix to the servier

        }

        $this->mailer->Username   = $usn; //"yourusername@gmail.com";  // GMAIL username
        $this->mailer->Password   = $psw; //"yourpassword";            // GMAIL password


        $this->mailer->CharSet = 'UTF-8';

        $this->mailer->IsHTML(true);

        //set High priority to prevent going to SPAM folder
        $this->mailer->Priority = 1;
        $this->mailer->AddCustomHeader("X-MSMail-Priority: High");
        $this->mailer->AddCustomHeader("Importance: High");


    }//function __construct





    public function send( $address, $senderName, $replyAddr, $replyName, $fromAddr, $fromName, $subject, $body, $attachment = false, $cc = false, $bcc = false )
    {


      try {

          //$this->mailer->AddReplyTo( $replyAddr, $replyName);
          $this->mailer->AddAddress($address, $senderName);
          $this->mailer->SetFrom($fromAddr, $fromName);
          $this->mailer->AddReplyTo($replyAddr, $replyName);
          $this->mailer->Subject = $subject; //'PHPMailer Test Subject via mail(), advanced';
          $this->mailer->AltBody = 'در صورتی که قادر به تماشای محتوای ای-میل نیستید از یک نمایشگر ای-میل تحت HTML استفاده کنید!'; // optional - MsgHTML will create an alternate automatically
          $this->mailer->MsgHTML($body);
              //$this->mailer->MsgHTML(file_get_contents('contents.html'));
              //$this->mailer->AddAttachment('images/phpmailer.gif');      // attachment
              //$this->mailer->AddAttachment('images/phpmailer_mini.gif'); // attachment
          $send = $this->mailer->Send();

          if( $send )
          {
              return true;

          }
          else
          {
              return false;

          }
      }//try send
      catch( Exception $e )
      {

          return false;

      }//catch

    }//function Send

}//class mailSender

Thanks in Advance

vahed
  • 161
  • 1
  • 2
  • 12
  • http://stackoverflow.com/questions/23782689/smtp-error-failed-to-connect-to-server-connection-timed-out-110-with-phpmail – Umair Ayub Aug 07 '15 at 12:35
  • Has your webserver been configured properly to allow phpmailer? – Crecket Aug 07 '15 at 12:35
  • @Crecket Would you please mention what kind of configure do you mean? – vahed Aug 07 '15 at 12:40
  • @Kiya what is your webserver right now? On xampp I usually have to configure my php.ini and sendmail.ini. [Here](http://php.net/manual/en/mail.configuration.php) is a example off the php settings you may need to configure – Crecket Aug 07 '15 at 13:02
  • As you will no doubt have seen in many of the answers on StackOverflow, you're using an old version of PHPMailer and have based your code on an old example. [Get the latest version](https://github.com/PHPMailer/PHPMailer) and [*read the docs*](https://github.com/PHPMailer/PHPMailer/wiki). When you've done all that, ask a question. – Synchro Aug 07 '15 at 13:27

1 Answers1

3

Might be your gmail acct is locked. Try the unlock captcha:

https://accounts.google.com/DisplayUnlockCaptcha.

Also check your account setting called Allow less secure apps here:

https://myaccount.google.com/security#connectedapps