11

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" [email protected]
2
  • 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, 2012 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, 2014 at 19:46

4 Answers 4

4

/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.

3
  • 1
    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, 2012 at 21:37
  • This is true... to determine my path, I ran which mail and it returned /bin/mail.
    – Ben
    Mar 19, 2012 at 12:11
  • 2
    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, 2014 at 19:41
15

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: [email protected]
Subject: Testing
From: [email protected]

This is a test message
EOF

Note that the blank line is important.

5
  • 1
    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, 2012 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, 2012 at 13:46
  • 2
    This is the canonical approach to safe programmatic mail sending under UNIX.
    – Jim Dennis
    Jun 16, 2014 at 19:42
  • finally got a working example for sendmail! Thank you! May 21, 2019 at 9:56
  • if the sendmail were to fail and write to STDERR, how could I capture this as a variable to use later in the script?
    – mkst
    Dec 31, 2019 at 8:54
1
echo -e "This is my body\nSecond line" 2>&1 | sed '1!b;s/^/To: [email protected]\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.

-1
php -f file.php 2>&1 | sed 's/^/To: [email protected]\nSubject: My Test Email Subject\n\n/' | sendmail -t
1
  • 1
    Hi and welcome to Server Fault. Perhaps you could elaborate your answer a bit?
    – slm
    Aug 21, 2013 at 0:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .