python 发送邮件实现方法

python 发送邮件实现方法,第1张

概述python 发送邮件实现方法 感兴趣python 发送邮件实现方法的小伙伴,下面一起跟随内存溢出 jb51.cc的小编来看看吧。@H_404_0@# 内存溢出 (jb51.cc) sendmail.py#!/usr/bin/python# -*- Coding: UTF-8 -*-import smtplibimport email.MIMEMultipartimport email.MIMETextimport email.MIMEBaseimport 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 内存溢出 jb51.ccsmtp以及邮件的签名,还有发件人为定值,需要自己修改。 总结

以上是内存溢出为你收集整理的python 发送邮件实现方法全部内容,希望文章能够帮你解决python 发送邮件实现方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1199883.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存