#!/usr/bin/python
#-*-Coding:utf-8-*-#此处支持中文字符一定要放在前两行, after the words do not take effect, the foot of the newspaper wrong
#!/usr/bin/env python
Import Smtplib
From Email.mime.text import Mimetext
Import string
Import time
Import datetime
HOST = "Mail.xxxxxx.com" #smtp主机地址
subject_1 = "xxx data push" #邮件标题
Subject_1=unicode (subject_1, ' utf-8 ') #转码成中文, otherwise push to mailbox display as garbled
to = [' [email protected] ', ' [email protected] '] #定义邮件收件人
from = "[email protected]" #定义邮件发件人
Time_now = Time.strftime ("%y%m%d", Time.localtime ()) #获取系统当前时间格式为年月日
Now_time = Datetime.datetime.now () #
Time = Now_time + Datetime.timedelta (days=-1) #
Time_yes = Time.strftime ('%y%m%d ') #获取系统昨日时间, formatted as Month day
Title= "Xxx_" +time_yes+ "_12:00-" +time_now+ "_12:00" + ". csv" #定义邮件文件名, here is used to automatically get yesterday's date and today's date as part of the file name, the effect is:
xxx_20160708_1500-20160710_1200
#创建一个MIMEText对象, attaching name.txt documents
attach_1 = Mimetext (Open ("Name.txt", "RB"). Read (), "base64", "Utf-8") #打开name. txt files
attach_1["Content-type"] = "Application/octet-stream" #指定文件格式类型
#指定Content the-disposition value is attachment the Download Save dialog box appears, and the saved default file name is specified using the filename
attach_1["content-disposition"] = "attachment; Filename= "+title
#比如qqmail使用gb18030页面编码, in order to ensure the receipt of the message, the Chinese file name does not appear garbled, you need to encode the file name conversion
Decode ("Utf-8"). Encode ("GB18030")
msg_1 = Mimemultipart (' related ')
Msg_1.attach (attach_1) #MIMEMultipart对象附加MIMEText附件内容
msg_1[' Subject '] = subject_1 #邮件主题
msg_1[' from ') = from #邮件发件人, mailbox header visible
#msg_1 [' to '] = to #邮件收件人, message header visible, optional
Try
Server = Smtplib. SMTP () #创建一个SMTP () object
Server.connect (HOST, "#通过connect方法连接smtp主机")
Server.starttls () #启动安全传输模式
Server.login (' name ', ' MyPassword ') #邮箱账号登录校验
Server.sendmail (From,to,msg_1.as_string ()) #邮件发送
Server.quit () #断开smtp连接
Print "success!"
Except Exception, E:
Print "Fail:" +str (E)
This article is from the "one Gourd soy sauce" blog, please be sure to keep this source http://zhushenghang.blog.51cto.com/11105203/1825371
Python script--auto push txt,excel and other file templates