SMTP connect() failed when request comes from browser

emailgmailphp5sendmailsmtp

I have a PHP script which send email using PHPMailer library. When I run the script in the SSH using PHP command, it sends email perfectly. But when the request for the same script is coming from a browser, it is failing and giving below error.

2016-12-17 19:41:24 SMTP ERROR: Failed to connect to server: (0) 2016-12-17 19:41:24    SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting error

I am not sure what is causing it not work for browser requests.

My script is on AWS and I am using google business email.

Below is my code snippet.

require_once 'PHPMailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->IsHTML(true);
$mail->CharSet = "text/html; charset=UTF-8;";
$mail->SMTPDebug = 2;
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; 
$mail->Username = "google email address";  
$mail->Password = "password";
$mail->From = $email_from;
$mail->FromName = "Name";
$mail->AddAddress($to);
$mail->Subject = $subject;
$mail->Body    = $message;
$sent = $mail->Send();

I will appreciate any help. Thanks.

EDITED: I have managed to solve the "Module 'curl' already loaded in Unknown on line 0" issue. There were 2 PHP.ini files in the server which was causing the issue. Renaming one PHP.ini resolved the curl loaded issue.

But still I am unable to send email using nobody user. However I can send email using root user.

I have noticed a configuration in the WHM for "Prevent “nobody” from sending mail". By default it was on meaning nobody is not allowed to send email. So, if I turn it off, I should be able to send email using nobody. But not sure why after turning it off still I can not send email using nobody user.

EDITED: When I checked apache server logs, I found below error being generated for each request coming from the browser. Not sure what this error is. I am doing research on this. I will appreciate if anybody is aware of the issue. Thank You!
******************ERROR LOGS*******************************
[Tue Dec 20 12:38:45.875892 2016] [ssl:error] [pid 913] (101)Network is unreachable: [client xx.xx.xx.xx:151] AH01974: could not connect to OCSP responder 'ocsp.comodoca.com'
[Tue Dec 20 12:38:45.875929 2016] [ssl:error] [pid 913] AH01941: stapling_renew_response: responder error
[Tue Dec 20 12:38:46.181171 2016] [ssl:error] [pid 919] (101)Network is unreachable: [client xx.xx.xx.xx:848] AH01974: could not connect to OCSP responder 'ocsp.comodoca.com'
[Tue Dec 20 12:38:46.181211 2016] [ssl:error] [pid 919] AH01941: stapling_renew_response: responder error


Thanks for Your Help!

Best Answer

Are you running the php as root over ssh? Or another user that has higher permissions than the web server?

If you know what user the web server is running as, try running the php script as that user. You can use phpinfo() to find out what user it is running as. One way of running a script as that user is as follows... First be root, or a user that can do sudo, and type the following, to change to the web server user:

sudo -u www-data bash

Obviously replace www-data with the name of the web server user if it is different, and bash with a valid shell on your server if you don't have bash. One you have the web server user's bash prompt, run id to check you are indeed the right user, and then try running your PHP script from that user's commandline. If it gives the same error then it must be the permissions issue. If it works, then at least you ruled it out!

If it does appear to be a permissions issue, try the following:

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

To enable permission.