PHPMailer cannot connect to SMTP host

For the past 2 days, I have been trying to get the PHP script to send email, but it does not seem to work. First I tried the regular php mail function, then PHPMailer, and then Swiftmailer.

I tried to use my Gmail and SMTP accounts, as well as my ISP's email and SMTP account, none of which worked. I tried to ping both SMTP servers, and they worked fine, and telnet also answered correctly. On telnet smtp.gmail.com 587 he said 220 smtp.gmail.com ESMTP q125sm9630397wmd.19 - gsmtp. I tried port 465 and 587 and combinations with TLS and SSL. For gmail, I also edited my gmail account to make sure it allows the use of less secure devices, allow imap, etc.

I am running apache on my computer in Windows 7. I have redirected ports 465 and 587. I have already gone through the PHPMailer troubleshooting page. I also disabled the firewall for testing, but that didn't help.

I correctly edited the php.ini settings, and also made sure that; has been removed from extension = php_openssl.dll.

This is my PHPMailer code based on the PHPMailer example provided for Gmail (I edited SMTP, accounts and passwords):

require '/PHPMailer/PHPMailerAutoload.php'; $mail = new PHPMailer; $mail->SMTPDebug = 3; // Enable verbose debug output $mail->isSMTP(); // Set mailer to use SMTP $mail->Host = 'smtp.x.x'; // Specify main and backup SMTP servers $mail->SMTPAuth = true; // Enable SMTP authentication $mail->Username = ' x@x.x '; // SMTP username $mail->Password = 'password'; // SMTP password $mail->SMTPSecure = 'tls'; // Enable TLS encryption, 'ssl' also accepted $mail->Port = 587; // TCP port to connect to $mail->setFrom(' x@x.x ', 'Mailer'); $mail->addAddress(' x@x.x '); // Add a recipient $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()) { $checkup = 'Something went wrong'; echo 'Mailer Error: ' . $mail->ErrorInfo; } else { $checkup = 'Success'; } 

