指定邮件编码
msg = MIMEText(content,'html', 'utf-8')
msg.add_header("Content-Type",'text/plain; charset="utf-8"')
#!/usr/bin/env python
#coding:utf-8
import smtplib
from email.mime.text import MIMEText
import sys
mail_host = 'smtp.qq.com'
mail_user = '2219722370'
mail_pass = 'tyztnlzandbbeahj'
mail_postfix = 'qq.com'
def send_mail(to_list,subject,content):
me = mail_user + "<" + mail_user + "@" + mail_postfix + ">"
msg = MIMEText(content)
msg['Subject'] = subject
msg['From'] = me
msg['To'] = ",".join(to_list)
try:
s = smtplib.SMTP() #创建SMTP对象
s.connect(mail_host,"25") #通过connect方法连接smtp主机
s.starttls() #启用TLS(安全传输)模式,所有SMTP指令都将加密传输,例如使用gmail的smtp服务时需要启动此项才能正常发送邮件
s.login(mail_user,mail_pass)
s.sendmail(me,to_list,msg.as_string())
s.quit() #断开smtp连接
print "邮件发送成功!"
return True
except Exception as e:
print "失败:" + str(e)
return False
if __name__ == "__main__":
#send_mail(sys.argv[1], sys.argv[2], sys.argv[3])
to_list = ["[email protected]", "[email protected]"]
send_mail(to_list,"title","msg")
#附件
#!/usr/bin/env python
#coding:utf-8
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import sys
mail_host = 'smtp.qq.com'
mail_user = '2219722370'
mail_pass = 'tyztnlzandbbeahj'
mail_postfix = 'qq.com'
def send_mail(to_list,subject,content):
me = mail_user + "<" + mail_user + "@" + mail_postfix + ">"
msg = MIMEMultipart('related')
msgtext = MIMEText(content)
msg['Subject'] = subject
msg['From'] = me
msg['To'] = to_list
attach = MIMEText(open("/etc/passwd","rb").read(),"base64","utf-8") #附件
attach["Content-Disposition"] =
"attachment; filename=\"业务服务质量周报(12周).xlsx\"".decode("utf-8").encode("gb18030") #附件名
msg.attach(msgtext) #MIMEMultipart对象附加MIMEText的内容,邮件内容
msg.attach(attach)
try:
s = smtplib.SMTP() #创建SMTP对象
s.connect(mail_host,"25") #通过connect方法连接smtp主机
s.starttls() #启用TLS(安全传输)模式,所有SMTP指令都将加密传输,例如使用gmail的smtp服务时需要启动此项才能正常发送邮件
s.login(mail_user,mail_pass)
s.sendmail(me,to_list,msg.as_string())
s.quit() #断开smtp连接
print "邮件发送成功!"
return True
except Exception as e:
print "失败:" + str(e)
return False
if __name__ == "__main__":
#send_mail(sys.argv[1], sys.argv[2], sys.argv[3])
send_mail("[email protected]","subject","msg")