Skip to content

Instantly share code, notes, and snippets.

@cuongdcdev
Last active December 25, 2015 16:12
Show Gist options
  • Save cuongdcdev/acb9e65baf8b0f433a65 to your computer and use it in GitHub Desktop.
Save cuongdcdev/acb9e65baf8b0f433a65 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# -*- coding: utf-8 -*-
# source code : https://gist.github.com/dbieber/5146518
import getpass
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 login(user):
global gmail_user, gmail_pwd
gmail_user = raw_input('nhập TÊN TÀI KHỎAN GMAIL __ ko có @gmail.com đâu nhá __ của người gửi vào đây : ')
gmail_pwd = getpass.getpass('nhập mật khẩu tài khoản của %s: ' % gmail_user)
def mail(to, subject, text, attach=None):
msg = MIMEMultipart()
msg['From'] = gmail_user
msg['To'] = to
msg['Subject'] = subject
msg.attach(MIMEText(text))
if attach:
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())
mailServer.close()
# Example!
def example():
login(gmail_user)
send_to = raw_input("nhập địa chỉ email đầy đủ của người nhận __ khuyến khích dùng Gmail __ : ")
title = raw_input('nhập tiêu đề mail vào đây thím :')
body = raw_input('nhập nội dung của email vào đây : ')
mail(send_to, title, body)
example()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment