Sending an email via script

Jason
Contributor II

This is something i've never tried to do before so i'm not quite sure of the process. I want to run a script and have it send an email at the end letting me know that it completed (repo_sync results from reposado).

I've seen some mention of sendmail, something i've never used before. But when i try it it doesn't seem to do anything. So i'm fairly certain i need to configure it for my Exchange Online configuration. Is this a fairly straight forward process? I've seen a few different examples through searching and not sure what the best approach is. Do i just edit main.cf with a relayhost? Do i need to specify credentials in sasl_passwd? Are there other steps? Is there any easier/newer options if it's running High Sierra?

Thanks

3 REPLIES 3

dan-snelson
Valued Contributor II

@Jason We use Python to send emails from the end-point:
The Python Standard Library: 20.1.8. email: Examples.

#!/usr/bin/python
####################################################################################################
#
# ABOUT
#
#   Script to send plain-text email:
#       * Parameter 4: recipient
#       * Parameter 5: subject
#       * Parameter 6: message
#
####################################################################################################
#
# HISTORY
#
#   Version 1.0, 7-Dec-2015, Dan K. Snelson
#
####################################################################################################

# Import commands
import commands, os, sys, smtplib, email
from email import encoders

SMTP_SERVER = 'smtp.company.com'
SMTP_PORT = 25
SMTP_FROM = 'noreply@company.com'                                                     # Must be a complete email address
SMTP_TO = str(sys.argv[4])                                                          # Parameter 4: recipient
SUBJECT = str(sys.argv[3]) + ", " + str(sys.argv[2]) + ": " + str(sys.argv[5])      # Parameter 5: subject
MESSAGE = str(sys.argv[6])                                                          # Parameter 6: message


msg = email.MIMEMultipart.MIMEMultipart()
body = email.MIMEText.MIMEText(MESSAGE)
msg.attach(body)
msg.add_header('From', SMTP_FROM)
msg.add_header('To', SMTP_TO)
msg.add_header('Subject', SUBJECT)


# Now send the message
mailer = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
mailer.sendmail(SMTP_FROM, [SMTP_TO], msg.as_string())
mailer.close()

mm2270
Legendary Contributor III

The old Unix mail command seems to work well most of the time. The syntax to do this is something like the following

echo "Message goes here" | mail -s "Subject goes here" <email_recipient>

If the above gets sent as the user, the account it's coming from usually shows up as the computer or host name, I believe. if sent via root, it shows up as "System Administrator" in my experience.

There are more advanced ways of sending mail than the above, and even some python methods to get attachments into the email, something that's really hard, maybe even impossible to do with the regular unix mail command. But if all you need is a simple email sent with subject and message, the above should work.

Caveat: I have not tried the above in High Sierra, so I don't know if something changed in that OS to make this no longer work. It wouldn't surprise me, but hopefully that hasn't been killed in it.

Can you confirm this still works? I was working on a process to email notify as well s display notify the end user when the battery is faulty within the MacBook they are using, so that IT support can respond and potentially have the battery replaced while warranty is still detected.