Cant send mails with PHPMailer

telmark

Registered
Jul 31, 2017
2
0
1
uruguay
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.

Code:
And the PHP script returns:
    2017-07-30 21:46:54 SERVER -> CLIENT: 220-hosting.xxxx.cl 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 xxxx.com.ar
    2017-07-30 21:46:54 SERVER -> CLIENT: 250-hosting.xxxx.cl Hello vovstore.com.ar [xxx.xxx.xxx.xxx] 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 hosting.xxxx.cl 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:
Code:
<?php
/**
 * 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
date_default_timezone_set('Etc/UTC');

require '../PHPMailerAutoload.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;

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

//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// 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
$mail->addAttachment('images/phpmailer_mini.png');

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

SysSachin

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

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,880
2,258
463
The option: whm > security center > SMTP Restrictions is Disable.
Hello,

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
29
4
53
Baku
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