Python’la Gmail Üzerinden Mail Gönderme
Eyl 17
![]()
Bu sene yaz stajında Python ile çalışıyordum ve mail göndermek için araştırma yaparken Kutuma’s Ramblings adlı blogda 2007′de yapılmış bir girdi ile karşılaştım. Python ile Gmail üzerinden mail gönderen bu script aynı zamanda mailin eki olarak bir dosya da gönderiyor. Aynen paylaşıyorum:
#!/usr/bin/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 = "your_email@gmail.com"
gmail_pwd = "your_password"
def mail(to, subject, text, attach):
msg = MIMEMultipart()
msg['From'] = gmail_user
msg['To'] = to
msg['Subject'] = subject
msg.attach(MIMEText(text))
part = MIMEBase('application', 'octet-stream')
part.set_payload(open(attach, 'rb').read())
Encoders.encode_base64(part)part.add_header('Content-Disposition','attachment;filename="%s"'% os.path.basename(attach))
msg.attach(part)
mailServer = smtplib.SMTP("smtp.gmail.com", 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(gmail_user, gmail_pwd)
mailServer.sendmail(gmail_user, to, msg.as_string())
# Should be mailServer.quit(), but that crashes...
mailServer.close()
mail("some.person@some.address.com",
"Hello from python!",
"This is a email sent with python",
"my_picture.jpg")

![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=afeb0f3e-a672-4290-820f-3595813430f5)





