Thursday, 25 September 2014

PHP smtp mailer error- SMTP connect() failed.

I am getting error as like SMTP connect() failed. 
<?php
$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("someone@domain.com",$this->session->userdata('username'));  //email address that receives the response
                $mail->Subject    = $subject;
                $mail->Body       = $message;
                $mail->AltBody    = $message;
                $destino = "someone@domain.com"; // 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);

                }
?>
You need full email address in your connection configurations:
$mail->Username = 'username@gmail.com';
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;

0 comments:

Post a Comment