Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a cron command that runs a file and I'm trying to setup the output so it emails to me. I use this with 3rd party scripts that I don't want to modify the direct files, so I need to pipe the output instead of modifying the PHP.

I'm migrating from a server that uses mail to a server that uses sendmail. I'm struggling to find out how to properly send a subject to sendmail via a command line.

/usr/local/bin/php -f /path/to/file.php 2>&1 | /usr/sbin/sendmail -s "My Test Email Subject" test@email.com
share|improve this question
    
sendmail is the binary that is used by your system to route mail that is submitted to it by mail. It is part of a suite of programs like sendmail, postfix, exim and so on. So you need to install the appropriate package for mail. So tell us the linux distribution that you are using. –  adamo Mar 18 '12 at 21:32
    
All POSIX compliant forms of UNIX/Linux should provide some executable named "sendmail" and taking a specific set of options which are compatible with a subset of sendmail's functionality (including the -t option, described in timday's answer, below). Exim, Postfix, and qmail all provide these --- sometimes they'll be installed under /usr/libexec/sendmail rather than /usr/bin or /usr/sbin, for example. –  Jim Dennis Jun 16 at 19:46

4 Answers 4

up vote 2 down vote accepted

/usr/sbin/sendmail

That's wrong.

You should have a mail command some where. It could be called mailx. Should be in the /bin/ directory. As standard practice, php scripts should never be calling anything in /sbin or /usr/sbin. The sbin programs are typically for root.

Also, php has a built in mail function.

share|improve this answer
    
If it is not mail or mailx it may be Mail. But even if those do not exist, you can install mutt and run mutt -x which emulates mailx. –  adamo Mar 18 '12 at 21:37
    
This is true... to determine my path, I ran which mail and it returned /bin/mail. –  Webnet Mar 19 '12 at 12:11
    
sendmail is the better approach (it's a Posix standard) ... but it's better to use sendmail -t (take the headers from the input stream). This avoids many possible shell variable interpolation vulnerabilities by allowing the program sending mail to interact only with the sendmail utility without invoking any shell at all). See "timday's" answer for better details. –  Jim Dennis Jun 16 at 19:41

On my Debian systems (which have Exim rather than "real" sendmail, but still have a sendmail binary for compatibility), when I want to send mail from a script I do something equivalent to:

cat <<EOF | sendmail -t
To: recipient@example.com
Subject: Testing
From: sender@example.com

This is a test message
EOF

Note that the blank line is important.

share|improve this answer
    
The blank line is used to separate the header part of a message from the message body. Why not sendmail -t <<EOF though? –  adamo Mar 18 '12 at 22:01
    
@adamo: Because it was closer to the pattern in the original question to pipe into sendmail with '|' (and in fact the places I do this myself it's generally something like generate_test_message.sh | sendmail -t ). –  timday Mar 19 '12 at 13:46
    
This is the canonical approach to safe programmatic mail sending under UNIX. –  Jim Dennis Jun 16 at 19:42
php -f file.php 2>&1 | sed 's/^/To: test@email.com\nSubject: My Test Email Subject\n\n/' | sendmail -t
share|improve this answer
1  
Hi and welcome to Server Fault. Perhaps you could elaborate your answer a bit? –  slm Aug 21 '13 at 0:25
echo -e "This is my body\nSecond line" 2>&1 | sed '1!b;s/^/To: test@email.com\nSubject: My Test Email Subject\n\n/' | sendmail -t

echo = The body of the email.
sed stuff = Applies the email headers, and important double-line between body and headers.
sendmail -t = Pipes it directly through sendmail.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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