How to Send Mail Using Gmail in Python

What is SMTP:-

  • SMTP stands for Simple Mail Transfer Protocol. SMTP is a set of communication guidelines that allow software to transmit an electronic mail over the internet is called Simple Mail Transfer Protocol. It is a program used for sending messages to other computer users based on e-mail addresses.
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def MailSending():

mail_content = '''Hello,This is Sample Mail which is Sent Using Python"
'''
#The mail addresses and password
sender_address = "Sender's Email Address"
sender_pass = 'password'
receiver_address = 'receivers email address'
#Setup the MIME
message = MIMEMultipart()
message['From'] = sender_address
message['To'] = receiver_address
message['Subject'] = 'A test mail sent by Python. ' #The subject line
#The body and the attachments for the mail
message.attach(MIMEText(mail_content, 'plain'))
#Create SMTP session for sending the mail
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')
def main():
print("Demonstration of Email Sending Using Gmail in Python ")
MailSending()
if __name__=="__main__":
main()
  • The smtplib module is used to send mail to any internet machine with SMTP.
class smtplib.SMTP(host='', port=0, local_hostname=None, [timeout, ]source_address=None)
  • Where we have to provide host and port as a parameter.
  • We can provide remaining parameter as well but there is no complusion as these are keyword argument and if we don’t provide that it will take its default value.
  • If the optional host and port parameters are given, the SMTP connect() method is called with those parameters during initialization.
  • For normal use, you should only require the initialization/connect(), sendmail(), and SMTP.quit() methods. An example is included below.
  • Connect():-
SMTP.connect(host='localhost', port=0)
  • This Method connects the host on the given port.
  • Sendmail():-
SMTP.sendmail(from_addr, to_addrs, msg, mail_options=(), rcpt_options=())
  • To send an email SMTP.sendmail is used and from_address,to_address and msg these three are the mandatory fields.
  • Quit():-
SMTP.quit()
  • Terminate the SMTP session and close the connection. Return the result of the SMTP QUIT command.
  • from email.mime.multipart we have to import MIMEMultipart

What is MIMEMultipart:-

  • MIMEMultipart is for saying “I have more than one part”, and then listing the parts — you do that if you have attachments, you also do it to provide alternative versions of the same content.
class email.mime.multipart.MIMEMultipart(_subtype='mixed', boundary=None, _subparts=None, *, policy=compat32, **_params)
  • All the parameters are default. You can specify if you want to specify it explicitly else there is no need.
class email.mime.text.MIMEText(_text, _subtype='plain', _charset=None, *, policy=compat32)
  • The MIMEText class is used to create MIME objects of major type text.
  • Only one parameter is needed rest all parameters are default.
SMTP.login(user, password, *, initial_response_ok=True)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store