Hello, ready to have fun? 🤓
There are more than one way to send email using Python. In this article, I will be sharing the code for one of the ways.
It is using the Python inbuilt
The SMPT(Simple Mail Transfer Protocol) is used by mail servers to send and receive e-mail messages on the internet.
You might wish to create a new account for this fun project, but most importantly to enable this code to work with your email, login to your Google Account and go to My Account > Security > Less Secure App Access and Turn On.
import smtplib message = """From: Me <email@example.com> To:You <firstname.lastname@example.org> MIME-Version: 1.0 Content-type: text/html Subject: <b>Hey, Python is Cool</b> Yes, it's cool cos I'm sending this email to you with the help Python. """ try: ml = smtplib.SMTP('smtp.gmail.com',587) ml.ehlo() ml.starttls() ml.login('email@example.com', 'password') ml.sendmail('', 'firstname.lastname@example.org',message) ml.close() print("Email has been successfully sent") except: print("Email not sent")
Probably you to have much fun and bomb the receiver's mail box, try this:
while True: ml.sendmail('email@example.com', 'message')
It's so cool that you can import
datetime module and write some lines of code to send the email at a specific time.