Send mail to Gmail programatically with Python


import smtplib from email.MIMEMultipart import MIMEMultipart from email.MIMEBase import MIMEBase from email.MIMEText import MIMEText from email import Encoders import os

gmail_user = "" gmail_pwd = "*******"

def mail(to, subject, text, attach=[]): msg = MIMEMultipart()

msg['From'] = gmail_user msg['To'] = to msg['Subject'] = subject


for file in files: part = MIMEBase('application', "octet-stream") part.set_payload( open(file,"rb").read() ) Encoders.encode_base64(part) part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(file)) msg.attach(part)

mailServer = smtplib.SMTP("", 587) mailServer.ehlo() mailServer.starttls() mailServer.ehlo() mailServer.login(gmail_user, gmail_pwd) mailServer.sendmail(gmail_user, to, msg.as_string()) mailServer.close()

mail("", "Hello from python!", "This is a email sent with python", "attachment1","attachment2")

Share this