Python/SMTP: Difference between revisions

From Fundamental Ramen
Jump to navigation Jump to search
(Created page with "<source lang="python"> try: HOST = self.config['smtp_host'] PORT = self.config['smtp_port'] TIMEOUT = self.config['smtp_timeout'] with...")
 
No edit summary
 
(8 intermediate revisions by the same user not shown)
Line 1: Line 1:
= Send a message. =
<source lang="python">
<source lang="python">
try:
try:
          HOST = self.config['smtp_host']
    with smtplib.SMTP(HOST, PORT, timeout = TIMEOUT) as smtp:
          PORT = self.config['smtp_port']
        # smtp.set_debuglevel(2)
          TIMEOUT = self.config['smtp_timeout']
        # smtp.starttls()
          with smtplib.SMTP(HOST, PORT, timeout = TIMEOUT) as smtp:
        # smtp.login(USER, PASS)
              FROM = self.config['smtp_mail_from']
        smtp.sendmail(FROM, RCPT, message)
              RCPT = self.config['smtp_rcpt_to']
except smtplib.SMTPConnectError:
              smtp.set_debuglevel(2)
    # Connection refused.
              smtp.sendmail(FROM, RCPT, source)
    pass
          self.smtp_state = SmtpState.CONNECTED
except smtplib.SMTPServerDisconnected:
        except smtplib.SMTPConnectError:
    # Connection reset.
          self.smtp_state = SmtpState.CONN_REFUSED
    pass
          #logger.error('Connection refused.')
except:
        except smtplib.SMTPServerDisconnected:
    # Connection timeout. (not sure)
          self.smtp_state = SmtpState.CONN_TIMEOUT
    pass
          #logger.error('Connection timeout.')
        except:
          self.smtp_state = SmtpState.CONN_UNKNOWN
</source>
</source>

Latest revision as of 09:49, 5 June 2018

Send a message.

try:
    with smtplib.SMTP(HOST, PORT, timeout = TIMEOUT) as smtp:
        # smtp.set_debuglevel(2)
        # smtp.starttls()
        # smtp.login(USER, PASS)
        smtp.sendmail(FROM, RCPT, message)
except smtplib.SMTPConnectError:
    # Connection refused.
    pass
except smtplib.SMTPServerDisconnected:
    # Connection reset.
    pass
except:
    # Connection timeout. (not sure)
    pass