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
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.
Since Mac OS X includes Python, consider using a Python script instead of a Bash script. I haven't tested the sending portion, but it follows the standard example.
# Settings SMTP_SERVER = 'mail.myisp.com' SMTP_PORT = 25 SMTP_USERNAME = 'myusername' SMTP_PASSWORD = '$uper$ecret' SMTP_FROM = 'firstname.lastname@example.org' SMTP_TO = 'email@example.com' 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) # EDIT: mailer is already connected # mailer.connect() mailer.login(SMTP_USERNAME, SMTP_PASSWORD) mailer.sendmail(SMTP_FROM, [SMTP_TO], msg.as_string()) mailer.close()
I hope this helps.
Actually, "mail" works just as well.
mail -s "subject line" firstname.lastname@example.org < 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.