前端之家收集整理的这篇文章主要介绍了
python 发送邮件实现方法,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
感兴趣python
发送邮件实现方法的小伙伴,下面一起跟随编程之家 jb51.cc的小编来看看吧。
# 编程之家 (jb51.cc)
sendmail.py
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import smtplib
import email.MIMEMultipart
import email.MIMEText
import email.MIMEBase
import sys
#from email.mime.application import MIMEApplication
#import os.path
def sendmail(f_from,f_to,f_cclist,alert_info,f_subject):
From = f_from
To = f_to
#file_name = f_file_name
server = smtplib.SMTP("smtp.xxxx.com.cn")
server.login("xxxx","xxxx")
#构造MIMEMultipart对象做为根容器
main_msg = email.MIMEMultipart.MIMEMultipart()
text_msg = email.MIMEText.MIMEText("您好。<br><br><br><br>"
+ alert_info.title() +
"<br>任凤军 <br>"
"xx技术股份有限公司 <br>"
"手机: xx<br>"
"座机:xxx<br>"
"邮箱:xxxx@xx.com<br>"
"地址:xxxx<br>"
"邮编:130011<br>"
"===================================<br>"
"",'HTML','utf-8')
main_msg.attach(text_msg)
#xlsxpart = MIMEApplication(open(file_name,'rb').read())
#xlsxpart.add_header('Content-Disposition','attachment',filename=f_subject+".docx")
#main_msg.attach(xlsxpart)
# 设置根容器属性
main_msg['From'] = From
main_msg['To'] = To
main_msg['Cc'] = ",".join(f_cclist)
main_msg['Subject'] = f_subject
main_msg['Date'] = email.Utils.formatdate()
#f_cclist为完整的需要接收邮件的列表,原本只存放抄送列表,这里需要添加上收件人
f_cclist.append(To)
# 得到格式化后的完整文本
fullText = main_msg.as_string()
# 用smtp发送邮件
try:
server.sendmail(From,fullText)
finally:
server.quit()
if __name__ == "__main__":
#sys.setdefaultencoding('utf-8')
message= [
'Usage:',' sendmail.py "topic" "mail body text" "mail to"','Examples of usage:',' sendmail.py "topic" "hello world" "14638852@qq.com"',]
try:
topic = str(sys.argv[1]).encode("utf-8")
alert = str(sys.argv[2]).encode("utf-8")
mailto = str(sys.argv[3]).encode("utf-8")
except IndexError:
for line in message:
print line+'\n'
sys.exit()
cclist=[]
#clist =[]
sendmail("xxxx@xxx",mailto,cclist,alert,topic)
备注:
sendmail("xxxx@gmail.com",topic)
发件人,收件人,抄送列表,正文内容,邮件标题
Usage:
sendmail.py "topic" "mail body text" "mail to"
Examples of usage:
sendmail.py "topic" "hello world" "14638852@qq.com"
./sendmail.py "topic" "hello world" "14638852@qq.com"
# End 512.笔记 jb51.cc
smtp以及
邮件的签名,还有发件人为定值,需要自己
修改。