M
M
Maxim2018-06-29 16:55:46
PHPMailer
Maxim, 2018-06-29 16:55:46

How to solve error when sending by SMTP method via PHP mailer?

I'm trying to set up mailing using the "SMTP" method through php mailer. Through AMS - everything works. What could be the problem?
Code:
<?php
/**
* This example shows making an SMTP connection with authentication.
*/
//Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC' );
require 'vendor/autoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Set the hostname of the mail server
$mail->Host = 'Server IP';
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 587;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = '[email protected]';
//Password to use for SMTP authentication
$mail->Password = 'password';
//Set who the message is to be sent from
$mail->setFrom('[email protected]', 'First Last');
$mail->addReplyTo('[email protected]', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('[email protected]', 'John Doe');
//Set the subject line
$mail->Subject = 'PHPMailer SMTP test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents('contents.html'), __DIR__);
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//send the message, check for errors
if (!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
An error appears:
2018-06-29 13:51:28 SERVER -> CLIENT: 220 mail.my domain.ru ESMTP Exim 4.90_1 Fri, 29 Jun 2018 16:51:28 +0300
2018-06-29 13:51 :28 CLIENT -> SERVER: EHLO mail.my domain.ru
2018-06-29 13:51:28 SERVER -> CLIENT: 250-mail.my domain.ru Hello my domain.ru [Server IP]250-SIZE 52428800250 -8BITMIME250-PIPELINING250-AUTH PLAIN LOGIN CRAM-MD5250-CHUNKING250-STARTTLS250 HELP
2018-06-29 13:51:28 CLIENT -> SERVER: STARTTLS
2018-06-29 13:51:28 SERVER -> CLIENT: 220 TLS go ahead
SMTP Error: Could not connect to SMTP host.
2018-06-29 13:51:28 CLIENT -> SERVER: QUIT
2018-06-29 13:51:28
2018-06-29 13:51:28
SMTP connect() failed.https://github.com/PHPMailer/PHPMailer/wiki/Troubl...
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubl...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
glem1337, 2019-10-03
@glem1337

Maybe late, but I had the same problem. Solution that worked for me:

$mail->SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    )
);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question