SMTP connection failed

SMTP connect() failed. then, this is mostly because of the firewall rules on their infrastructure which explicitly blocks the outgoing SMTP connection to ports 25, 587 and 465 to all external servers.

SMTP connection failed

A) Solution:-

How to fix it?

To send and receive emails via PHPMailer, both the sending and receiving servers must accept connections.

  1. Modify the server’s firewall rules to permit outbound connections on ports such as 465.
  2. Change the server’s SMTP restrictions. Then, add the specified website user to the list of users who are authorized to initiate outbound SMTP connections.
  3. Edit PHPMailer’s configuration settings, such as host and port.
  4. Correct DNS resolution for the mail server.

Email authentication is a good thing, but it can also be a pain when you cannot access your email account because of the “smtp error: failed to connect to server” problem.

B) solution:-

To install:

  1. Download PHPMailer
  2. Add it to your project (i put it on root)
  3. Add Autoload class to your Script.
  4. Rest of code is below

            require "PHPMailer/PHPMailerAutoload.php";
            $mail = new PHPMailer;
            $mail->SMTPOptions = array(
                'ssl' => array(
                    'verify_peer' => false,
                    'verify_peer_name' => false,
                    'allow_self_signed' => true
                )
            );
            //$mail->SMTPDebug = 2;                                 // Enable verbose debug output
            $mail->isSMTP();                                      // Set mailer to use SMTP
            $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
            $mail->SMTPAuth = true;                               // Enable SMTP authentication
            $mail->Username = 'example@gmail.com';                 // SMTP username
            $mail->Password = 'securepass';                           // SMTP password
            $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
            $mail->Port = 465;                                    // TCP port to connect to
            //Recipients
            $mail->setFrom('example@gmail.com', "Mailer");
            $mail->addAddress("example@gmail.com","receiver Name");  
            $mail->isHTML(true);                                  
            $mail->Subject = "Subject";
            $mail->Body    = "Body";
            $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
               if( $mail->send()){
                return array("msg"=>msg("success","Email has been sent.<br>"));
                } else {
                    return array("msg"=>msg("error","Email can't send.Try Again<br>"));
                }


Click Here to Visit

What's Your Reaction?

like
0
dislike
0
love
0
funny
0
angry
0
sad
0
wow
0