Welcome you, to another Interesting project in python that sends emails using python. I am excited to teach you about this project.
Recently I observe the mailing systems online, they are charging a little price to send bulk emails and some mail servers allow free services. so I decided to build my own mail sending system using python that delivers bulk emails at a time to my youtube & blog subscribers.
What you learn here
I planned this tutorial as step by step guide, so you will learn
- Sending email to a sinlge recipient
- Sending emails to multiple recipients
- Sending emails from a file
so are you ready to learn the exciting stuff here..? of course yes I think.
Check out my amazing machine learning project on corona patients prediction
1. Sending email to a single recipient using python
To send an email using python we use the SMTP module in python, its already in your system if you installed python.
import smtplib from cred import my_mail,my_pwd s = smtplib.SMTP('smtp.gmail.com',587) #host, port s.starttls() s.login(my_mail,my_pwd) message = 'hello this a test email' #write your message here reciver_mail = 'email@example.com' # reciever email s.sendmail(my_mail,reciver_mail,message) s.quit() print('successfully sent..!')
starttls(): This method provides security to our mail system.
smtplib.SMTP(‘smtp.gmail.com’,587): Here we used Gmail was our hosting server to send emails and port is 587. you can use your email hosting servers also.
login(): Allows to login to our Gmail account with our Gmail and password.
sendmail(): It sends an email to the recipient, it allows parameters sender_email, reciever_email, message and etc, check out the documentation.
Note: To send emails using Gmail host, you must allow the permissions, otherwise it may not work. To allow permissions check here.
In the same way, you can use other email hosting services.
2. Sending emails to multiple recipients using python
To send emails to multiple recipients we just modify a single line, not using any for loops.
import smtplib from cred import my_mail,my_pwd s = smtplib.SMTP('smtp.gmail.com',587) #host, port s.starttls() s.login(my_mail,my_pwd) message = 'hello this a test email' #write your message here reciver_mail =['firstname.lastname@example.org','email@example.com','firstname.lastname@example.org',] s.sendmail(my_mail,reciver_mail,message) s.quit() print('successfully sent..!')
here we are sending an email to a list of recipients here. so we passed a list of recipient emails in reciever_email.
3. Sending emails from a file using python
Now its time to move a little advance in sending emails using python. sometimes we get emails in a file, so we need to send emails to recipients of that file.
import smtplib from cred import my_mail,my_pwd # this a module to hold our password and email secretely in another file, here we are importing them, you can try your own. s = smtplib.SMTP('smtp.gmail.com',587) s.starttls() s.login(my_mail,my_pwd) message = 'hello this a check for sending emails from a file using python' reciver_mail = with open('emails.txt','r') as f: d = f.read() reciver_mail.append(d.replace('\n','')) f.close() print(reciver_mail) s.sendmail(my_mail,reciver_mail,message) s.quit() print('successfully sent..!')
In the above code, I get emails from emails.txt, that file containing a list of emails in the below format. if you got any different format then use python regular expressions.
Sending emails using python with subject
In all these methods we send emails without subject, so we need to send emails using python with the subject also. so let’s do that also.
import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText s = smtplib.SMTP('smtp.gmail.com',587) s.starttls() from_mail = "email@example.com" to_mail = "firstname.lastname@example.org" s.login(from_mail, "your password") msg = MIMEMultipart() msg['From'] = from_mail msg['To'] = to_mail msg['Subject'] = "I love you" # here we are adding subject body = "I am just joking..!" msg.attach(MIMEText(body, 'plain')) # Message text = msg.as_string() s.sendmail(from_mail, to_mail, text) s.quit() print('Successfully sent..!')
Successfully we send emails using Python with the subject. To add the subject we import MIMEMultipart and MIMEText.
I hope you get this clearly, so if like it please do comment below and for more interesting tutorials, do subscribe to our newsletter.