LinHES Forums http://forums.linhes.org/ |
|
Email from LinHES box http://forums.linhes.org/viewtopic.php?f=5&t=31265 |
Page 1 of 1 |
Author: | winnpeny [ Sat Feb 13, 2021 4:37 pm ] |
Post subject: | Email from LinHES box |
Is there a recommended way to send emails from a LinHES box? I would like to use a command line based method to send an email depending on the return status of a script. This topic has been brought up at least twice before, in 2007 http://forums.linhes.org/viewtopic.php?f=5&t=1617 and in 2011 http://forums.linhes.org/viewtopic.php?f=21&t=22246. Is there anything current baked in to LinHES or something that can be installed from a package to accomplish this? Is anyone doing this and have a method you like? Thanks |
Author: | welner [ Sat Feb 13, 2021 5:37 pm ] |
Post subject: | Re: Email from LinHES box |
I do believe I played around with postfix a while back. I forget the details. I think postfix tries to configure itself for you upon initial invocation. postfix is in the linhes 'extra' repo. |
Author: | nbdwt73 [ Mon Feb 15, 2021 8:08 am ] |
Post subject: | Re: Email from LinHES box |
Yes it is quite easy... I use email alerts for a lot of things on my Myth box. See https://rtcamp.com/tutorials/linux/ubun ... mail-smtp/ for instructions to configure postfix (you will have to get the app from 'extra'). After installation and configuration, make sure you restart postfix... postfix stop postfix start I have one script file that calls postfix and I call that script file from several places within the system (ie one common script for everything). #!/bin/bash sendmail xxxxxxxx@yyyyyyyyyy.com <<EOF From:xxxxxxxxx@gmail.com subject:Mythtv Alert $* EOF |
Author: | winnpeny [ Sat Mar 27, 2021 7:13 pm ] |
Post subject: | Re: Email from LinHES box |
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) |
Page 1 of 1 | All times are UTC - 6 hours |
Powered by phpBB® Forum Software © phpBB Group http://www.phpbb.com/ |