Python是一门很简单和强大的语言,在熟悉其他编程语言的基础上,可以很快的入门Python。Python本身内置了很多工具模块,远比C++方便的多(当然各自的适合场景不同)。
这里简单介绍下我使用python中email模块发送邮件的相关知识。
SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件、HTML邮件以及带附件的邮件。python发送邮件主要使用到了smtplib和email两个模块,email负责构造邮件,smtplib负责发送邮件。
相关模块
smtplib模块
smtplib.SMTP()smtplib.SMTP([host[, port[, local_hostname[, timeout]]]])
SMTP类构造函数,表示与SMTP服务器之间的连接,通过这个连接可以向smtp服务器发送指令,执行相关操作(如:登陆、发送邮件)。所有参数都是可选的。参数意义如下:
host:smtp服务器主机名 port:smtp服务的端口,默认是25;如果在创建SMTP对象的时候提供了这两个参数,在初始化的时候会自动调用connect方法去连接服务器。
smtplib模块还提供了SMTP_SSL类和LMTP类,对它们的操作与SMTP基本一致。
smtplib.SMTP提供的方法:
SMTP.set_debuglevel(level):#设置是否为调试模式。默认为False,即非调试模式,表示不输出任何调试信息。 SMTP.connect([host[, port]]):#连接到指定的smtp服务器。参数分别表示smpt主机和端口。注意: 也可以在host参数中指定端口号(如:smpt.yeah.net:25),这样就没必要给出port参数。 SMTP.docmd(cmd[, argstring]):向smtp服务器发送指令。可选参数argstring表示指令的参数。 SMTP.helo([hostname]) :使用"helo"指令向服务器确认身份。相当于告诉smtp服务器“我是谁”。 SMTP.has_extn(name):判断指定名称在服务器邮件列表中是否存在。出于安全考虑,smtp服务器往往屏蔽了该指令。 SMTP.verify(address) :判断指定邮件地址是否在服务器中存在。出于安全考虑,smtp服务器往往屏蔽了该指令。 SMTP.login(user, password) :登陆到smtp服务器。现在几乎所有的smtp服务器,都必须在验证用户信息合法之后才允许发送邮件。 SMTP.sendmail(from_addr, to_addrs, msg[, mail_options, rcpt_options]) :发送邮件。这里要注意一下第三个参数,msg是字符串,表示邮件。我们知道邮件一般由标题,发信人,收件人,邮件内容,附件等构成,发送邮件的时候,要注意msg的格式。这个格式就是smtp协议中定义的格式。 SMTP.quit() :断开与smtp服务器的连接,相当于发送"quit"指令。
email模块
emial模块用来处理邮件消息,包括MIME和其他基于RFC 2822 的消息文档。使用这些模块来定义邮件的内容,是非常简单的。其包括的类有( 点此查看更加详细的介绍):
class email.mime.base.MIMEBase(_maintype, _subtype, **_params):这是MIME的一个基类。一般不需要在使用时创建实例。其中_maintype是内容类型,如text或者image。_subtype是内容的minor type 类型,如plain或者gif。 **_params是一个字典,直接传递给Message.add_header()。class email.mime.multipart.MIMEMultipart([_subtype[, boundary[, _subparts[, _params]]]]:MIMEBase的一个子类,多个MIME对象的集合,_subtype默认值为mixed。boundary是MIMEMultipart的边界,默认边界是可数的。class email.mime.application.MIMEApplication(_data[, _subtype[, _encoder[, **_params]]]):MIMEMultipart的一个子类。class email.mime.audio. MIMEAudio(_audiodata[, _subtype[, _encoder[, **_params]]]): MIME音频对象class email.mime.image.MIMEImage(_imagedata[, _subtype[, _encoder[, **_params]]]):MIME二进制文件对象。class email.mime.message.MIMEMessage(_msg[, _subtype]):具体的一个message实例.class email.mime.text.MIMEText(_text[, _subtype[, _charset]]):MIME文本对象,其中_text是邮件内容,_subtype邮件类型,可以是text/plain(普通文本邮件),html/plain(html邮件), _charset编码,可以是gb2312等等。
几个实例代码
发送普通文本邮件
#!/usr/bin/env python# -*- coding: utf-8 -*-import smtplibfrom email.mime.text import MIMETextfrom email.Header import Headerdef sendMailText(title, content, sender, receiver, serverip, serverport, username, pwd): msg = MIMEText(content, _subtype="plain", _charset="utf-8") # 设置正文为符合邮件格式的HTML内容 msg['Subject'] = Header(title, "utf-8") # 设置邮件标题 msg['From'] = sender # 设置发件人 msg['To'] = receiver # 设置收件人 s = smtplib.SMTP(serverip, serverport) # 注意!如果是使用SSL端口,这里就要改为SMTP_SSL #s.ehlo() #s.starttls() s.login(username, pwd) # 登陆邮箱 s.sendmail(sender, receiver, msg.as_string()) # 发送邮件if __name__ == "__main__": config = { "from": "XXXXXXXXX@163.com", # 发件人邮箱 "to": "YYYYYYYYYY@163.COM", # 收件人邮箱 "serverip": "smtp.163.com", # 发件服务器IP "serverport":"25", # 发件服务器Port "username": "XXXXXXXXX@163.com", # 发件人用户名 "pwd": "AAAAAAAAAAAAAA" # 发件人密码 } title = "python send mail" body = "<a href='http://cpper.info'>cpper</a>" sendMailText(title, body, config['from'], config['to'], config['serverip'], config['serverport'], config['username'], config['pwd'])
发送Html格式邮件
与上面介绍的文本邮件基本一致,除了一点不同:
msg = MIMEText(content, _subtype="html", _charset="utf-8")
发送带附件的邮件
如果发送带附件的邮件,首先要创建MIMEMultipart()实例,然后构造附件,如果有多个附件,可依次构造,最后利用smtplib.smtp发送。
def sendMailWithAttachment(title, content, sender, receiver, serverip, serverport, username, pwd, attach = {}): attachsize = len(attach) if attachsize > 0 : print 'send attach' msg = MIMEMultipart() #创建一个带附件的实例 for path, name in attach.items(): print path, name if os.path.exists(path): att = MIMEText(open(path, 'rb').read(), 'base64', 'gb2312') #构造附件 att["Content-Type"] = 'application/octet-stream' att["Content-Disposition"] = 'attachment; filename="' + name + '"' msg.attach(att) # http://help.163.com/09/1224/17/5RAJ4LMH00753VB8.html 提示发送病毒或垃圾邮件 554, 'DT:SPM 163 else: msg = MIMEText(content, _subtype="html", _charset="utf-8") # 设置正文为符合邮件格式的HTML内容 msg['Subject'] = Header(title, "gb2312") # 设置邮件标题 msg['From'] = sender # 设置发件人 msg['To'] = receiver # 设置收件人 s = smtplib.SMTP(serverip, serverport) # 注意!如果是使用SSL端口,这里就要改为SMTP_SSL s.login(username, pwd) # 登陆邮箱 s.sendmail(sender, receiver, msg.as_string()) # 发送邮件attach = {} # key : 要发送的文件,需要存在,否则读取不到:value :自定义发送邮件中的文件名attach['sendEmail.py'] = '1.py' attach['memset.cpp'] = '2.c'#attach['noexist.cpp'] = '3.cpp'sendMailWithAttachment(title, body, config['from'], config['to'], config['serverip'], config['serverport'], config['username'], config['pwd'], attach)
发送带图片的邮件
def sendMailWithImage(title, content, sender, receiver, serverip, serverport, username, pwd, images = []): imagesize = len(images) if imagesize > 0 : msg = MIMEMultipart() #创建一个带附件的实例 for path in images: print path if os.path.exists(path): image = MIMEImage(open(path,'rb').read()) name = path.split(os.sep)[-1] image.add_header("Content-Disposition", "attachment", filename=name) image.add_header('Content-ID', '<0>') image.add_header('X-Attachment-Id', '0') # We reference the image in the IMG SRC attribute by the ID we give it below #msg.attach(MIMEText('hello
' + '' + '', 'html', 'utf-8')) msg.attach(image) else: msg = MIMEText(content, _subtype="html", _charset="utf-8") # 设置正文为符合邮件格式的HTML内容 msg['Subject'] = Header(title, "gb2312") # 设置邮件标题 msg['From'] = sender # 设置发件人 msg['To'] = receiver # 设置收件人 s = smtplib.SMTP(serverip, serverport) # 注意!如果是使用SSL端口,这里就要改为SMTP_SSL s.login(username, pwd) # 登陆邮箱 s.sendmail(sender, receiver, msg.as_string()) # 发送邮件
一个综合示例
# 发送含有各种元素的邮件def sendMail(title, content, sender, receiver, serverip, serverport, username, pwd, attach = {}, images = {}): msg = MIMEMultipart() msg['Subject'] = Header(title, "utf-8") # 设置邮件标题 msg['From'] = sender # 设置发件人 msg['To'] = receiver # 设置收件人 #msg['To'] = ";".join(receiver) # 多个收件人 attachsize = len(attach) if attachsize > 0 : print 'send attach' for path, name in attach.items(): 3print path, name if os.path.exists(path): att = MIMEText(open(path, 'rb').read(), 'base64', 'gb2312') #构造附件 att["Content-Type"] = 'application/octet-stream' att["Content-Disposition"] = 'attachment; filename="' + name + '"' print att["Content-Disposition"] msg.attach(att) imagesize = len(images) if imagesize > 0 : for path, name in images.items(): #print path, name if os.path.exists(path): image = MIMEImage(open(path,'rb').read()) #image = MIMEImage(open(path,'rb').read(), _subtype="jpg") image.add_header("Content-Disposition", "attachment", filename=name) image.add_header('Content-ID', '<0>') image.add_header('X-Attachment-Id', '0') msg.attach(image) s = smtplib.SMTP(serverip, serverport) # 注意!如果是使用SSL端口,这里就要改为SMTP_SSL s.login(username, pwd) # 登陆邮箱 s.sendmail(sender, receiver, msg.as_string()) # 发送邮件if __name__ == "__main__": config = { "from": "XXXXXXXXX@163.com", # 发件人邮箱 "to": "YYYYYYYYYY@163.COM", # 收件人邮箱 "serverip": "smtp.163.com", # 发件服务器IP "serverport":"25", # 发件服务器Port "username": "XXXXXXXXX@163.com", # 发件人用户名 "pwd": "AAAAAAAAAAAAAA" # 发件人密码 } title = "python send mail" body = "<a href='http://cpper.info'>cpper</a>" attach = {} # key : 要发送的文件,需要存在,否则读取不到:value :自定义发送邮件中的文件名 attach['sendEmail.py'] = '1.py' attach['memset.cpp'] = '2.c' attach['noexist.cpp'] = '3.cpp' images = {} images['lizheng3.jpg'] = '1.jpg' sendMail(title, body, config['from'], config['to'], config['serverip'], config['serverport'], config['username'], config['pwd'], attach, images)
代码下载
本文所有代码可以点此下载:sendEmail.py。

HTML的未来趋势是语义化和Web组件,CSS的未来趋势是CSS-in-JS和CSSHoudini,JavaScript的未来趋势是WebAssembly和Serverless。1.HTML的语义化提高可访问性和SEO效果,Web组件提升开发效率但需注意浏览器兼容性。2.CSS-in-JS增强样式管理灵活性但可能增大文件体积,CSSHoudini允许直接操作CSS渲染。3.WebAssembly优化浏览器应用性能但学习曲线陡,Serverless简化开发但需优化冷启动问题。

HTML、CSS和JavaScript在Web开发中的作用分别是:1.HTML定义网页结构,2.CSS控制网页样式,3.JavaScript添加动态行为。它们共同构建了现代网站的框架、美观和交互性。

HTML的未来充满了无限可能。1)新功能和标准将包括更多的语义化标签和WebComponents的普及。2)网页设计趋势将继续向响应式和无障碍设计发展。3)性能优化将通过响应式图片加载和延迟加载技术提升用户体验。

