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
Copy link

untitled

This code isn't working.

@shreyanshu7101904
Copy link

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

@deeproverb
Copy link

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

@Vishal9619
Copy link

`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
Copy link

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
Copy link

That helped a lot. Many thanks. =]

@hotheadhacker
Copy link

How to add Bcc?

@hotheadhacker
Copy link

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