Change language

Sending mail from your Gmail account using Python

| |

Steps to send mail from your Gmail account:

  1. First of all, you need to import the "smtplib" library.
  2. After that, to create session, we will use its SMTP instance to encapsulate the SMTP connection.
     s = smtplib.SMTP (’smtp.gmail.com’, 587) 

    In this case, you need to pass the first parameter of the server location and the second the port parameter to be used. For Gmail, we use port number 587.

  3. For security reasons, set your SMTP connection to TLS mode. TLS (Transport Layer Security) encrypts all SMTP commands. After that, for security and authentication, you need to pass your Gmail account credentials to the login instance. 
    The compiler will show an authentication error if you enter an invalid email id or password.
  4. Save the message you need to send in a variable, say message. Using a sendmail () instance, send your message. sendmail () uses three parameters: sender_email_id, receive_email_id, and message_to_be_sent . The parameters must be in the same sequence.

This will send an email from your account. Once you have completed your task, end your SMTP session with quit ().

# Python code for illustration Sending mail from
# your Gmail account

import smtplib

 
# creates an SMTP session

s = smtplib.SMTP ( ’ smtp.gmail.com’ , 587 )

 
# trigger TLS for security
s.starttls ()

 
# Authentication < / code>

s.login ( "sender_email_id" , "sender_email_id_password" )

 
# message to send

message = "Message_you_need_to_send"

 
# sending mail

s.sendmail ( " sender_email_id " , "receiver_email_id" , message)

 
# terminating the session
s.quit ()

Sending one message to multiple people yam

If you need to send the same message to different people. You can use to loop for this. 
For example, you have a list of email ids to which you need to send the same mail. To do this, insert a for loop between initialization and termination of the SMTP session. The loop will be initialized in turn, and after the email is sent, the SMTP session will be terminated.

# Python code for illustration Sending mail
# multiple users
# from your Gmail account

import smtplib

 
# list of email_id to send mail

li = [ " [email protected] " , " yyyyy @ gmail.com " ]

  

for i in range ( len ( li)):

s = smtplib.SMTP ( ’smtp.gmail.com’ , 587 )

s.starttls ()

s.login ( "sender_email_id" , "sender_email_id_password" )

  message = "Message_you_need_to_send"

s.sendmail ( "sender_email_id" , li [i], message)

  s.quit ()

Important points:

  • This code can send simple mail that has no attachments or any subject .
  • One of the most amazing things about this code is that we can send any number of emails with this, and Gmail always puts your mail in the main section. Sent emails will not be spam.
  • File processing can also be used to retrieve an email ID from a file and then send emails.

Next:

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically