9951 explained code solutions for 126 technologies


pythonHow to send email using gmail


import smtplib

to = '[email protected]'
subject = 'Hi!'
body = 'Hi again!'

email_text = """\
From: %s
To: %s
Subject: %s

%s
""" % ('[email protected]', to, subject, body)

server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.ehlo()
server.login('[email protected]', 'pwd')
server.sendmail('[email protected]', to, email_text)
server.close()ctrl + c
import smtplib

library to work with SMTP servers

[email protected]

gmail login (and sender address)

'pwd'

gmail password

[email protected]

receiver address

email_text

formatted email message to send through SMTP

smtplib.SMTP_SSL(

connects to gmail SMTP

server.ehlo()

handshake with SMTP

server.login(

authenticate our gmail account

server.sendmail(

sends email

server.close()

closes connection to SMTP server