Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old May 5, 2011, 01:02 PM   #1
SSmagicman
macrumors newbie
 
Join Date: Mar 2003
Sendmail gives error only when invoked via apache

Hi,

I have a bash script that used sendmail to send a simple e-mail...

-------------------------------------------------------------------------------- ---------------------------
DistributionList=`/usr/libexec/PlistBuddy -c "Print :EmailDistributionList" /Library/WebServer/Documents/server_settings.plist`;
MessageFile="/tmp/build_failure_email.txt";

if [ -e $MessageFile ];
then
rm $MessageFile
fi

#echo "To: $DistributionList" >> $MessageFile;

Subject="My Subject...";
echo "Subject: ${Subject}" >> $MessageFile;

FromName="My Name";
FromAddress="me@mydomain.com";
echo "From: $FromName <${FromAddress}>" >> $MessageFile;


echo "Message Body line 1..." >> $MessageFile;
echo "Message Body line 2." >> $MessageFile;

/usr/sbin/sendmail "$DistributionList" < $MessageFile
#/usr/sbin/sendmail -t "$DistributionList" < $MessageFile

-------------------------------------------------------------------------------- ---------------------------

When I run this script from the command line it works perfectly fine! No errors, and I get the e-mail, all is good in the world. Now If I have a webpage running out of /Library/WebServer/Documents and have a php file there that invokes the script, then it outputs the following error:

SendMail: fatal: Recipient addresses must be specified on the command line or via the -t option

Note, apache is running as an administrative user (the same user that I manually called the script from when it worked). As you can see I AM specifying the address on the command line, and I have also tried it with -t and putting the address in the file. But neither way works when the bash script is called from apache by way of the PHP script. I have spent hours trying to figure this out with no luck. Can anyone provide any pointers?

THANKS!
SSmagicman is offline   0 Reply With Quote
Old May 5, 2011, 02:15 PM   #2
willieva
macrumors 6502
 
Join Date: Mar 2010
It looks like the DistributionList isn't getting filled in. Print that out prior to the sendmail call to see what is happening.

It's always a good idea to start a bash script with #!/bin/bash so you're sure it's being run as a bash script.
willieva is offline   0 Reply With Quote
Old May 5, 2011, 02:16 PM   #3
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
I don't know why it's not working, but why not use PHP's mail() function to send the email instead (which uses sendmail)?
kainjow is offline   0 Reply With Quote
Old May 5, 2011, 02:35 PM   #4
ulbador
macrumors 68000
 
ulbador's Avatar
 
Join Date: Feb 2010
There would be two main things that would cause this to fail.

First, the proper environment variables. This seems not to be the case because everything has a full path on it.

The second and more likely problem, is some sort of permission error. Does whatever user the apache process is running as have the right permission to run that? You can test it with something like "su _www /pathToMyScript".