When I use this in the SMTP of my ISP with the email address of the ISP, I get this error:

 2016-05-06 12:27:04 Connection: opening to smtp.xx:587, timeout=300, options=array ( ) 2016-05-06 12:27:04 Connection: opened 2016-05-06 12:27:04 SERVER -> CLIENT: 220-smtp6.mnd.mail.iss.as9143.net ESMTP Fri, 06 May 2016 14:27:07 +0200 220-Sending spam or unsolicited commercial e-mail to this server is strictly 220-prohibited by our NO UBE / NO UCE policy. Abuse will be prosecuted and/or 220 charged per attempted recipient at international postal rates. 2016-05-06 12:27:04 CLIENT -> SERVER: EHLO localhost 2016-05-06 12:27:04 SERVER -> CLIENT: 250-smtp6.mnd.mail.iss.as9143.net Hello (MY IP ADDRESS, edited out for security) 250-SIZE 31457280 250-8BITMIME 250-PIPELINING 250-STARTTLS 250 HELP 2016-05-06 12:27:04 CLIENT -> SERVER: STARTTLS 2016-05-06 12:27:04 SERVER -> CLIENT: 220 TLS go ahead 2016-05-06 12:27:04 SMTP Error: Could not connect to SMTP host. 2016-05-06 12:27:04 CLIENT -> SERVER: QUIT 2016-05-06 12:27:04 SERVER -> CLIENT: MIA x     dqi7  =   &J  3ʚ   ă S _  Z $  NH ؏ |  m>[ z@  f g  G    .pAGD 8Q :  J   'L a8%  v (*YC  u W s    l #6F w        9 | \% !     B=:  /U  m  bc\ cڊ  4  d -U  seEZ PC  Q Ai'JㅇK U ]h   H v   i   cF M΂ x    }   V HR c !  ,'  *) $Oȕ EMƍ &B +   u9A R  j'[7  y    a     N     2016-05-06 12:27:04 SMTP ERROR: QUIT command failed: MIA x     dqi7  =   &J  3ʚ   ă S _  Z $  NH ؏ |  m>[ z@  f g  G    .pAGD 8Q :  J   'L a8%  v (*YC  u W s    l #6F w        9 | \% !     B=:  /U  m  bc\ cڊ  4  d -U  seEZ PC  Q Ai'JㅇK U ]h   H v   i   cF M΂ x    }   V HR c !  ,'  *) $Oȕ EMƍ &B +   u9A R  j'[7  y    a     N     2016-05-06 12:27:04 Connection: closed 2016-05-06 12:27:04 SMTP connect() failed. Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting 

As you can see, it connects, the SMTP server seems to recognize me by IP and says “forward”, and then suddenly it crashes. I searched everywhere and totally don’t understand how I can fix it.

When I use gmail SMTP + gmail account, it says this:

 2016-05-06 17:42:40 Connection: opening to smtp.gmail.com:587, timeout=300, options=array ( ) 2016-05-06 17:42:40 Connection: opened 2016-05-06 17:42:40 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP 8sm9564071wms.14 - gsmtp 2016-05-06 17:42:40 CLIENT -> SERVER: EHLO localhost 2016-05-06 17:42:40 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [MY IP ADDRESS] 250-SIZE 35882577 250-8BITMIME 250-STARTTLS 250-ENHANCEDSTATUSCODES 250-PIPELINING 250 SMTPUTF8 2016-05-06 17:42:40 CLIENT -> SERVER: STARTTLS 2016-05-06 17:42:40 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS 2016-05-06 17:42:40 SMTP Error: Could not connect to SMTP host. 2016-05-06 17:42:40 CLIENT -> SERVER: QUIT 2016-05-06 17:42:40 SERVER -> CLIENT: MIA  b8   4} 5KO q0$&  Br  Q   P  =   r$ %➽,  X " c 5ȭ D C b   B  @  T ?bڌ_ *܋    X1'T%_     QQ    y}]OO     8d R| $     b     [  ,  5 [* H    5 l   B    a    f  f   Pj   c@ >    vk+Cq r*  - 4   'W勱A''A S , w: q 1     .    * ',ȁ  iH T0 E /1W   | i4 { Gk J n,US F |   |L    n}Qp  2016-05-06 17:42:40 SMTP ERROR: QUIT command failed: MIA  b8   4} 5KO q0$&  Br  Q   P  =   r$ %➽,  X " c 5ȭ D C b   B  @  T ?bڌ_ *܋    X1'T%_     QQ    y}]OO     8d R| $     b     [  ,  5 [* H    5 l   B    a    f  f   Pj   c@ >    vk+Cq r*  - 4   'W勱A''A S , w: q 1     .    * ',ȁ  iH T0 E /1W   | i4 { Gk J n,US F |   |L    n}Qp  2016-05-06 17:42:40 Connection: closed 2016-05-06 17:42:40 SMTP connect() failed. Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting 

In debug mode 4, it says the same thing (this shows only part of the huge text wall that you get):

 CLIENT -> SERVER: STARTTLS 2016-05-06 17:55:10 SMTP -> get_lines(): $data is "" 2016-05-06 17:55:10 SMTP -> get_lines(): $str is "220 TLS go ahead " 2016-05-06 17:55:10 SERVER -> CLIENT: 220 TLS go ahead 2016-05-06 17:55:10 SMTP Error: Could not connect to SMTP host. 2016-05-06 17:55:10 CLIENT -> SERVER: QUIT 2016-05-06 17:55:10 SMTP -> get_lines(): $data is "" 2016-05-06 17:55:10 SMTP -> get_lines(): $str is "MIA " 2016-05-06 17:55:10 SMTP -> get_lines(): $data is "MIA " 2016-05-06 17:55:10 

He says that he is ready to start TLS, and then he says that the connection failed again. I read a lot of topics about this in stackoverflow, but none of the solutions and options suggested there worked. Does anyone have any ideas?

0
source share
2 answers

The same problem that I have with Gmail, and I solved it by downloading the cacert.pem certificate from https://curl.haxx.se/docs/caextract.html You should also write the php.ini file as follows:

