1

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!

1

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.

7
  • Yes. It didn't run when I run the script using user apache is running with. It gives my some below specific error. PHP Warning: Module 'curl' already loaded in Unknown on line 0 X-Powered-By: PHP/5.6.29 Content-type: text/html; charset=UTF-8 2016-12-18 03:20:34 SMTP ERROR: Failed to connect to server: (0) 2016-12-18 03:20:34 SMTP connect() failed. github.com/PHPMailer/PHPMailer/wiki/Troubleshooting – user_387909 Dec 18 '16 at 3:24
  • ok good, that narrows it down. have updated my answer. see if that fixes. – bao7uo Dec 18 '16 at 10:25
  • Hey, thanks for your comment. But I am still getting same error after I issued your command. FYI, I have Centos server. – user_387909 Dec 18 '16 at 14:33
  • Hey, I think now I can not even run the script from SSH by root. I am getting same error as previously for browser request. "PHP Warning: Module 'curl' already loaded in Unknown on line 0". Do you think it might stopped working for root when i run the above two commands ? – user_387909 Dec 18 '16 at 16:30
  • 1
    selinux? serverfault.com/questions/563872/… – user993553 Dec 20 '16 at 12:46
1

Finally, I have managed to send email by nobody user in my Cent OS. I had to edit two settings from WHM.

Path to change the settings.

WHM -> Server Configuration -> Tweak Settings -> Under Mail Tab

By default below two settings was On. When I turned them off, it allowed me to send email by nobody user.

  1. Restrict outgoing SMTP to root, exim, and mailman (FKA SMTP Tweak)

  2. Prevent “nobody” from sending mail

Hope somebody can get benefited from this. Regards!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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