Github 大牛封装 Python 代码,实现自动发送邮件只需三行代码

《Github 大牛封装 Python 代码,实现自动发送邮件只需三行代码》 Github 大牛封装 Python 代码,实现自动发送邮件只需三行代码

*注意:全文代码可左右滑动观看

在运维开发中,使用 Python 发送邮件是一个非常常见的应用场景。今天一起来探讨一下,GitHub 的大牛门是如何使用 Python 封装发送邮件代码的。

一般发邮件方法

SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件、HTML邮件以及带附件的邮件。

我们以前在通过Python实现自动化邮件功能的时候是这样的:

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;”>import smtplib
from email.mime.text import MIMEText
from email.header import Header

发送邮件服务器

smtpserver = ‘smtp. sina. com’

发送邮件用户/密码

user = ‘usernamee@sina. com’
password = ‘123456’

发送邮件

sender = ‘username@sina. com”

接收邮件

receiver = ‘receive@126. com’

发送邮件主题

subject = ‘Python email test’

编写HTML类型的邮件正文

msg = MIMEText(‘<html><h1>你好 ! </h1></html>’,’html’,’utf-8′)
msg[‘Subject’] = Header(subject,’utf-8′)

连接发送邮件

smtp = smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login(user,password)
satp.sendmail(sender, receiver, msg.as_ string())
satp.quit()
</pre>

python发邮件需要掌握两个模块的用法,smtplib和email,这俩模块是python自带的,只需import即可使用。smtplib模块主要负责发送邮件,email模块主要负责构造邮件。

smtplib模块主要负责发送邮件:是一个发送邮件的动作,连接邮箱服务器,登录邮箱,发送邮件(有发件人,收信人,邮件内容)。

email模块主要负责构造邮件:指的是邮箱页面显示的一些构造,如发件人,收件人,主题,正文,附件等。

其实,这段代码也并不复杂,只要你理解使用过邮箱发送邮件,那么以下问题是你必须要考虑的:

  • 你登录的邮箱帐号/密码
  • 对方的邮箱帐号
  • 邮件内容(标题,正文,附件)
  • 邮箱服务器(SMTP.xxx.com/pop3.xxx.com)

如果要把一个图片嵌入到邮件正文中怎么做?直接在HTML邮件中链接图片地址行不行?答案是,大部分邮件服务商都会自动屏蔽带有外链的图片,因为不知道这些链接是否指向恶意网站。

要把图片嵌入到邮件正文中,我们只需按照发送附件的方式,先把邮件作为附件添加进去,然后,在HTML中通过引用src=”cid:0″就可以把附件作为图片嵌入了。如果有多个图片,给它们依次编号,然后引用不同的cid:x即可。

yagmail 实现发邮件

yagmail 可以更简单的来实现自动发邮件功能。

github项目地址: https://github.com/kootenpv/yagmail

代码开源,解释如下:

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;”>yag = SMTP(args.user,args.password)
yag.send(to.=args.to,subject=args.subject,contents=args.contents,attachments=args.attachments)
</pre>

安装:

pip install yagmail

简单例子:

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;”>import yagmail

链接邮件服务器

yag = yagmail.SMTP(user=”user@126.com“,password=”1234”,host=’smtp.126.com’)

邮件正文

contents = [‘This is the body,and here is just text http://somedomain/image.png’,’You can find an andio file atteched.’,’/local/path/song mp3′]

发送邮件

yag.send(‘taaa@126.com‘,’subject’,contents)
</pre>

给多个用户发邮件:

只需要将接收邮箱 变成一个list即可。

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;”>yag.send([‘aa@126.com‘,’bb@qq.com‘,’cc@gmail.com‘], ‘subject’, contents)
</pre>

发送附件

如何发送附件呢?只要添加一个附件列表就可以了。

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;”>yag.send(‘aaaa@126.com‘, ‘发送附件’, contents, [“d://log.txt”,”d://baidu_img.jpg”])
</pre>

抄送

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;”>#邮件正文 文本及附件contents = [‘This is the body,and here is just text http://somedomain/image.png’,’You can find an audio file attached.’,’/local/path/song.mp3′,’测试邮件’,’test.html’,’logo.jpg’,’yagmal_test.txt’]#发送yag.send(to=’xx@xx.com‘,cc=’xxx@xxx.com‘,subject=’发送附件’,contend=contents)
</pre>

很简单吧,开箱即用~~

    原文作者:Python小老弟
    原文地址: https://www.jianshu.com/p/8126cb62e8b2
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