Skip to content

Instantly share code, notes, and snippets.

@zaman
Created December 8, 2012 02:53
Show Gist options
  • Save zaman/4238337 to your computer and use it in GitHub Desktop.
Save zaman/4238337 to your computer and use it in GitHub Desktop.
send email with python
#!/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 = "[email protected]"
gmail_pwd = "SIFRE"
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("[email protected]",
"buraya mail konusu!",
"mail govdesi",
"image.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment