要想实现一个能够发送带有文本、图片、附件的python程序,首先要熟悉两大模块:
email以及smtplib
然后对于MIME(邮件扩展)要有一定认知,因为有了扩展才能发送附件以及图片这些媒体或者非文本信息
最后一个比较细节的方法就是MIMEMultipart,要理解其用法以及对应参数所实现的功能区别
发送邮件三部曲:
创建协议对象
连接邮件服务器
登陆并发送邮件
from email.header import Header from email.mime.base import MIMEBase from email.mime.text import MIMEText import mimetypes from email.mime.multipart import MIMEMultipart import os import smtplib from email import Encoders as email_encoders class Message(object): def __init__(self, from_addr, to_addr, subject="", html="", text=None, cc_addr=[], attachment=[]): self.from_addr = from_addr self.subject = subject if to_addr: if isinstance(to_addr, list): self.to_addr = to_addr else: self.to_addr = [d for d in to_addr.split(',')] else: self.to_addr = [] if cc_addr: if isinstance(cc_addr, list): self.cc_addr = cc_addr else: self.cc_addr = [d for d in cc_addr.split(',')] else: self.cc_addr = [] if html is not None: self.body = html self.body_type = "html" else: self.body = text self.body_type = "plain" self.parts = [] if isinstance(attachment, list): for file in attachment: self.add_attachment(file) def add_attachment(self, file_path, mimetype=None): """ If *mimetype* is not specified an attempt to guess it is made. If nothing is guessed then `application/octet-stream` is used. """ if not mimetype: mimetype, _ = mimetypes.guess_type(file_path) if mimetype is None: mimetype = 'application/octet-stream' type_maj, type_min = mimetype.split('/') with open(file_path, 'rb') as fh: part_data = fh.read() part = MIMEBase(type_maj, type_min) part.set_payload(part_data) email_encoders.encode_base64(part) part_filename = os.path.basename(file_path) part.add_header('Content-Disposition', 'attachment; filename="%s"' % part_filename) part.add_header('Content-ID', part_filename) self.parts.append(part) def __to_mime_message(self): """Returns the message as :py:class:`email.mime.multipart.MIMEMultipart`.""" ## To get the message work in iOS, you need use multipart/related, not the multipart/alternative msg = MIMEMultipart('related') msg['Subject'] = self.subject msg['From'] = self.from_addr msg['To'] = ','.join(self.to_addr) if len(self.cc_addr) > 0: msg['CC'] = ','.join(self.cc_addr) body = MIMEText(self.body, self.body_type) msg.attach(body) # Add Attachment for part in self.parts: msg.attach(part) return msg def send(self, smtp_server='localhost'): smtp = smtplib.SMTP() smtp.connect(smtp_server) smtp.sendmail(from_addr=self.from_addr, to_addrs=self.to_addr + self.cc_addr, msg=self.__to_mime_message().as_string()) smtp.close()
对于实际发送程序,要注意个参数的类型,比如from_addr是字符串,to_addr和cc_addr以及attachment都是列表
from mail_base import Message import datetime from_addr = 'xxx' mail_to = 'xxx' def send_go(): time_now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M') attach_files = ['testcsv.xlsm','test1.jpg','test2.jpg','test3.jpg'] mail_msg = """ <p>Hi Lockey:</p> <p><img src="/UploadFiles/2021-04-08/cid:test1.jpg">对于测试程序我们命名为sendGo.py,运行测试程序
~$ python sendGo.py以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年11月20日
2024年11月20日
- 童丽《微风细雨XRCD2》[原抓WAV+CUE]
- 群星 《车乐坊1》DTS魔音[WAV+CUE][1.7G]
- 邓丽君.2024 -《生誕70年ベスト?アルバム 沒後30年》环球日版 [WAV+分轨][993M]
- 群星《一人一首成名曲》1998 台湾金碟珍藏版[WAV+CUE][1.1G]
- 陈百强2024-《凝望》头版限量编号MQA-UHQCD[WAV+CUE]
- 测试示范《监听天碟3》头版限量编号LECD[WAV+CUE]
- 陈瑞《爱你到天荒地老HQ》头版限量[低速原抓WAV+CUE]
- 徐小凤 《徐小凤殿堂18首》24K金碟[WAV+CUE]
- 保时捷原厂车载爆棚动态试音碟《Panamera_Soundtrack》DTS[WAV分轨][1G]
- 容祖儿《小小》香港首版 [WAV+CUE][1.1G]
- 莫文蔚《拉活…》SONY [WAV+CUE][1G]
- Beyond《极品天碟》LPCD45II首批限量版[WAV+CUE][1.7G]
- HIFI示范巅峰之作《情解药·Hi-Fi心魂》2CD[WAV+CUE]
- 房东的猫2021-关于彻夜不眠的事情(EP)[青柴文化][WAV+CUE]
- 群星.1993-一曲成名·青春无悔【飞碟】【WAV+CUE】