5

Good day!

Im trying to make my phpmailer work in my shared hosting (freehostia.com) and I always get this error. The username and password of my gmail is correct, and the rest of the settings is like this:

$mail = new PHPMailer;

$mail->isSMTP();
$mail->SMTPDebug  = 2;
$mail->Host       = 'tls://smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth   = true; // Enable SMTP authentication
$mail->Username   = '[email protected]'; // SMTP username
$mail->Password   = 'mypassword'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port       = 587; // TCP port to connect to

$mail->setFrom('[email protected]', 'ASAPHOT Administrator'); // Add a recipient
$mail->addAddress('[email protected]'); // Name is optional
$mail->addReplyTo('[email protected]', 'ASAPHOT Administrator');

$mail->isHTML(true); // Set email format to HTML

$mail->Subject = 'it works';
$mail->Body    = 'it works';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if (!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

openssl is uncommented in the php.ini also. Am I missing something here? Thank you.

Complete error message:

Connection: opening to smtp.gmail.com:587, timeout=300, options=array ()
SMTP ERROR: Failed to connect to server: Permission denied (13)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
4

6 Answers 6

26

Let's get to the answer first of all! Try with the following command:

$ getsebool httpd_can_sendmail

if it shows: httpd_can_sendmail --> off

switch it on with this:

$ sudo setsebool -P httpd_can_sendmail 1

then try sending the email again.

This solution comes from this great page.

As it was pointed out from this article, you may also need to try sudo setsebool -P httpd_can_network_connect 1. While for my CentOS 7 vm hosted by DigitalOcean, it's not necessary.

The problem I met was failing to send out emails from a Drupal website, with the SMTP Authentication Support module, which relies on PHPMailer underneath. And the SMTP server used was Google.

BTW, I had suspected it was OpenSSL certificate problem and did some test but no luck. So by setting the $SMTPDebug level to 2 from the PHPMailer source code, I was able to capture the "Permission denied (13)" error message.

1
  • Thank you so much for this answer, I wish I could upvote it more than once.
    – Azkatro
    May 25, 2021 at 7:11
2

This suggests that fopen wrappers or socket functions are disabled in your PHP installation. Not unusual in shared hosting. Running phpinfo() should tell you.

You can probably use $mail->isMail(); and skip the auth to send via the ISP's mail server instead of SMTP, but beware of SPF issues.

1
  • 1
    you just helped me a lot.
    – theRiley
    Nov 17, 2019 at 6:53
0

the problem is that the free hosting of freehostia blocks outcoming mails.

0

Freehostia blocks the list below in the free plan. (Chocolate)

-mailer

-curl

-soap get

-xml get

0

this is important for http

  • getsebool httpd_can_sendmail is Off then turn On it sudo setsebool -P httpd_can_sendmail 1
  • may need sudo 'setsebool -P httpd_can_network_connect 1'
  • disable_function = ...... delete exec for exec() in php.ini
-2
use this
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "ssl://smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port

if this not working than try bellow for php code.

mail($to,$subject,$message);

2
  • try this $mail->SMTPDebug = 0; // 1 to enables SMTP debug (for testing), 0 to disable debug (for production)
    – kaushik
    Nov 27, 2015 at 6:28
  • Hi dear, your phpmailer class file is not a correct code. so please find correct class.phpmailer.php
    – kaushik
    Feb 26, 2016 at 9:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.