1

So, for one reason or another, I can't use the mail() PHP function and neither PHPMailer with SMTP. So looking for options, I wrote a short shell script to send an email with an attachment, as follows:

#!/bin/bash
echo "Some text $1 $2 $3" | mail -a "/path/to/attached/file.xlsx" -s "Subject text" some@email.com 

When I run the file directly from the terminal, it works perfectly and the mail is correctly received with the attachment and all.

But when I try to run from PHP, I have the following code:

shell_exec("bash /path/to/script $m1t $m2t $afl");

And it doesn't show any error and doesn't return any output, yet the mail is not sent.

Can't I make a shell script send emails by calling it through PHP? Or am I doing something wrong?

Appreciate any help on the matter.

José Colina
  • 21
  • 1
  • 10
  • Maybe [be this helps](https://stackoverflow.com/questions/20124327/php-shell-exec-command-is-not-working) – Yolo Jul 27 '17 at 19:24
  • I've had problems on FreeBSD servers where the `$PATH` either wasn't used or wasn't set when running shell commands from PHP. Try running `which $foo` through PHP to see if your PHP shell process can locate programs. – amphetamachine Jul 27 '17 at 19:27
  • @amphetamachine I'll take a look. But I don't think this is the problem as when I run other scripts (for example an `ls` inside the same shell file) it runes perfectly. – José Colina Jul 27 '17 at 19:32
  • 1
    I really advise sending e-mails using the built-in `mail()`-function or a library like `PHPMailer`. Calling `shell_exec()` raises security issues you should take care of (and is therefore disabled by [some] hosting providers). Read https://stackoverflow.com/a/24644450/2903251 for common issues why `mail()` won't work. – Peter van der Wal Jul 27 '17 at 19:33
  • Thanks for the advice @PetervanderWal but as I stated, at the current project I'm working on it's impossible, and for some reason the company's server has blocked all connections to outside SMTP servers. – José Colina Jul 27 '17 at 19:37
  • What server are you running on (CentOS/Debian/...)? And curious how it sends your `mail -a ....` without SMTP – Peter van der Wal Jul 27 '17 at 19:43
  • @Yolo already tried everything on that post and none of the answers helped with my problem. Thanks for the advice. – José Colina Jul 27 '17 at 19:43
  • @PetervanderWal you and me both, but none of the telnets I've tried have been successful (gmail and some other smtp addresses). And the server is running under Red Hat Enterprise Server 6.4 – José Colina Jul 27 '17 at 19:46
  • Hoping Red Hat is the same as CentOS in this, in terminal (as root): verify `getsebool httpd_can_sendmail` and enable it with `setsebool -P httpd_can_sendmail 1` (`-P` makes it permanent after a reboot) – Peter van der Wal Jul 27 '17 at 19:52
  • Have you tried sending it directly? `shell_exec('/bin/mail -a "/path/to/attached/file.xlsx" -s "Subject text" "some@email.com" < "Some text $1 $2 $3");` or appending ` 2>&1` to your shell_exec parameter so you can see any output of mail? – Yolo Jul 27 '17 at 20:00
  • Try `error_log(shell_exec("bash /path/to/script $m1t $m2t $afl"));` and see what gets written to the error log – FKEinternet Jul 27 '17 at 21:36
  • @FKEinternet Yes, I've tried sending it directly to no success. Appending 2>&1 happened to reveal the nature of the problem, and it appears to be a permission issue, the message was: 'send-mail: fatal: chdir /var/spool/postfix: Permission denied' I also tried Peter van der Wal's solution which apparently would help as well but I have no root access so I can't change active booleans and can't change permissions on the postfix directory (right now it's 0755). Guess I'm gonna have to give a call to the sysadmin, right? – José Colina Jul 28 '17 at 12:55
  • Turns out your comment @PetervanderWal solved my problem after I talked to the admin and changed that boolean to 1. If you wish to post it as an answer I'll go ahead and accept it. Thanks for everyone's help. – José Colina Jul 28 '17 at 13:45
  • @JoséColina - done. Glad it helped. Is the `mail()`-function/`PHPMailer` also working now? – Peter van der Wal Jul 28 '17 at 14:40
  • @PetervanderWal yes. After enabling httpd_can_sendmail both mail() and PHPMailer->mail() worked perfectly. Still no outside SMTP access, but don't need it now. Worth to note that the shell mail from PHP also worked, but didn't when tried to add attachments. PHPMailer solved this. Thanks for the help. – José Colina Jul 28 '17 at 15:01

1 Answers1

0

For Red Hat Enterprise Linux, CentOS, Fedora and other Linux-distros whith SELinux you should verify wether the Apache-process is allowed to access sendmail. This can be done by logging in to the server (SSH or open a terminal if localhost) and run

getsebool httpd_can_sendmail

When the output is httpd_can_sendmail --> off you can enable it by

sudo setsebool -P httpd_can_sendmail on

The -P-flag makes it permanent, so it still works after a reboot. Command may take a while to execute.

Peter van der Wal
  • 11,141
  • 2
  • 21
  • 29