Skip to content

Instantly share code, notes, and snippets.

@jiacai2050
Created July 29, 2016 08:04
Show Gist options
  • Save jiacai2050/e357e17fbe146137fcce79e30979663a to your computer and use it in GitHub Desktop.
Save jiacai2050/e357e17fbe146137fcce79e30979663a to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from datetime import datetime
from os import path
import smtplib
import sys
import json
def send(fileToSend, subject, conf):
msg = MIMEMultipart()
att1 = MIMEText(open(file_to_send, 'rb').read(), 'base64', 'gb2312')
att1["Content-Type"] = 'application/octet-stream'
filename = path.basename(file_to_send)
att1["Content-Disposition"] = b'attachment; filename=%s' % filename.encode('gb2312')
msg.attach(att1)
text_msg = MIMEText('中文测试', 'plain', 'utf-8')
msg.attach(text_msg)
emailfrom = conf['from']
emailto = conf['to']
password = conf['password']
msg['subject'] = subject
msg['from'] = emailfrom
msg['to'] = emailto
server = smtplib.SMTP()
server.connect(conf['smtp'])
server.login(emailfrom, password)
server.sendmail(emailfrom, emailto, msg.as_string())
server.quit()
if __name__ == "__main__":
args = sys.argv[1:]
file_to_send = args[0]
conf_file = args[1]
subject = "中文测试"
with open(conf_file) as f:
conf = json.load(f)
send(file_to_send, subject, conf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment