Problem with phpmailer from website on Oracle Cloud

I have implemented the classic phpmailer (https://github.com/PHPMailer) to send emails from my website. It works fine on the localhost and on another web server in the cloud. But my virtual server in the Oracle Cloud has its name resolution on Cloudflare. The DNS A records are not proxified. The MX records point to the gmail mail servers, and hence, all normal email in and out it running fine. Just the php script in my web page on this exact web server can’t connect to the Google SMTP servers. I guess it is a problem with Cloudflare. Does anyone have experience with this? This is a part of my php script:

$mail->Port = 587;
$mail->Host = “smtp.gmail.com”;
$mail->Username = “…my emai address here …”;
$mail->Password = “…my app password here…”;
$mail->AddAddress("…TO email address here …", “receiver name”);
$mail->SetFrom("…my emai address here …", “my name”);
$mail->Subject = “some subject”;
$mail->MsgHTML($content);
$result = $mail->Send();

The error message:
2020-02-24 18:29:49 SMTP ERROR: Failed to connect to server: Permission denied (13)

Your server shouldn’t be initiating outbound connections through Cloudflare on its way to Gmail. You’re going to have to try some manual outbound SMTP connections on your server. Something like:

telnet smtp.gmail.com 25

It could be that Gmail is blocking your server’s IP address and it has nothing to do with Cloudflare.

2 Likes

I could telnet to gmail on port 587 and on port 25, connect, EHLO, STARTTLS but while typing MAIL FROM: … the host closes the connection, perhaps I am not fast enough. But connecting works. I suspected Cloudflare, because my other server and localhost have no Cloudflare DNS (but same Gmail MX records) and they execute the script without change.

SOLUTION: It was a SELinux related problem. I had to issue the following commands to get the script running:

sudo setsebool -P httpd_can_sendmail 1
sudo setsebool -P httpd_can_network_connect 1

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.