Hackerss.com

hackerss
hackerss

Posted on

Send an email in Python

Send an email - Python:


import smtplib

def send_email(subject, msg):
    try:
        server = smtplib.SMTP('smtp.gmail.com:587')
        server.ehlo()
        server.starttls()
        server.login("youremailusername", "password")
        message = 'Subject: {}\n\n{}'.format(subject, msg)
        server.sendmail("[email protected]", "[email protected]", message)
        server.quit()
        print("Success: Email sent!")
    except:
        print("Email failed to send.")

subject = "An email with attachment from Python"
msg = "This is an email with attachment sent from Python"

send_email(subject, msg)
Enter fullscreen mode Exit fullscreen mode

Latest comments (0)