How to Send mails using Python script?

0
187
How to Send mails using Python script?

Introduction To Send mails Using Python Script

Send mails using Python

In this blog, we will learn how to send mails using the python library. To send mail using python we need an external library called SMTPlib which comes with python. It uses SMTP ( Simple Mail Transfer protocol ) To send mail using python programming. 

It create an SMTP client session object for mailing. 

To sent mail using SMTP needs valid source and destination email Ids and port number. The port number is different for different sites for Gmail the port is 587.

In this blog, we are learning how to Send mails using Python, we are using google Gmail service to send mail. So for that, we need to import some settings (if required) for google security purposes. If those settings are not set up, then the following code may not work, if Google doesn’t support access from a third-party app. 

To allow the access we need ‘Less Secure App Access’ setting in the google account.  If the two-step verification is on, we cannot use the less secure access.

To complete this setup, go to the Google’s Admin Console, and search for the Less Secure App setup.

Or Go to the following link and enable the less secure access.

To send mail first, we need to import the module to send mail.

import smtplib

Here in the module also using the MIME (Multipurpose Internet Mail Extension) module to make it more flexible. Using the MIME header, we can store the sender and receiver information and also some other details.

One to one email sender

Let’s start by creating a simple script that sends one text email to other people from your account 

# Code Without Attachment of any file 

# Python code to illustrate Sending mail from  
# your Gmail account  
import smtplib 
  
# creates SMTP session 
s = smtplib.SMTP('smtp.gmail.com', 587) 
  
# start TLS for security 
s.starttls() 
  
# Authentication 
s.login("Sender mail Id", "Sender mail password") 
  
# message to be sent 
message = "This mail is sent by using python script SMTP library "
  
# sending the mail 
s.sendmail("Sender Mail Id", "Receiver Mail Id", message) 
  
# terminating the session 
s.quit() 

print('Mail Sent’)

One to one email sender with attachment

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase 
from email import encoders 


#The mail addresses and password
sender_address = 'Enter Sender Mail Address'
sender_pass = 'Enter Sender Mail Password '
receiver_address = 'Enter Receiver Mail Address'


#Setup the MIME( Multipurpose Internet Mail Extension )
message = MIMEMultipart()
message['From'] = sender_address
message['To'] = receiver_address
message['Subject'] = 'Enter the Subject Of Mail '     #The subject line


mail_content = 'Enter Mail Content'

# Ex:- '''Hello, This is a simple mail. There is an attachment in the mail, 
#                The mail is sent using Python SMTP library with an attachment of file. Thank You'''


#The body and the attachments for the mail
message.attach(MIMEText(mail_content, 'plain'))
#Create SMTP session for sending the mail
# open the file to be sent  
filename = 'Enter File Name With Extension To Attchment'

# Open PDF file in binary mode
# We assume that the file is in the directory is at the same location where you run your Python script from

with open(filename, "rb") as attachment:
    # MIME attachment is a binary file So we pass content type "application/octet-stream" 
    part = MIMEBase("application", "octet-stream")
    part.set_payload(attachment.read())
# encode into base64 
encoders.encode_base64(part) 
   
part.add_header('Content-Disposition', "attachment; filename= %s" % filename) 


# attach the instance 'part' to instance 'message' 
message.attach(part) 

    
session = smtplib.SMTP('smtp.gmail.com', 587) #use gmail with port
session.starttls() #enable security
session.login(sender_address, sender_pass) #login with mail_id and password
text = message.as_string()
session.sendmail(sender_address, receiver_address, text)
session.quit()
print('Mail Sent')

Conclusion

In this blog, you get an understanding of how to send mail by using python programming with attachment. 

LEAVE A REPLY

Please enter your comment!
Please enter your name here