Kirim email via gmail menggunakan python

April 25, 2011

1 min read.


import smtplib
from email.MIMEText import MIMEText

gmail_user = "seseorang@gmail.com"
gmail_password = "password"

def kirim_email(ke, subject, pesan):
pesan_email = MIMEText(pesan)
pesan_email['From'] = gmail_user
pesan_email['To'] = ke
pesan_email['Subject'] = subject

server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login(gmail_user, gmail_password)
server.sendmail(gmail_user, [ke], pesan_email.as_string())
server.close()

if __name__ == '__main__':
kirim_email('someone@yahoo.com', 'testing',
'testing kirim pesan dari python')