##!/usr/bin/env python
# -*- coding: utf8 -*-
#此脚本为发送此邮件的脚本程序
#导入smtplib和MIMEText
import os,smtplib,mimetypes
from email.mime.text import MIMEText
from email.mime.p_w_picpath import MIMEImage
from email.mime.multipart import MIMEMultipart
#############
#要发给谁,这里指定邮箱
mailto_list1=["xxxxx@xxx.com"]
#####################
#设置服务器,用户名、口令以及邮箱的后缀
mail_host="smtp.xxx.com"
mail_user="xxxxx"
mail_pass="xxxxx"
mail_postfix="xxx.com"
######################
def send_mail(to_list1,sub,content,filename=None):
'''
to_list:发给谁
sub:主题
content:内容
send_mail("aaa@126.com","sub","content")
'''
me=mail_user+"<"+mail_user+"@"+mail_postfix+">"
# msg = MIMEText(content,_charset='utf-8')
msg=MIMEMultipart()
msg.attach(MIMEText(content,_charset='utf-8'))
msg['Subject'] = sub
msg['From'] = me
msg['To'] = ";".join(to_list1)
if filename != None and os.path.exists(filename):
ctype, encoding = mimetypes.guess_type(filename)
if ctype is None or encoding is not None:
ctype = "application/octet-stream"
maintype, subtype = ctype.split("/", 1)
p_w_upload = MIMEImage((lambda f: (f.read(), f.close()))(open(filename, "rb"))[0], _subtype = subtype)
p_w_upload.add_header("Content-Disposition", "p_w_upload", filename = filename)
msg.attach(p_w_upload)
try:
s = smtplib.SMTP()
s.connect(mail_host)
s.login(mail_user,mail_pass)
s.sendmail(me, to_list1, msg.as_string())
s.close()
return True
except Exception, e:
print str(e)
return False
if __name__ == '__main__':
# cwd="cat /root/python/20121114/mail.py"
# p=os.popen(cwd).read()
if send_mail(mailto_list1,"python测试邮件","测试邮件",r"e:\p_w_upload.rar"):
print "发送成功"
else:
print "发送失败"
使用python发送带附件的邮件
精选 转载下一篇:python ftp测试
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Python 发送邮件 和 发送带附件邮件
平时运行一些脚本,需要把结果发送到邮箱,可以用python来处理,如下:1,有邮
sendmail mail python 邮件服务器 发送邮件 -
Python 发送带 附件 邮件 脚本
Python 发送普通邮件的脚本参考: Python 发送 RMAN 备份 Log 脚本 h
python encoding email import 脚本