Cant send mails with PHPMailer


Jul 31, 2017
cPanel Access Level
Root Administrator
I can not send mails (I want to send them directly from Gmail, not with the mail () function), I'm trying the PHPMailer library and when I run it from a URL It does not work, it disconnects me from the server in the middle of the connection. If I do from SSH, with the root user, all right, send the mails correctly.
The option: whm > security center > SMTP Restrictions is Disable.
Now if I run from the browser that same, it does not work:
In: /var/log/apache2/error_log does not register errors.

And the PHP script returns:
    2017-07-30 21:46:54 SERVER -> CLIENT: ESMTP Exim 4.89 # 1 Sun, 30 Jul 2017 17:46:54 -0400 220-We do not authorize the use of this system To transport unsolicited, 220 and / or bulk email.
    2017-07-30 21:46:54 CLIENT -> SERVER: EHLO
    2017-07-30 21:46:54 SERVER -> CLIENT: Hello [] 250-SIZE 52428800250-8BITMIME250-PIPELINING250-AUTH PLAIN LOGIN250-STARTTLS250 HELP
    2017-07-30 21:46:54 CLIENT -> SERVER: STARTTLS
    2017-07-30 21:46:54 SERVER -> CLIENT: 220 TLS go ahead
    SMTP Error: Could not connect to SMTP host.
    2017-07-30 21:46:54 CLIENT -> SERVER: QUIT
    2017-07-30 21:46:54 SERVER -> CLIENT: 221 closing connection
    SMTP connect () failed. Troubleshooting · PHPMailer/PHPMailer Wiki · GitHub
    Mailer Error: SMTP connect () failed. Troubleshooting · PHPMailer/PHPMailer Wiki · GitHub
It seems that you are just starting to send the mail, close the connection. What can be the problem?

The code of the script is the follow:
 * This example shows settings to use when sending via Google's Gmail servers.

//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

require '../PHPMailerAutoload.php';

//Create a new PHPMailer instance
$mail = new PHPMailer;

//Tell PHPMailer to use SMTP

//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;

//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';

//Set the hostname of the mail server
$mail->Host = '';
// use
// $mail->Host = gethostbyname('');
// if your network does not support SMTP over IPv6

//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;

//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';

//Whether to use SMTP authentication
$mail->SMTPAuth = true;

//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "[email protected]";

//Password to use for SMTP authentication
$mail->Password = "xxxxxxx";

//Set who the message is to be sent from
$mail->setFrom('[email protected]', 'First Last');

//Set an alternative reply-to address
$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 GMail 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'), dirname(__FILE__));

//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';

//Attach an image file

//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
Last edited by a moderator:


Well-Known Member
Aug 23, 2015
cPanel Access Level
Root Administrator
Do you have any custom firewall rules or applications that could block that port? If yes then try to disable it.


Staff member
Apr 11, 2011
The option: whm > security center > SMTP Restrictions is Disable.

Are you also using the CSF firewall? If so, check to see if the "SMTP_BLOCK" setting is enabled.

Thank you.

Usif Nasirov

Active Member
Jun 11, 2016
cPanel Access Level
Reseller Owner
maybe somebody is try to find how can solve this problem

vi /etc/csf/csf.conf
section # SECTION:SMTP Settings

just add your user

smtp_allowuser : cpanel,myuser

( user is ftp login domain )

csf -r
that is all