That may not work without setting a login shell for the apache user, so edit the /etc/passwd entry for the apache user (or whatever user your Apache is running as, I believe the default is _www on OSX) and temporarily change it from probably /usr/bin/false to /bin/sh (don't forget to change it back)

Finally, the -t option tells it to parse the message to figure out who to send it to. So with:

/usr/sbin/sendmail -t < $MessageFile

$MessageFile would have to contain like:

Code:
To: Me@me.com
From: You@you.com
Subject: Hello

World
__________________
Mooooooooooooo

Last edited by ulbador; May 5, 2011 at 02:41 PM.
ulbador is offline   0 Reply With Quote
Old May 5, 2011, 02:38 PM   #5
SSmagicman
Thread Starter
macrumors newbie
 
Join Date: Mar 2003
Quote:
Originally Posted by willieva View Post
It looks like the DistributionList isn't getting filled in. Print that out prior to the sendmail call to see what is happening.

It's always a good idea to start a bash script with #!/bin/bash so you're sure it's being run as a bash script.
DistributionList gets filled in OK. I printed it out. And I also replaced it with a single address hard-coded. Again, the same script run from the Terminal works, but when called from the web interface doesn't.

Also, I just included a small section of the script here which is why the #!/bin/bash is missing, the whole thing spans multiple files and is dozens of pages long. But thanks for the suggestions.

Quote:
Originally Posted by kainjow View Post
I don't know why it's not working, but why not use PHP's mail() function to send the email instead (which uses sendmail)?
I am building a web interface to run a series of shell scripts that reside on the server. The bash script are written such that they can be used independently for the task they are meant for, or the same scripts can be run from this web UI. The scripts are mostly for doing software builds, testing, and uploading to another server. So moving any of the functionality out of the bash script into the webapp really wouldn't work, because then the same script couldn't be used manually to do the same thing.

Quote:
Originally Posted by ulbador View Post
Finally, the -t option tells it to parse the message to figure out who to send it to. So with:

/usr/sbin/sendmail -t < $MessageFile

$MessageFile would have to contain like:

Code:
To: Me@me.com
From: You@you.com
Subject: Hello
Right, as you can see in the code in my original post I had the line commented out that added the To: field to the $MessageFile. But when that line is there, the error is still produced. Or if the to address is provided on the command line the error is still produced. Or if the to address is provided both places the error is still produced.

Quote:
Originally Posted by ulbador View Post
The second and more likely problem, is some sort of permission error. Does whatever user the apache process is running as have the right permission to run that? You can test it with something like "su _www /pathToMyScript".
Well, my Apache process is already running as my administrative (non-root) user. That is because of other things I do in the scripts. So it is not running as _www. And the script runs fine, its the single command that calls sendmail that produces the error.

Quote:
Originally Posted by ulbador View Post
That may not work without setting a login shell for the apache user, so edit the /etc/passwd entry for the apache user (or whatever user your Apache is running as, I believe the default is _www on OSX) and temporarily change it from probably /usr/bin/false to /bin/sh (don't forget to change it back)
When I open /etc/passwd the comments at the top say that the file is only used when running in single user mode, which is not the case here. Also I do not see any entry there in the file for any of the Mac OS X users that are listed in the Accounts preference pane, such as the one my script is running as. I do see _www listed there with /usr/bin/false, but again apache isn't running as that user.

Last edited by kainjow; May 5, 2011 at 07:35 PM. Reason: merged posts
SSmagicman is offline   0 Reply With Quote
Old May 5, 2011, 03:23 PM   #6
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by SSmagicman View Post
I am building a web interface to run a series of shell scripts that reside on the server. The bash script are written such that they can be used independently for the task they are meant for, or the same scripts can be run from this web UI. The scripts are mostly for doing software builds, testing, and uploading to another server. So moving any of the functionality out of the bash script into the webapp really wouldn't work, because then the same script couldn't be used manually to do the same thing.
The php interpreter can be run from the command-line. It has a man page, which see.

Change all your uses of $MessageFile to "$MessageFile". Quoting is always good hygiene in shell scripts.

Run the printenv command and the id command to make sure both are what is needed. I realize you've said you've checked those, but having the evidence in a log file is better.

Make sure the home directory for your user id is correct. If some command is looking for config info or defaults in a file, then you could have the correct uid but the wrong $HOME.

Have you tried running the bash script with the -x option? This will cause expanded command-lines to be echoed before being executed. Check to make sure the command-line with -t blah blah is what you expect it to be, and doesn't have any unexpected things like maybe some quoted whitespace that might be misinterpreted as a recipient to sendmail.

To turn on -x in the bash script itself:
Code:
set -x
If you still don't see anything, then replace the /usr/sbin/sendmail command in the script with the pathname of a diagnostic shell script that enumerates and prints its cmd-line args, and logs exactly what the contents of its stdin is (the hexdump command may be handy).

I would also suggest that to diagnose the problem it's possible we'd need to see the exact data in the MessageFile. Since that contains email addresses, I'm not asking you to post it. I'm just saying that without an actual fail-case of the actual code being run, on the actual data being processed, all any of us can do is guess.
chown33 is offline   0 Reply With Quote
Old May 5, 2011, 11:22 PM   #7
ulbador
macrumors 68000
 
ulbador's Avatar
 
Join Date: Feb 2010
Quote:
Originally Posted by SSmagicman View Post

Right, as you can see in the code in my original post I had the line commented out that added the To: field to the $MessageFile. But when that line is there, the error is still produced. Or if the to address is provided on the command line the error is still produced. Or if the to address is provided both places the error is still produced.
chown is probably right here, without knowing the data, it'll probably be difficult to diagnose.

As one more thing to try, notice the difference between your "sendmail -t" example and the one I quoted. It could potentially be an important difference.

As mentioned by others, you really are probably best off just putting this in a PHP script and executing it via the command-line, unless you have a hugely compelling reason not to. My company has dozens of little scripts that are run from the command-line from Apache, cron and everywhere else that do everything you can think of, and they all work great.

You can even put "#!/usr/bin/php" (or whatever the path to your PHP binary, type 'which php') at the top of your PHP scripts, chmod the x permission, and call your php scripts just like you would a shell script.

Code:
system("/home/myName/myPhpScript.php");

vs

system("/usr/bin/php /home/myName/myPhpScript.php");
__________________
Mooooooooooooo
ulbador is offline   0 Reply With Quote
Old May 11, 2011, 11:05 AM   #8
SSmagicman
Thread Starter
macrumors newbie
 
Join Date: Mar 2003
All,

I hate to admit it, but this was actually something really simple.

I had the portion of the script that sent the e-mail in a separate file, that was (supposed to be) sourced at the top of the main script. When I was running the main script interactively from the command line (when everything worked) it worked because I had already manually sourced that other file earlier in the Terminal session. And when apache was calling that other file it didn't work because that file hadn't been sourced yet. The reason it showed up as a sendmail error message was because in that other file I had named the routine that sent the e-mail "SendMail" as opposed to "sendmail" but when that other file wasn't sourced it was calling the normal sendmail program with no parameters -- thus the error.

Multiple mistakes here: 1) naming the separate routine "SendMail" rather than something more unique, 2) not sourcing the other file at the top of the main file.

Thanks for the help.
SSmagicman is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Workaround for:Unapproved caller. SecurityAgent may only be invoked by Apple software Sub-o OS X Mavericks (10.9) 7 Aug 11, 2014 04:51 PM
Invoked by apple software luisfelipetrigo OS X Mavericks (10.9) 5 Mar 12, 2014 01:43 PM
apache virtual hosts breaking apache ddragas OS X Mavericks (10.9) 1 Nov 30, 2013 10:54 AM
Beta 5: Control Center can't be invoked when keyboard is displayed Gogurt48 iOS 7 15 Aug 6, 2013 03:39 PM
Compiling SendMail program - Need Help papo2 OS X 10.8 Mountain Lion 0 Mar 29, 2013 02:56 PM

Forum Jump

All times are GMT -5. The time now is 06:10 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC