Email Sending by Python
#Simple email
import smtplib
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login("client_email@gmail.com", "password")
msg = "Email Sending by python"
server.sendmail("from@gmail.com", "to@gmail.com", msg)
server.quit()
#Email with subject and attachment
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEBase import MIMEBase
from email import encoders
fromaddr = "from@gmail.com"
toaddr = "to@gmail.com"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Subject - Eamil subject"
body = "Email body message"
msg.attach(MIMEText(body, 'plain'))
filename = "File name"
attachment = open("file source", "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
msg.attach(part)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, "Password")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
import smtplib
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login("client_email@gmail.com", "password")
msg = "Email Sending by python"
server.sendmail("from@gmail.com", "to@gmail.com", msg)
server.quit()
#Email with subject and attachment
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEBase import MIMEBase
from email import encoders
fromaddr = "from@gmail.com"
toaddr = "to@gmail.com"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Subject - Eamil subject"
body = "Email body message"
msg.attach(MIMEText(body, 'plain'))
filename = "File name"
attachment = open("file source", "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
msg.attach(part)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, "Password")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
Comments
Post a Comment