Sendmail works from script, but not when called from Apache


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sendmail works from script, but not when called from Apache
# 1  
Old 05-05-2011
Sendmail works from script, but not when called from Apache

Hi,

I am building a web interface to run a series of shell scripts that reside on the web 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. The webpage issues a call out to a tiny PHP script that calls the bash shell script. In one of the scripts I am trying to add the ability to send an e-mail with sendmail when a software build fails. It looks something like this:

---------------------------------------------------------------------------------------
Code:
DistributionList="me@mydomain.com";
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 when I invoke the SAME script file via the web UI it produces the following error:

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

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. Note, this is all running on Mac OS X. Can anyone provide any assistance, or pointers to relevant informaion?

Note, when I run the script manually from a Terminal window (when it works) I am logged in as an administrative user (not root), call it user_a. Apache is set in its config file to also run as the same administrative (non-root) user, user_a. (And yes I confirmed it is indeed running as that user.) The sendmail command is just a line in the bash script so it should be running as the same user that invokes the script - that would be the user apache is running as - or user_a.

Also, when googling this error it turns up a lot of pages about PHP & Drupal and setting the PHP.ini file to use the -t flag when calling sendmail. But that has nothing to do with my problem. My call to sendmail is not occurring from inside PHP, to be clear, it is inside a bash script that gets called by PHP.

Any help would be greatly appreciated.

THANKS!

MacQAGuy

Last edited by pludi; 05-05-2011 at 05:58 PM..
# 2  
Old 05-11-2011
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to pass values to a script called from within another script in shell?

Need ideas on how to achieve the below. We have a script say "profile.sh" which internally calls another existing script called "name.sh" which prompts for the name and age of a person upon execution. When i run profile.sh how can i populate a pre-defined value from another file and pass that... (1 Reply)
Discussion started by: sankasu
1 Replies

2. Shell Programming and Scripting

Shell script to pass the config file lines as variable on the respective called function on a script

I want to make a config file which contain all the paths. i want to read the config file line by line and pass as an argument on my below function. Replace all the path with reading config path line by line and pass in respective functions. how can i achieve that? Kindly guide. ... (6 Replies)
Discussion started by: sadique.manzar
6 Replies

3. Shell Programming and Scripting

Calling bash script works when called manually but not via Cron?

Hi, I've got a Bash backup script I'm trying to run on a directory via a cron job nightly. If I ssh in and run the script manually it works flawlessly. If I set up the cron to run evertything is totally messed up I don't even know where to begin. Basically the path structure is ... (6 Replies)
Discussion started by: wyclef
6 Replies

4. Shell Programming and Scripting

Need output of script on screen and file with correct return status of the called script.

Hi, I am trying to capture logs of the script in the file as well as on the screen. I have used exec and tee command for this. While using exec command I am getting the correct output in the file but, script output is not getting displayed on the screen as it get executed. Below is my sample... (14 Replies)
Discussion started by: Prathmesh
14 Replies

5. Shell Programming and Scripting

Passing variable from called script to the caller script

Hi all, Warm regards! I am in a difficult situation here. I have been trying to create a shell script which calls another shell script inside. Here is a simplified version of the same. Calling Script. #!/bin/ksh # want to run as a different process... (6 Replies)
Discussion started by: LoneRanger
6 Replies

6. Shell Programming and Scripting

Script will keep checking running status of another script and also restart called script at night

I am using blow script :-- #!/bin/bash FIND=$(ps -elf | grep "snmp_trap.sh" | grep -v grep) #check snmp_trap.sh is running or not if then # echo "process found" exit 0; else echo "process not found" exec /home/Ketan_r /snmp_trap.sh 2>&1 & disown -h ... (1 Reply)
Discussion started by: ketanraut
1 Replies

7. Shell Programming and Scripting

Expect script called in loop from Bash Script

Having issues with an expect script. I've been scripting bash, python, etc... for a couple years now, but just started to try and use Expect. Trying to create a script that takes in some arguments, and then for now, just runs a pwd command(for testing, final will be command I pass). Here is... (0 Replies)
Discussion started by: cbo0485
0 Replies

8. Shell Programming and Scripting

How to return the value from the called shell script to the calling sh script

Hi all, I have two ksh scripts #sample1.sh #!/bin/ksh . ./sample2.sh echo $fileExist #sample2.sh #!/bin/ksh func() { i=1 return $a } func echo $? Here how should I return the value of sample2.sh back to sample1.sh? Thanks in advance. (2 Replies)
Discussion started by: gp_singh
2 Replies

9. Shell Programming and Scripting

passing a variables value from the called script to calling script using ksh

How do i get the value of the variable from the called script(script2) to the calling script(script1) in ksh ? I've given portion of the script here to explain the problem. Portion of Script 1 ============= ----- ----- tmp=`a.ksh p1 p2 p3` if then # error processing fi -----... (10 Replies)
Discussion started by: rajarkumar
10 Replies

10. UNIX for Dummies Questions & Answers

how sendmail works

Excuse me for this question really for dummies! I would like to know how sendmail works, obviously even in few words. If it uses a mail server or relay to send mail, if there is some check that sendmail makes to the from address and so on... Thank you very much. (3 Replies)
Discussion started by: alzep
3 Replies
Login or Register to Ask a Question