extension = php_openssl.dll openssl.cafile = C: \ xampp \ php \ extras \ ssl \ cacert.pem

It must be activated in the GMail account on the shortcut: Access and security options: Allow access to less secure applications: ON

This solution is thanks to user matteobin, stackoverflow

+1
source

Here is a sample PHP Mailer code. Be sure to download the PHPMailer classes from https://github.com/PHPMailer/PHPMailer

To find your mail server,

If you are using a Windows operating system:

  1. Open a command prompt (CMD.exe).
  2. Type nslookup and press Enter.
  3. Type set type = MX and press enter.
  4. Enter a domain name and press Enter, for example: youtube.com
  5. The result is a list of host names that are configured for SMTP

If you are using Linux

  1. Open a command prompt.
  2. Type dig domain.name MX and press Enter, where domain.name is the domain for which you are trying to find the smtp server.
  3. If you do not receive any response, your cpanel may not have an SMTP configuration. Contact technical support for help.

Basically, smtp server names are something like smtp.yourdomain.com or mail.yourdomain.com.

Open a command prompt and try the following two commands:

  1. ping smtp.yourdomain.com
  2. ping mail.yourdomain.com

Most likely, you will receive a response from any of the above commands, and this may be your SMTP server.

If this does not work, open cpanel → go to your mail accounts → click on the settings account email settings -> there somewhere on the page you will get information about your SMTP server

It will be written like this:

  • Incoming server: mail.yourdomain.com
  • IMAP Port: ---
  • POP3 Port: ---
  • Outgoing server: mail.yourdomain.com
  • SMTP Port: ---

Here is the code.

 <?php // Import PHPMailer classes into the global namespace // These must be at the top of your script, not inside a function use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; require 'your_directory/php_mailer/Exception.php'; require 'your_directory/php_mailer/PHPMailer.php'; require 'your_directory/php_mailer/SMTP.php'; $to = ' your_email_here@gmail.com '; $name = 'my first name'; // Passing 'true' enables exceptions //$mail = new PHPMailer(TRUE); $mail = new PHPMailer(); try { //Set SMTP Options $mail->SMTPOptions = array( 'ssl' => array( 'verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true ) ); //Server settings // Enable verbose debug output $mail->SMTPDebug = 2; // Set mailer to use SMTP $mail->isSMTP(); // Specify main and backup SMTP servers $mail->Host = 'mail.your_domain.com;your_backup_smtp.your_domain.com.co.ke'; // Enable SMTP authentication $mail->SMTPAuth = true; // SMTP username (This is smtp sender email. Create one on cpanel eg noreply@your _domain.com) $mail->Username = ' sender_email@your _domain.com'; // SMTP password (This is that emails' password (The email you created earlier) ) $mail->Password = 'your_password'; // Enable TLS encryption, 'ssl' also accepted $mail->SMTPSecure = 'tls'; // TCP port to connect to. the port for TLS is 587, for SSL is 465 and non-secure is 25 $mail->Port = 25; //Recipients $mail->setFrom(' sender_email@your _domain.com', 'Company Name'); // Add a recipient $mail->addAddress(' recipient_address@gmail.com ', 'Recipient Name'); // Name is optional //$mail->addAddress(' another_email@example.com '); $mail->addReplyTo(' info@your _domain.com', 'Information Team'); //$mail->addCC(' cc@example.com '); //$mail->addBCC(' bcc@example.com '); //Attachments (Ensure you link to available attachments on your server to avoid errors) //$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments //$mail->addAttachment('/tmp/image.jpg', 'some_imaje.jpg'); // Optional name //Content $mail->isHTML(true); // Set email format to HTML $mail->Subject = 'Here is the subject'; $mail->Body = 'This is the HTML message body <b><i>in bold and italicized!</i></b>'; $mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; $mail->send(); echo 'Message has been sent'; } catch (Exception $e) { echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo; } 

?>

0
source

Source: https://habr.com/ru/post/1310258/


All Articles