Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Send an email with a gmail account using python 3
# smtplib module send mail
import smtplib
TO = 'recipient@mailservice.com'
SUBJECT = 'TEST MAIL'
TEXT = 'Here is a message from python.'
# Gmail Sign In
gmail_sender = 'sender@gmail.com'
gmail_passwd = 'password'
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login(gmail_sender, gmail_passwd)
BODY = '\r\n'.join(['To: %s' % TO,
'From: %s' % gmail_sender,
'Subject: %s' % SUBJECT,
'', TEXT])
try:
server.sendmail(gmail_sender, [TO], BODY)
print ('email sent')
except:
print ('error sending mail')
server.quit()
@SoleCodr

This comment has been minimized.

Copy link

@SoleCodr SoleCodr commented Jun 25, 2018

untitled

This code isn't working.

@shreyanshu7101904

This comment has been minimized.

Copy link

@shreyanshu7101904 shreyanshu7101904 commented Jul 25, 2018

try to change the name of your file instead of mail.py

@deeproverb

This comment has been minimized.

Copy link

@deeproverb deeproverb commented Jul 28, 2018

Do you have a python script for sending a personalized email to a group of emails one at a time

@Vishal9619

This comment has been minimized.

Copy link

@Vishal9619 Vishal9619 commented Dec 14, 2018

`import smtplib
li = ["xxxxx@gmail.com", "yyyyy@gmail.com"] #list of email_id to send the mail

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() `

@jricardodeleon

This comment has been minimized.

Copy link

@jricardodeleon jricardodeleon commented Apr 15, 2019

This is regarding multiple emails at once:
---- List of emials:
TO = "email1@gmail.com,email2@hotmail.com,email3@gmail.com"

---- change this line :
"s.sendmail("sender_email_id", li[i], message)"

---- For this line:
s.sendmail(sender_email_id, TO.split(","), message)

@ragnarbled777

This comment has been minimized.

Copy link

@ragnarbled777 ragnarbled777 commented Oct 18, 2019

That helped a lot. Many thanks. =]

@hotheadhacker

This comment has been minimized.

Copy link

@hotheadhacker hotheadhacker commented May 16, 2020

How to add Bcc?

@hotheadhacker

This comment has been minimized.

Copy link

@hotheadhacker hotheadhacker commented May 16, 2020

Done I found a Way:

BODY = '\r\n'.join([ 'From: %s' % gmail_sender, 'Subject: %s' % SUBJECT, '', TEXT])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment