views:

3982

answers:

10

I am writing a bash shell script for Mac that sends 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 OS 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.

+10  A: 

There a program called sendmail.

EDIT You probably don't want to use the -bs command unless you are sending it raw SMTP like martin's example below. -bs is for running a smtp server as a deamon. Sendmail will send directly to the receiving mail server (on port 25) unless you override it in the config file. You can specify the config file by the -C paramter.

In the config, you can specify a relay server (any mail server or sendmail running -bs on another machine)

Using a properly configured relay server is good idea because when IT manages mail servers they implement SPF and domain keys. That keeps your mail out of the junk bin.

If port 25 is blocked you are left with 2 options.

  1. Use the corporate smtp server.
  2. Run sendmail -bd on a machine outside of the corporate firewall that listens on a port other than 25.

Edit2 I believe you can add configuration parameters on the command line. What you want is the SMART_HOST option. so call sendmail like sendmail -OSMART_HOST=nameofhost.com

Byron Whitlock
hmm why the downvotes? sendmail [email protected] \nhellow byron\n. works just fine for me!
Byron Whitlock
because you could hardly have put any less effort into your answer.
hop
If the answer is good enough, I don't see why there should be more effort. More is not always better.
Fortega
votes should reflect relevance of an answer, not efforts put in it.
mouviciel
Can the -bs option in sendmail do what I am trying to do?
ridogi
I'm fine with the option of specifying a local SMTP server, but the question of how to specify that in a bash script is what I'm trying to get at.
ridogi
If you're going to use your own SMTP server (such as you would be if you ran sendmail), you may have some difficulty originating/relaying messages, depending on the host IP. Before you consider this option, I'd check http://www.spamhaus.org/ to make sure your IP address isn't listed in a common DNS blocklist.
Jason R. Coombs
+2  A: 

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.

hop
I'd like to make it easy to deploy this script on many machines without configuring anything else like postfix so I'd like it to be self contained in the script. I'll look into nullmailer and ssmtp. Is this possible with sendmail or smtpd that comes with OS X?
ridogi
errr...? sendmail is part of postfix, and postfix is the smtpd on os x, so what is your question?
hop
I didn't know if they are components of postfix or could operate on their own.
ridogi
I've used `esmtp` on the Mac. You can get it here: http://esmtp.sourceforge.net/
Dennis Williamson
Good to know about nullmailer, but I would prefer postfix over having to install anything to simplify deploying this script. I have not been able to find any directions on setting up postfix to handle outbound mail for a mail server that I specify. I assume that I would not be able to specify those settings only in my shell script, but that is ideally how this would work for me. If that isn't possible what config files for postfix need to be modified?
ridogi
+4  A: 

Probably the only way you could do this, while keeping the program self-sufficient, is if you have direct access to an SMTP server from the clients.

If you do have direct access to an SMTP server you can use the SMTP example from wikipedia and turn it into something like this:

#!/bin/bash
telnet smtp.example.org 25 <<_EOF
HELO relay.example.org
MAIL FROM:<[email protected]>
RCPT TO:<[email protected]>
DATA
From: Joe <[email protected]>
To: Jane <[email protected]>
Subject: Hello

Hello, world!
.
QUIT
_EOF

To handle errors I would redirect the output from telnet to a file and then grep that for a "success message" later. I am not sure what format the message should be, but I see something like "250 2.0.0 Ok: queued as D86A226C574" in the output from my SMTP server. This would make me grep for "^250.*queued as".

Martin Olsen
-1 That is a nice example, but useless in a real environment. For one: Where is the error-handling?
Tomas
Added error handling!
Martin Olsen
Shouldn't there be an empty line between the headers and the content in the DATA section?
Boldewyn
@Tomas: Answers in StackOverflow are mostly not adapted for industry strength robustness. And as proof of concept this is really nice.
Boldewyn
Technically, telnet is an external program and therefore this is not self -sufficient :) However, +1 for SMTP example.
Kimvais
@Kimvais: Neither is grep, bash, etc... ;-) Also, I can find no indication of telnet not being part of the default installation.
Martin Olsen
+8  A: 

actually "mail" works just as well.

mail -s "subject line" [email protected] < 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)

http://www.mactech.com/articles/mactech/Vol.21/21.09/ScriptingMail/index.html has a good description of how to work with mail in applescript.

