Thursday, 30 May 2019

How to Send Email from Python

Please find the below sample code to send email from Python.


import os
import smtplib

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

msg = MIMEMultipart('alternative')

msg['Subject'] = "Hello from Mandrill, Python style!"
msg['From']    = "Please give From Email Addresss" # Your from name and email address
msg['To']      = "Please give To Email Address"

#text = "Mandrill speaks plaintext"
#part1 = MIMEText(text, 'plain')

html = "<em>Mandrill speaks <strong>HTML</strong></em>"
part2 = MIMEText(html, 'html')

username = "Please give user id of the email account"
password = "Please give password of the email account"

#msg.attach(part1)
msg.attach(part2)

s = smtplib.SMTP('smtp.mandrillapp.com', 587)

s.login(username, password)
s.sendmail(msg['From'], msg['To'], msg.as_string())

s.quit()



No comments:

Post a Comment

How to Send Email from Python

Please find the below sample code to send email from Python. import os import smtplib from email.mime.multipart import MIMEMultipart...