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.
Sendmail is one of the most popular SMTP servers in Linux. You can easily send emails directly from the command line using the sendmail command. To route the information, the sendmail command makes use of the network configured on your system. Let's execute the following commands to create a file having email content.
$() Command Substitution According to the official GNU Bash Reference manual: “Command substitution allows the output of a command to replace the command itself.
Once logged in, you can run the following command to send email: [server]$ /usr/sbin/sendmail [email protected] Subject: Test Send Mail Hello World control d (this key combination of control key and d will finish the email.)
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 = '[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) # 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With