Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending mail from a Bash shell script

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.

like image 802
ridogi Avatar asked Nov 30 '09 21:11

ridogi


People also ask

Can you send email from command line?

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.

What is $() in bash script?

$() Command Substitution According to the official GNU Bash Reference manual: “Command substitution allows the output of a command to replace the command itself.

How do I send an email using sendmail?

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.)


1 Answers

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.

Python script

# 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.

like image 164
Jason R. Coombs Avatar answered Sep 24 '22 17:09

Jason R. Coombs