Application of smtplib module

Author - Neelima Mohanty
20/01/2022 | 10:30 PM

The smtplib module

The smtplib is a Python library for sending emails using the Simple Mail Transfer Protocol (SMTP). The smtplib is a built-in module; we do not need to install it. It abstracts away all the complexities of SMTP. Simple Mail Transfer Protocol (SMTP) is a communication protocol for electronic mail transmission. Is is an Internet standard, which was first defined in 1982 by RFC 821, and updated in 2008 by RFC 5321 to Extended SMTP additions. Mail servers and other message transfer agents use SMTP to send and receive mail messages.
In this post , I will explain you one of the major application of this python module i.e,sending email by performing a small task.



Prerequisites

Before following this tutorial, you need the following:

Note: Avoid using Gmail, because it is a highly secured server and it is quite complex to make it work.Instead, use development servers or a shared webhosting server.



Creating an SMTP object

1. This is a simple syntax to create one SMTP object, which can later be used to send an e-mail :

import smtplib
smtpObj = smtplib.SMTP( [host [, port [, local_hostname]]] )
Details of the parameters −
•host − This is the host running your SMTP server. You can specify IP address of the host or a domain name like tutorialspoint.com. This is optional argument.
•port − If you are providing host argument, then you need to specify a port, where SMTP server is listening. Usually this port would be 25.
•local_hostname − If your SMTP server is running on your local machine, then you can specify just localhost as of this option.
An SMTP object has an instance method called sendmail, which is typically used to do the work of mailing a message. It takes three parameters −
•The sender − A string with the address of the sender.
•The receivers − A list of strings, one for each recipient.
•The message − A message as a string formatted as specified in the various RFCs.

2. Open your code editor, and create a file named app.py.


3. Then open the file and type the following code in it :

import smtplib from tkinter import *
def send_message():
address_info = address.get()
email_body_info = email_body.get()
print(address_info,email_body_info)
sender_email = "type your email ID"
sender_password = "type your password"
server = smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login(sender_email,sender_password)
print("Login successful")
server.sendmail(sender_email,address_info,email_body_info)
print("Message sent")
address_entry.delete(0,END)
email_body_entry.delete(0,END)
app = Tk()
app.geometry("500x500")
app.title("Python Mail Send App")
heading = Label(text="Python Email Sending App",bg="yellow",fg="black",font="10",width="500",height="3")
heading.pack()
address_field = Label(text="Recipient Address :")
email_body_field = Label(text="Message :")
address_field.place(x=15,y=70)
email_body_field.place(x=15,y=140)
address = StringVar()
email_body = StringVar()
address_entry = Entry(textvariable=address,width="30")
email_body_entry = Entry(textvariable=email_body,width="30")
address_entry.place(x=15,y=100)
email_body_entry.place(x=15,y=180)
button = Button(app,text="Send Message",command=send_message,width="30",height="2",bg="grey")
button.place(x=15,y=220)
mainloop()

4.Now , type the following command in the terminal:

python app.py

The following application would appear.

Type in the Recipient's E-mail address and the message to be delivered and click Send Message button.



Conclusion

Check the recipient's inbox to find the sent mail.Happy learning.


People who read this also read

article

QuackChat – Creating a Real Time Chat App

Author: Neelima Mohanty
10/01/2022 | 10:30 PM