HTML、CSS和JavaScript在网页开发中的角色分别是:HTML负责内容结构,CSS负责样式,JavaScript负责动态行为。1.HTML通过标签定义网页结构和内容,确保语义化。2.CSS通过选择器和属性控制网页样式,使其美观易读。3.JavaScript通过脚本控制网页行为,实现动态和交互功能。

HTMLISNOTAPROGRAMMENGUAGE; ITISAMARKUMARKUPLAGUAGE.1)htmlStructures andFormatSwebContentusingtags.2)itworkswithcsssforstylingandjavascript for Interactivity,增强WebevebDevelopment。

HTML是构建网页结构的基石。1.HTML定义内容结构和语义,使用、、等标签。2.提供语义化标记,如、、等,提升SEO效果。3.通过标签实现用户交互,需注意表单验证。4.使用、等高级元素结合JavaScript实现动态效果。5.常见错误包括标签未闭合和属性值未加引号,需使用验证工具。6.优化策略包括减少HTTP请求、压缩HTML、使用语义化标签等。

HTML是一种用于构建网页的语言,通过标签和属性定义网页结构和内容。1)HTML通过标签组织文档结构,如、。2)浏览器解析HTML构建DOM并渲染网页。3)HTML5的新特性如、、增强了多媒体功能。4)常见错误包括标签未闭合和属性值未加引号。5)优化建议包括使用语义化标签和减少文件大小。

WebDevelovermentReliesonHtml,CSS和JavaScript:1)HTMLStructuresContent,2)CSSStyleSIT和3)JavaScriptAddSstractivity,形成thebasisofmodernWebemodernWebExexperiences。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

SublimeText3汉化版
中文版,非常好用

Dreamweaver Mac版
视觉化网页开发工具

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器