View unanswered posts    View active topics

All times are UTC - 6 hours





Post new topic Reply to topic  [ 4 posts ] 
Print view Previous topic   Next topic  
Author Message
Search for:
 Post subject: Email from LinHES box
PostPosted: Sat Feb 13, 2021 4:37 pm 
Offline
Joined: Sat Jun 05, 2004 2:00 pm
Posts: 37
Location: St. Paul, MN
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


Top
 Profile  
 
PostPosted: Sat Feb 13, 2021 5:37 pm 
Offline
Joined: Sat Jan 06, 2007 7:08 pm
Posts: 125
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.

_________________
DH87MC i7-4770 16GB ram Xonar Essence ST geforce 710 LinHes 8.6


Top
 Profile  
 
PostPosted: Mon Feb 15, 2021 8:08 am 
Offline
Joined: Wed Jan 07, 2004 12:14 pm
Posts: 425
Location: Charlotte, NC
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

_________________
Backend server - 4.0 TB 3.0ghz dual core 6 gig RAM, nVidia 9400, Gigabyte GA-870A-UD3 MB, 2 HD-5500, 2 HD Homerun dual tuners, 3 frontend machines - LinHES 8.6.1


Top
 Profile  
 
PostPosted: Sat Mar 27, 2021 7:13 pm 
Offline
Joined: Sat Jun 05, 2004 2:00 pm
Posts: 37
Location: St. Paul, MN
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)


Top
 Profile  
 

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 


All times are UTC - 6 hours




Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group

Theme Created By ceyhansuyu