def sendmail(sender, recipient, subject, body, server='localhost'):
    """Sends an e-mail to the specified recipient."""
body = '<html><head></head><body><pre>%s</pre></body></html>' % body.replace(
    '&', '&').replace('<', '<')
headers = ['From: ' + sender, 'Subject: ' + subject, 'To: ' + recipient,
    'MIME-Version: 1.0', 'Content-Type: text/html']
headers = '\r\n'.join(headers)
session = smtplib.SMTP(server)
session.sendmail(sender, recipient, headers + '\r\n\r\n' + body)
session.quit()
Отправка электронной почты из gmail с помощью Python