SMTP connect() failed.PHPmailer

15,995

Solution 1

You need full email address in your connection configurations:

$mail->Username = '[email protected]';

And if use TLS security protocol you need use:

$mail->SMTPSecure = "tls";
$mail-> Port = 587;
...

Or to SSL (that is deprecated status):

$mail->SMTPSecure = "ssl";
$mail-> Port = 465;
...

Solution 2

Check your php.ini file, on it, if you have this:

;extension=php_openssl.dll

change it to

extension=php_openssl.dll
Share:
15,995
Emmanuel Mariki
Author by

Emmanuel Mariki

Updated on June 05, 2022

Comments

  • Emmanuel Mariki
    Emmanuel Mariki almost 2 years

    I have been trying to send email from my local host using PHPMailer but i cant fix this error

    SMTP connect() failed.

    I know there are suggestion for this kind of error but non i have tried seems to work for me. Here are my settings

    $mail = new PHPMailer();
                    $mail->IsSMTP(); // we are going to use SMTP
                    $mail->Host       = 'smtp.gmail.com';      // setting GMail as our SMTP server
                    $mail->SMTPAuth   = true; // enabled SMTP authentication
                    $mail->Username   = 'mygamil';  // user email address
                    $mail->Password   = "mypassword";            // password in GMail
                    $mail->SMTPSecure = "tls";  // prefix for secure protocol to connect to the server
                    $mail->SetFrom($this->session->userdata('email'), $this->session->userdata('username'));  //Who is sending the email
                    $mail->AddReplyTo("[email protected]",$this->session->userdata('username'));  //email address that receives the response
                    $mail->Subject    = $subject;
                    $mail->Body       = $message;
                    $mail->AltBody    = $message;
                    $destino = "[email protected]"; // Who is addressed the email to
                    $mail->AddAddress($destino, "Sender name");
                    $mail->AddAttachment($file_path);      // some attached files/
    
                    if($mail->Send() ==TRUE){
    
                        redirect('app/send/'.$this->uri->segment(3).'/succeeded ');
    
                    }
                    else
                    {
                      //show an error email failed erroers 
                        $email_error = array('email_error'=>$mail->ErrorInfo);
    
                    }
    

    I have tried with port 587 and 465 none of them seems to work but when i telnet to either of the ports i get connections without error

    I have openssll.dill enable in my php.ini

    i have tried this settings both in MAMP in my mac and in Ubuntu 12.04 and still i get the same error

    Any input would be appreciated thanks in advance

  • Steven Koch
    Steven Koch almost 10 years
    Hi, maybe your problem is not in your code. Try test generally access. stackoverflow.com/a/2226759/2744184
  • Steven Koch
    Steven Koch almost 10 years
    try this solution: stackoverflow.com/a/18290256/2744184 Add open_ssl library.
  • Zein Miftah
    Zein Miftah over 9 years
    I check that with php -i | grep ssl, there is OpenSSL enabled, but i got the same error