Sending mail from a Bash shell script

2019-03-07 17:50发布

I am writing a Bash shell script for Mac that sends an email notification by opening an automator application that sends email out with the default mail account in Mail.app. The automator application also attaches a text file that the script has written to. The problems with this solution are

  1. It is visible in the GUI when sending
  2. It steals focus if Mail is not the current application
  3. It is dependent on Mail.app's account setup being valid in the future

I figure to get around those shortcomings I should send the mail directly from the script by entering SMTP settings, address to send to, etc. directly in the script. The catch is I would like to deploy this script on multiple computers (10.5 and 10.6) without enabling Postfix on the computer. Is it possible to do this in the script so it will run on a base Mac OS X install of 10.5. and 10.6?

Update: I've found the -bs option for Sendmail which seems to be what I need, but I'm at a loss of how to specify settings.

Also, to clarify, the reason I'd like to specify SMTP settings is that mails from localhost on port 25 sent out via Postfix would be blocked by most corporate firewalls, but if I specify the server and an alternate port I won't run into that problem.

12条回答
疯言疯语
2楼-- · 2019-03-07 18:18

1) Why not configure postfix to handle outbound mail only and relay it via a mail gateway? Its biggest advantage is that it is already installed on OS X clients.

2) Install and configure one of the lightweight MTAs that handle only outbound mail, like nullmailer or ssmtp (available via MacPorts).

In both cases use mailx(1) (or mutt if you want to get fancy) to send the mails from a shell script.

There are several questions on Server Fault that go into the details.

查看更多
看我几分像从前
3楼-- · 2019-03-07 18:20

Actually, "mail" works just as well.

mail -s "subject line" name@address.ext < filename

works perfectly fine, as long as you have SMTP set up on your machine. I think that most Macs do, by default.

If you don't have SMTP, then the only thing you're going to be able to do is go through Mail.app. An ALTERNATIVE way to go through mail.app is via AppleScript. When you tell Mail.app to send mail via AppleScript you can tell it to not pop up any windows... (this does still require Mail.app to be configured).

Introduction to Scripting Mail has a good description of how to work with mail in AppleScript.

查看更多
▲ chillily
4楼-- · 2019-03-07 18:27

sendEmail is a script that you can use to send email from the command line using more complicated settings, including connecting to a remote smtp server: http://caspian.dotconf.net/menu/Software/SendEmail/

On OSX it is easily installable via macports: http://sendemail.darwinports.com/

Below is the help page for the command, take note of the -s, -xu, -xp flags:

Synopsis:  sendEmail -f ADDRESS [options]

Required:
  -f ADDRESS                from (sender) email address
  * At least one recipient required via -t, -cc, or -bcc
  * Message body required via -m, STDIN, or -o message-file=FILE

Common:
  -t ADDRESS [ADDR ...]     to email address(es)
  -u SUBJECT                message subject
  -m MESSAGE                message body
  -s SERVER[:PORT]          smtp mail relay, default is localhost:25

Optional:
  -a   FILE [FILE ...]      file attachment(s)
  -cc  ADDRESS [ADDR ...]   cc  email address(es)
  -bcc ADDRESS [ADDR ...]   bcc email address(es)

Paranormal:
  -xu USERNAME              authentication user (for SMTP authentication)
  -xp PASSWORD              authentication password (for SMTP authentication)
  -l  LOGFILE               log to the specified file
  -v                        verbosity, use multiple times for greater effect
  -q                        be quiet (no stdout output)
  -o NAME=VALUE             see extended help topic "misc" for details

Help:
  --help TOPIC              The following extended help topics are available:
      addressing            explain addressing and related options
      message               explain message body input and related options
      misc                  explain -xu, -xp, and others
      networking            explain -s, etc
      output                explain logging and other output options
查看更多
Melony?
5楼-- · 2019-03-07 18:28

Here's a modified shells script snip I've used on various UNIX systems... (echo "${MESSAGE}" | ${uuencode} ${ATTACHMENT}$basename ${ATTACHMENT}) | ${mailx} -s "${SUBJECT}" "${TO_LIST}"

uuencode and mailx are set to the executables. The other variables are from user input parsed using getopts.

This does work but I have to admit more often than not I use a simple Java program to send console emails.

查看更多
我想做一个坏孩纸
6楼-- · 2019-03-07 18:30

sendmail and even postfix may be too big to install if all you want to do is to send a few emails from your scripts.

If you have a Gmail account for example, you can use Google's servers to send email using SMTP. If you don't want to use gGoogle's server, as long as you have access to some SMTP server, it should work.

A very lightweight program that makes it easy to do so is msmtp. They have examples of configuration files in their documentation.

The easiest way to do it would be to set up a system-wide default:

account default
host smtp.gmail.com
from john.doe@gmail.com
user john.doe@gmail.com
password XXX
port 587

msmtp should be very easy to install. In fact, there is a port for it, so it could be as easy as port install msmtp.

After installing and configuring msmtp, you can send email to john.doe@gmail.com using:

mail -s <subject> john.doe@gmail.com <<EOF
<mail text, as many lines as you want. Shell variables will be expanded>.
EOF

You can put the above in a script. See man mail for details.

查看更多
等我变得足够好
7楼-- · 2019-03-07 18:32

Send mail from Bash with one line:

echo "your mail body" | mail -s "your subject" yourmail@yourdomain.com -a "From: sender@senderdomain.com"
查看更多
登录 后发表回答