Python + Sendmail

Another interesting solution is to use the smtplib library, but I will discuss only the usage of sendmail tool with subprocess.

The interesting of this is that I’m using subprocess library instead of os or popen.

After some issues on how to send input to the child process the reference below helped me to understand the way to do it, so I easily implemented this with sendmail.

from subprocess import Popen, PIPE, STDOUT

SENDMAIL = '/usr/sbin/sendmail'

def sendMail(from_address, to_address_list, subject='', msg=''):
"""Sending Email using sendmail tool."""
  to_address = ', '.join(to_address_list)
  content = ("From: %snTo: %snSubject: %snn%s" %(from_address, to_address, subject, msg))
  sendmail = Popen([SENDMAIL, '-t', '-f', from_address], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
  sendmail_mail = sendmail.communicate(content)[0]

References:
StackOverflow Python