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
- It is visible in the GUI when sending
- It steals focus if Mail is not the current application
- 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.
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
orssmtp
(available via MacPorts).In both cases use
mailx(1)
(ormutt
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.
Actually, "mail" works just as well.
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.
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:
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.
sendmail
and evenpostfix
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:
msmtp
should be very easy to install. In fact, there is a port for it, so it could be as easy asport install msmtp
.After installing and configuring
msmtp
, you can send email tojohn.doe@gmail.com
using:You can put the above in a script. See
man mail
for details.Send mail from Bash with one line: