Skip to content

Instantly share code, notes, and snippets.

@kmonsoor
Created October 3, 2017 13:36
Show Gist options
  • Select an option

  • Save kmonsoor/ec45a604438895c0ca074ca1666bbeb2 to your computer and use it in GitHub Desktop.

Select an option

Save kmonsoor/ec45a604438895c0ca074ca1666bbeb2 to your computer and use it in GitHub Desktop.

Revisions

  1. Khaled Monsoor created this gist Oct 3, 2017.
    24 changes: 24 additions & 0 deletions smtp_send_mail.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    # src: https://stackoverflow.com/a/12424439/617185

    def send_email(user, password, recipient, subject, body):
    import smtplib

    FROM = user
    TO = recipient if type(recipient) is list else [recipient]
    SUBJECT = subject
    TEXT = body

    # Format the email body
    message = """From: %s\nTo: %s\nSubject: %s\n\n%s
    """ % (FROM, ", ".join(TO), SUBJECT, TEXT)

    try:
    server_ssl = smtplib.SMTP_SSL("smtp.gmail.com", 465)
    server_ssl.login(FROM, password)
    server_ssl.sendmail(FROM, TO, message)
    server_ssl.close()
    except Exception as e:
    print("failed to send mail")
    print(e)
    else:
    print 'successfully sent the mail'