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" test@email.com
Ben
  • 3,800
  • 18
  • 65
  • 96
  • `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](http://www.sendmail.org)_, _postfix_, _exim_ and so on. So you need to install the appropriate package for [mail](http://linux.die.net/man/1/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 '14 at 19:46

4 Answers4

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: recipient@example.com
Subject: Testing
From: sender@example.com

This is a test message
EOF

Note that the blank line is important.

timday
  • 866
  • 1
  • 10
  • 24
  • 1
    The blank line is used to separate the header part of a message from the message body. Why not `sendmail -t < – 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
  • 2
    This is the canonical approach to safe programmatic mail sending under UNIX. – Jim Dennis Jun 16 '14 at 19:42
  • finally got a working example for sendmail! Thank you! – insidepower May 21 '19 at 09: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 '19 at 08:54
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.

ablackhat
  • 1,933
  • 2
  • 14
  • 19
  • 1
    If it is not `mail` or `mailx` it may be `Mail`. But even if those do not exist, you can install [mutt](http://www.mutt.org) 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`. – Ben Mar 19 '12 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 '14 at 19:41
1
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.

sysadmin1138
  • 133,124
  • 18
  • 176
  • 300
Praveen R
  • 11
  • 2
-1
php -f file.php 2>&1 | sed 's/^/To: test@email.com\nSubject: My Test Email Subject\n\n/' | sendmail -t
bernstein
  • 101
  • 2
  • 1
    Hi and welcome to Server Fault. Perhaps you could elaborate your answer a bit? – slm Aug 21 '13 at 00:25