I ended up implementing this method. The advantage is that it does not require the installation or setup of any software beyond what comes with a vanilla install of LinHES. The disadvantage is that it requires the sender's email address and password in plain text in the script file. It requires a smtp_ssl type connection, which is the type gmail uses. So I created a new gmail account, and that account is the one that sends the email message from the LinHES box. I've been using this for a couple months now and its been working great for me.
This version sends the contents of a file as the body of the message:
Code:
$ cat email_body.py
#!/usr/bin/python2
# https://realpython.com/python-send-email/
# https://www.tutorialspoint.com/python/python_command_line_arguments.htm
# email a file as the body of a plain text email
# usage: email_attach.py -r <receipient email address> -s <subject> -f </path/to/file>
# assumes the smtp server uses smtp_ssl, gmail for example
# Note: senders email credentials are in plain text in this file
import sys, getopt
import email, smtplib, ssl
import sys
import os.path
def options(argv):
from os import path
exit_code = 0
receiver_email = ''
subject = ''
body_file = ''
try:
opts, args = getopt.getopt(argv,"hr:s:f:")
except getopt.GetoptError:
print 'Illegal option'
exit_code = exit_code + 2
for opt, arg in opts:
if opt == '-h':
exit_code = exit_code + 1
elif opt == '-r':
receiver_email = arg
elif opt == '-s':
subject = arg
elif opt == '-f':
body_file = arg
else:
print 'unrecognized option: ' + opt
exit_code = exit_code + 4
if body_file != '':
if path.isfile(body_file) == False:
print 'File: ' + body_file + ' not found'
exit_code = exit_code + 32
else:
print '<filename> is undefined'
exit_code = exit_code + 64
if receiver_email == '':
print '<email addr> is undefined'
exit_code = exit_code + 128
if exit_code > 0:
print str(sys.argv[0]) + '-r <email_addr> -s <subject> -f <email_body_fiile>'
if exit_code > 1:
print 'Error: ' + str(exit_code)
sys.exit(exit_code)
return receiver_email, subject, body_file;
def send_email(receiver_email, subject, body_file):
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
sender_email = “myemailaddress@gmail.com"
password = “myPassword123”
# Create a multipart message and set headers
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = receiver_email
message["Subject"] = subject
#message["Bcc"] = receiver_email # Recommended for mass emails
# Add body to email
fp = open(body_file, 'r')
message.attach(MIMEText(fp.read()))
fp.close()
#message.attach(MIMEText(body, "plain"))
# Log in to server using secure context and send email
server = smtplib.SMTP_SSL("smtp.gmail.com", 465)
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, message.as_string())
if __name__ == "__main__":
#print str(sys.argv)
receiver_email, subject, body_file = options(sys.argv[1:])
send_email(receiver_email, subject, body_file)
This is how I call the script from within a bash script:
Code:
/usr/bin/python2 /home/user/bin/email_body.py -r "${receiver_email}" -s "${subject}" -f "${thepath}${thefile}"
Here is a version that sends a file as an attachment:
Code:
$ cat email_attach.py
#!/usr/bin/python2
# https://realpython.com/python-send-email/
# https://www.tutorialspoint.com/python/python_command_line_arguments.htm
# email a file as an attachment
# usage: email_attach.py -r <receipient email address> -s <subject> -b <body message> -f </path/to/file>
# assumes the smtp server uses smtp_ssl, gmail for example
# Note: senders email credentials are in plain text in this file
import sys, getopt
import email, smtplib, ssl
import sys
import os.path
def options(argv):
from os import path
exit_code = 0
receiver_email = ''
subject = ''
body = ''
filename = ''
try:
opts, args = getopt.getopt(argv,"hr:s:b:f:")
except getopt.GetoptError:
print 'Illegal option'
exit_code = exit_code + 2
for opt, arg in opts:
if opt == '-h':
exit_code = exit_code + 1
elif opt == '-r':
receiver_email = arg
elif opt == '-s':
subject = arg
elif opt == '-b':
body = arg
elif opt == '-f':
filename = arg
else:
print 'unrecognized option: ' + opt
exit_code = exit_code + 4
if filename != '':
if path.isfile(filename) == False:
print 'File: ' + filename + ' not found'
exit_code = exit_code + 8
else:
print '<filename> is undefined'
exit_code = exit_code + 16
if receiver_email == '':
print '<email addr> is undefined'
exit_code = exit_code + 32
if exit_code > 0:
print str(sys.argv[0]) + '-r <email_addr> -s <subject> -b <email_body> -f </path/to/file>'
if exit_code > 1:
print 'Error: ' + str(exit_code)
sys.exit(exit_code)
basefilename = path.basename(filename)
return receiver_email, subject, body, filename, basefilename;
def send_email(receiver_email, subject, body, filename, basefilename):
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
sender_email = “myemailaddress@gmail.com"
password = “myPassword123”
# Create a multipart message and set headers
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = receiver_email
message["Subject"] = subject
#message["Bcc"] = receiver_email # Recommended for mass emails
# Add body to email
message.attach(MIMEText(body, "plain"))
with open(filename, "rb") as attachment:
# Add file as application/octet-stream
# Email client can usually download this automatically as attachment
part = MIMEBase("application", "octet-stream")
part.set_payload(attachment.read())
# Encode file in ASCII characters to send by email
encoders.encode_base64(part)
# Add header as key/value pair to attachment part
part.add_header(
"Content-Disposition",
"attachment; filename= " + basefilename,
)
# Add attachment to message and convert message to string
message.attach(part)
text = message.as_string()
# Log in to server using secure context and send email
server = smtplib.SMTP_SSL("smtp.gmail.com", 465)
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, text)
if __name__ == "__main__":
#print str(sys.argv)
receiver_email, subject, body, filename, basefilename = options(sys.argv[1:])
send_email(receiver_email, subject, body, filename, basefilename)