How to send emails using python – Complete Automation guide

how to send emails using python

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

  1. Sending email  to a sinlge recipient
  2. Sending emails to multiple recipients
  3. 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 = 'example@gmail.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 =['example@gmail.com','love@gmail.com','superman@wars.com',]  
 
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 = "your@gmail.com"
to_mail = "other@gmail.com"

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.

3 thoughts on “How to send emails using python – Complete Automation guide”

  1. Traceback (most recent call last):
    File “email_send.py”, line 6, in
    s = smtplib.SMTP(‘sm58pt.gmail.com’,587)
    File “C:\Users\sony\AppData\Local\Programs\Python\Python38-32\lib\smtplib.py”, line 253, in __init__
    (code, msg) = self.connect(host, port)
    File “C:\Users\sony\AppData\Local\Programs\Python\Python38-32\lib\smtplib.py”, line 339, in connect
    self.sock = self._get_socket(host, port, self.timeout)
    File “C:\Users\sony\AppData\Local\Programs\Python\Python38-32\lib\smtplib.py”, line 308, in _get_socket
    return socket.create_connection((host, port), timeout,
    File “C:\Users\sony\AppData\Local\Programs\Python\Python38-32\lib\socket.py”, line 787, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
    File “C:\Users\sony\AppData\Local\Programs\Python\Python38-32\lib\socket.py”, line 918, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
    socket.gaierror: [Errno 11001] getaddrinfo failed

Leave a Reply