GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Python_Lib/content/envelopes.md at master · windard/Python_Lib

Expand file tree

/

envelopes.md

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
52 lines (38 loc) · 2.17 KB
/

envelopes.md

Copy path

File metadata and controls

52 lines (38 loc) · 2.17 KB
Raw
Copy raw file
Download raw file
Edit and raw actions

envelopes

这是将email和smtplib两个库进行封装,使其发送邮件更加方便。
不过一般Python并不自带,需要自行安装。

安装方法

  • Linux下:pip install envelopes
  • Windows下,点击这里下载文件压缩包,解压后进入文件夹,python install setup.py即可安装。
from envelopes import Envelope

#创建一个envelopes对象
envelope = Envelope(
	#发件人的地址,和名称
	from_addr = (u"[email protected]",u"Windard"),
	#收件人的地址,和名称
	to_addr = (u"[email protected]",u"Yang"),
	#邮件的主题
	subject = u"天上地下,何以为家",
	#邮件的内容
	text_body = u"眼泪被岁月蒸发"
	)

#在邮件中添加附件也非常简单
envelope.add_attachment('images/163mail_smtp_demo.jpg')

#最后连接邮件服务器并发送
envelope.send("smtp.qq.com",login="[email protected]",password="XXXXXX",tls=True)

print "Sending Successful"

保存为envelopes_demo.py,运行,看一下结果。

envelopes_demo.jpg

envelopes_demo_successful.jpg

发送邮件的地方也可以不用envelope.send()而用原生的SMTP.SMTP(),只需要在开始的时候导入SMTP,然后就可以qq=SMTP(host="XXX",login="XXX",password="XXX"),再用qq.send(envelope)来发送出去。

envelope也可以进行群发,只需要在设定to_addr=(u"[email protected]",u"XXX",u"[email protected]",u"XXX")即可。
envelope也可以用来发送HTML,只需在对象中使用html_body=u'XXX',即可发送HTML文本。
envelope也可以设定字符编码,只需在对象中使用 charset=u'XXX',即可设定编码格式。
envelope也可设定抄送人,只需在对象中设定 cc_addr=u'XXX',即可设定抄送人,也可以是多个形式的列表。

发现很多很好用的 Python 发邮件的库,比如说 yagmail ,发邮件只需两步

yag = yagmail.SMTP(user='[email protected]', password='xxxx', host='smtp.163.com', port='25')
yag.send(to='[email protected]', subject = "I now can send an attachment", contents='This is contents', attachments=['a.txt', 'b.jpg'])  
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.