使用Python发送邮件

xiaoxiao2021-02-28  104

(转载)http://mrliao.cn/2016/06/17/使用python发送邮件/

前一段时间慢悠悠的写毕设,一直在偷懒没有写博客了。现在是该还债的时候了。 本文主要内容是使用Python来自动发送邮件。

准备邮件服务器

邮件服务器没有必要自己去做,因为搭建起来太复杂,发送的邮件还容易被其他的邮件商识别为垃圾邮件。因此推荐使用主流的邮件服务商的产品。如果要求不高的话,也可以使用自己平时用的邮箱来测试。 本文使用的是阿里云的邮件服务,好处是可以自定义自己的邮箱地址,比如我的就设置为no-reply@notice.mrliao.cn,绑定在自己的域名上。 确定好要使用的邮件服务器后,还要查一下这个服务器的邮件服务端口是多少,一般的端口都是25,但是也有不同。 因此开始之前,需要确定你已经有以下信息(我的为例):

1 2 3 4 邮箱账号: no-reply@notice.mrliao.cn 邮箱密码: *********** 服务器地址: smtpdm.aliyun.com 端口: 25

发送简单邮件

Python发送邮件需要使用两个自带的模块:email、smtplib。直接上代码:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 import email import smtplib account = "no-reply@notice.mrliao.cn" password = "**********" serverUrl = "smtpdm.aliyun.com" (这个地址仅仅适用阿里云) port = 25 sendTo = "hi@mrliao.cn" # 登录服务器 server = smtplib.SMTP(serverUrl, port) server.set_debuglevel( 1) server.login(account, password) # 新建邮件 newEmail = email.MIMEText( "Hello world,这是邮件正文!", 'plain', 'utf-8') newEmail[ 'From'] = email.Header( '发送者名称<%s>' % account, 'utf-8').encode() newEmail[ 'To'] = sendTo newEmail[ 'Subject'] = Header( '邮件主题', 'utf-8').encode() # 发送邮件 server.sendmail(account, sendTo, newEmail.as_string())

一共就三个步骤,登录服务器、创建邮件、发送邮件。代码中的set_debuglevel(1)是开启调试的意思,开启调试的时候哦会打印所有服务器返回的信息。

发送HTML邮件

发送HTML邮件的步骤和上面一样,不过在构造邮件的时候需要改一下,即是把文本类型由plain改为html

1 newEmail = email.MIMEText( "<p>Hello world</p><p>这是邮件正文!</p>", 'html', 'utf-8')

这里可以使用行内式和嵌入式的css布局,但不要使用外部引用的css,否则会因安全性问题屏蔽。而且需要注意的是,邮件里面尽量不要引用用外部资源,比如图片等等,大多会被拦截掉。如果需要加入图片,前看下一节。

添加附件

如果要添加附件,只需要用不同的方法来构造邮件,其余发送部分相同:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 newEmail = email.MIMEMultipart() newEmail[ 'From'] = email.Header( '发送者名称<%s>' % account, 'utf-8').encode() newEmail[ 'To'] = sendTo newEmail[ 'Subject'] = Header( '邮件主题', 'utf-8').encode() newEmail.attach(MIMEText( '邮件正文', 'plain', 'utf-8')) with open( 'C:/test/test.png', 'rb') as f: mime = MIMEBase( 'image', 'png', filename= 'test.png') mime.add_header( 'Content-Disposition', 'attachment', filename= 'test.png') mime.add_header( 'Content-ID', '<0>') mime.add_header( 'X-Attachment-Id', '0') mime.set_payload(f.read()) encoders.encode_base64(mime) newEmail.attach(mime)

如果要添加图片到正文中,可以直接使用下面的引用方法,这里的cid的值是上面添加附件中的X-Attachment-Id的值,因此如果要使用多张图片的话,就依次编写不同的值并引用就可以了。

1 <img src="cid:0">

群发邮件

Python对群发邮件的支持也很好。以群发简单邮件为例:

1 2 3 4 5 6 7 8 # 收件地址为一个数组 sendTo = [ "hi@mrliao.cn", "test@mrliao.cn"] # 构造邮件时要对收件地址进行处理 newEmail[ 'To'] = ','.join(sendTo) # 发送邮件 server.sendmail(account, sendTo, newEmail.as_string())

首先生成一个收件人地址列表;然后在构造邮件的收件人信息时,要把列表合并为一个用,分隔的字符串,注意这里必须且只能用英文逗号分隔;最后把列表传入sendmail函数中发送邮件。


More info: Python Send Email

阿里邮箱相关地址和端口

https://help.aliyun.com/knowledge_detail/36576.html

企业云邮箱POP\SMTP\IMAP地址和端口信息 更新时间:2017-06-07 13:26:11 分享: 企业云邮箱各个服务器地址及端口信息如下: 收件服务器地址: POP 服务器地址:pop3.mxhichina.com 端口110,SSL 加密端口995 或 IMAP 服务器地址:imap.mxhichina.com 端口143,SSL 加密端口993 发件服务器地址: SMTP 服务器地址:smtp.mxhichina.com 端口25, SSL 加密端口465

//--------------------------------------------------------------------------------------------------------------------------------

从一个阿里邮箱发送邮件到另一个阿里邮箱

import email import smtplib from email.header import Header from email.mime.text import MIMEText account = "xxx@xxx.com" password = "xxxxxx" serverUrl = "smtp.mxhichina.com" port = 25 sendTo = "xxx@xxx.com" # 登录服务器 server = smtplib.SMTP(serverUrl, port) server.set_debuglevel(1) server.login(account, password) # 新建邮件 email.message newEmail = MIMEText("Hello world,这是邮件正文!", 'plain', 'utf-8') newEmail['From'] = Header('发送者名称<%s>' % account, 'utf-8').encode() newEmail['To'] = sendTo newEmail['Subject'] = Header('邮件主题', 'utf-8').encode() # 发送邮件 server.sendmail(account, sendTo, newEmail.as_string())

转载请注明原文地址: https://www.6miu.com/read-42489.html

最新回复(0)