Send email with SMTPlib

    0

    3

    email
    Python public recipes

    Basic email sending with smtplib

    import smtplib
    import logging
    
    def send_email(server_host, server_port, server_username, server_password, fromaddr, toaddrs, messsage):
      """
      Send an email using SMTLlib
      :param server_host: host of the server to send email from
      :param server_port: port to connect to
      :param server_username: username to authenticate to the server
      :param server_password: password to authenticate to
      :param fromaddr: The email use for the From:
      :param toaddrs: all recipinents
      :param message: message to send
      """
      try:
        server = smtplib.SMTP(server_host, int(server_port))
        server.ehlo()
        if use_tls:
          server.starttls()
         server.ehlo()
         server.login(server_username, server_password)
    
         server.sendmail(fromaddr, toaddrs, message)
         server.quit()
     except smtplib.SMTPServerDisconnected:
       logging.exception("Cannot send email to %s", toaddrs)
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Explore
    • Cookbooks
    • Playground
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.