import smtplib my_email = "email@example.com" password = "bjorn123" with smtplib.SMTP("smtp.gmail.com", port=587) as connection: connection.starttls() connection.login(user=my_email, password=password) connection.sendmail(from_addr=my_email, to_addrs="firstname.lastname@example.org", msg="Subject:Hello\n\nThis is the body of the email") connection.close()
The code begins by creating an instance of the SMTP object. Then, it sets up the necessary parameters, including the hostname (smtp.gmail.com) and port number (587). It also sets up the authentication credentials for the user (my_email and password) and finally sends the email. Finally, it closes the SMTP connection.