Brian Postow
I am currently using postfix and my script contains the line cat /pathto/file.txt | mail -s "subject" [email protected]. I realize I could edit Postfix to specify an SMTP server with http://www.macosxhints.com/article.php?story=20081217161612647 but I am interested in putting the settings in the bash script itself. The applescript idea is a good one. I like that Mail.app wouldn't steal focus from the user (as it does with my automator workflow), but it still does require Mail.app be set up correctly. If I can't get these settings into my script, applescript may be the best alternative.
ridogi
what smtp settings are you trying to work with? you can probably set them, possibly with shell variables...
Brian Postow
It doesn't matter which server. Gmail, fastmail, Kerio, Exchange, etc. I would imagine any solution would work for any of them, but if there was a solution that worked with only one provider that would still be helpful. Specifying an alternate port would also be helpful as I mentioned in the update to my question above.
ridogi
+2  A: 

Here is a simple ruby script to do this. Ruby ships on the OS-X versions you mentioned.

Replace all the bits marked 'replace'. If it fails, it returns a non-zero exit code and a ruby back trace.

require 'net/smtp'

SMTPHOST = 'replace.yoursmtpserver.example.com'
FROM = '"Your Email" <[email protected]>'

def send(to, subject, message)
  body = <<EOF
From: #{FROM}
To: #{to}
Subject: #{subject}

#{message}
EOF
  Net::SMTP.start(SMTPHOST) do |smtp|
    smtp.send_message body, FROM, to
  end
end

send('[email protected]', 'testing', 'This is a message!')

You can embed this in a bash script like so:

ruby << EOF
 ... script here ...
EOF

Some other ways to send ruby emails: http://stackoverflow.com/questions/58478/how-do-i-send-mail-from-a-ruby-program

You can use other languages that ship with OS-X as well:

Ciao!

The Doctor What
A: 

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.

tmeisenh
Unfortunately, uuencode and mailx are not part of OSX. mail is, though. I'm unclear how much setup is required to make it work well. The from address, by default, is "systemname.local" which is uninformative. :-(
The Doctor What
I'm not concerned about the systemname.local being uninformative as I have identifying information in the subject, but I am concerned that the default postfix configuration will be blocked by firewalls.
ridogi
uuenecode and mailx are in the standard solaris install we have at my work. I _think_ they are standard in FreeBSD but I may have grabbed them from ports, I can't remember.You may look into a perl/python/ruby alternative.
tmeisenh
+8  A: 

Since Mac includes Python, why not consider a Python script instead of a batch script?

I haven't tested the sending portion, but it follows the standard example.

Hope this helps.

# settings

SMTP_SERVER = 'mail.myisp.com'
SMTP_PORT = 25
SMTP_USERNAME = 'myusername'
SMTP_PASSWORD = '$uper$ecret'
SMTP_FROM = '[email protected]'
SMTP_TO = '[email protected]'

TEXT_FILENAME = '/script/output/my_attachment.txt'
MESSAGE = """This is the message
to be sent to the client.
"""

# now construct the message
import smtplib, email
from email import encoders
import os

msg = email.MIMEMultipart.MIMEMultipart()
body = email.MIMEText.MIMEText(MESSAGE)
attachment = email.MIMEBase.MIMEBase('text', 'plain')
attachment.set_payload(open(TEXT_FILENAME).read())
attachment.add_header('Content-Disposition', 'attachment', filename=os.path.basename(TEXT_FILENAME))
encoders.encode_base64(attachment)
msg.attach(body)
msg.attach(attachment)
msg.add_header('From', SMTP_FROM)
msg.add_header('To', SMTP_TO)

# now send the message
mailer = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
mailer.connect()
mailer.login(SMTP_USERNAME, SMTP_PASSWORD)
mailer.sendmail(SMTP_FROM, [SMTP_TO], msg.as_string())
mailer.close()
Jason R. Coombs
Darn! I wanted to suggest the very same just the other moment. +1 anyways.
Boldewyn
+1 for python (and noting that it comes with the platform)
Kimvais
This is a great idea. I'm just now starting to learn python, so once I know my way around the language this may be just what I'm looking for.
ridogi
+1  A: 

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, -up 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
Mark Carey
A: 

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 google'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 [email protected]
user [email protected]
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 [email protected] using:

mail -s <subject> [email protected] <<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.

Alok
A: 

ridogi - Can you please post the solution when you have it? I have a very similar problem. Thanks!

marv