/ letsencrypt / notify.py
notify.py
 1  """Send e-mail notification to system administrators."""
 2  
 3  import email
 4  import smtplib
 5  import socket
 6  import subprocess
 7  
 8  
 9  def notify(subject, whom, what):
10      """Send email notification.
11  
12      Try to notify the addressee (``whom``) by e-mail, with Subject:
13      defined by ``subject`` and message body by ``what``.
14  
15      """
16      msg = email.message_from_string(what)
17      msg.add_header("From", "Let's Encrypt renewal agent <root>")
18      msg.add_header("To", whom)
19      msg.add_header("Subject", subject)
20      msg = msg.as_string()
21      try:
22          lmtp = smtplib.LMTP()
23          lmtp.connect()
24          lmtp.sendmail("root", [whom], msg)
25      except (smtplib.SMTPHeloError, smtplib.SMTPRecipientsRefused,
26              smtplib.SMTPSenderRefused, smtplib.SMTPDataError, socket.error):
27          # We should try using /usr/sbin/sendmail in this case
28          try:
29              proc = subprocess.Popen(["/usr/sbin/sendmail", "-t"],
30                                      stdin=subprocess.PIPE)
31              proc.communicate(msg)
32          except OSError:
33              return False
34      return True