当前位置:   article > 正文

Python使用smtplib发送邮件_import smtplib

import smtplib

一、邮件分析

此处发送邮件使用smtplib模块,不用下载,pycharm自带的有此模块。
我们通过实例化 smtplib 模块的 SMTP 对象 smtpObj 来连接到 SMTP 访问,并使用 sendmail 方法来发送信息。语法如下:

SMTP.sendmail(from_addr, to_addrs, msg[, mail_options, rcpt_options]
  • 1

参数说明:
from_addr: 邮件发送者地址。
to_addrs: 字符串列表,邮件发送地址。
msg: 发送消息
这里要注意一下第三个参数,msg是字符串,表示邮件。我们知道邮件一般由标题,发信人,收件人,邮件内容,附件等构成,发送邮件的时候,要注意msg的格式。这个格式就是smtp协议中定义的格式。

邮件有发送方sender、接收方receivers。一个邮件有需要三个头部信息: From, To, 和 Subject ,每个信息直接使用空行分割。

二、发送纯文本邮件

代码如下:
第一种

import smtplib
from email.mime.text import MIMEText
from email.header import Header

message = MIMEText('我来验证!')   # 邮件内容
message['From'] = Header('小爱')   # 邮件发送者名字
message['To'] = Header('小蓝枣')   # 邮件接收者名字
message['Subject'] = Header('来自异世界的一封信!')   # 邮件主题

mail = smtplib.SMTP()
mail.connect("smtp.qq.com")   # 连接 qq 邮箱
mail.login("接收方邮箱@qq.com", "授权码")   # 账号和授权码
mail.sendmail("发送方邮箱@qq.com", ["接收方邮箱@qq.com"], message.as_string())   # 发送账号、接收账号和邮件信息
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

关于授权码的问题,需要网上登录qq邮箱,设置中进入自己账户,打开SMIP服务。
1
2
黄色的消息框中有生成授权码,需要向腾讯科技发个信息来验证一下,再运行代码,即可收到短信。
1

第二种
和第一种几乎一样。

import smtplib
from email.mime.text import MIMEText
from email.header import Header

sender = '发送方邮箱@qq.com'
receivers = ['接收方邮箱@qq.com']  # 接收邮件,可设置为你的QQ邮箱或者其他邮箱

# 三个参数:第一个为文本内容,第二个 plain 设置文本格式,第三个 utf-8 设置编码
message = MIMEText('再验证一下SMIP模块', 'plain', 'utf-8')
message['From'] = Header("张", 'utf-8')  # 发送者
message['To'] = Header("艳", 'utf-8')  # 接收者

subject = 'Python SMTP 邮件测试'
message['Subject'] = Header(subject, 'utf-8')

try:
    smtpObj = smtplib.SMTP()
    smtpObj.connect("smtp.qq.com")  #连接qq邮箱
    smtpObj.login("接收方邮箱@qq.com", "cyxacyjvmrtxcjbe")  # 账号和授权码
    smtpObj.sendmail(sender, receivers, message.as_string())
    print("邮件发送成功")
except smtplib.SMTPException:
    print("Error: 无法发送邮件")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

结果运行成功。
1
最终手机上收到了两份邮件。
1
1

三、发送html格式邮件

注意修改语句message = MIMEText(mail_msg, 'html', 'utf-8'),传输的是html文件。代码如下:

import smtplib
from email.mime.text import MIMEText
from email.header import Header

sender = '发送方邮箱@qq.com'
receivers = ['接收方邮箱@qq.com']  # 接收邮件,可设置为你的QQ邮箱或者其他邮箱

# mail_msg是用html编写的内容,图片也用html格式,和链接放一起进行发送
mail_msg = """
<p>Python 邮件发送测试...</p>
<p><a href="http://www.runoob.com">这是一个链接</a></p>
"""
# 三个参数:第一个为文本内容,第二个 plain 设置文本格式,第三个 utf-8 设置编码
message = MIMEText(mail_msg, 'html', 'utf-8')     #邮件正文内容,修改plain为html
message['From'] = Header("张", 'utf-8')  # 发送者
message['To'] = Header("艳", 'utf-8')  # 接收者

subject = 'Python SMTP 发送html格式邮件测试'
message['Subject'] = Header(subject, 'utf-8')

try:
    smtpObj = smtplib.SMTP()
    smtpObj.connect("smtp.qq.com")  #连接qq邮箱
    smtpObj.login("接收方邮箱@qq.com", "udfhrmjpxyhcdbbf")  # 账号和授权码
    smtpObj.sendmail(sender, receivers, message.as_string())
    print("邮件发送成功")
except smtplib.SMTPException:
    print("Error: 无法发送邮件")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

1

四、发送html链接加附件邮件

传递两个附件,第一个是txt文件,第二个是png图片。先把png图片放到运行项目文件路径下。
1
利用MIMEMultipart()作附件对象。
代码如下:

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header

sender = '发送方邮箱@qq.com'
receivers = ['接收方邮箱@qq.com']  # 接收邮件,可设置为你的QQ邮箱或者其他邮箱

# 创建一个带附件的实例
message = MIMEMultipart()
message['From'] = Header("张", 'utf-8')
message['To'] = Header("艳", 'utf-8')
subject = 'Python SMTP html格式加附件邮件测试'
message['Subject'] = Header(subject, 'utf-8')

# 邮件正文内容
mail_msg = """
<p>Python 邮件发送测试...</p>
<p><a href="http://www.runoob.com">这是一个链接</a></p>
"""
message.attach(MIMEText(mail_msg, 'html', 'utf-8'))

# 构造附件1,传送当前目录下的 test.txt 文件
a=open('zy5.txt', 'rb').read()
att1 = MIMEText(a, 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
# 这里的filename可以任意写,写什么名字,邮件中显示什么名字
att1["Content-Disposition"] = 'attachment; filename="zy5.txt"'
message.attach(att1)

# 构造附件2,传送当前目录下的 runoob.txt 文件
b=open('1.png', 'rb').read()
att2 = MIMEText(b, 'base64', 'utf-8')
att2["Content-Type"] = 'application/octet-stream'
att2["Content-Disposition"] = 'attachment; filename="1.png"'
message.attach(att2)

try:
    smtpObj = smtplib.SMTP()
    smtpObj.connect("smtp.qq.com")  # 连接qq邮箱
    smtpObj.login('接收方邮箱@qq.com', 'udfhrmjpxyhcdbbf')  # 登录--发送者账号和口令
    smtpObj.sendmail(sender, receivers, message.as_string())
    print("邮件发送成功")
except smtplib.SMTPException:
    print("Error: 无法发送邮件")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

结果如下:
1

五、发送html图片、链接加附件邮件

插入上面作为附件的1.png图片,需要在邮件正文内容中添加捎带html图片的代码:

mail_msg = """
<p>Python 邮件发送测试...</p>
<p><a href="http://www.runoob.com">这是一个链接</a></p>
<p>图片演示:</p>
<p><img src="cid:image1"></p>#这里关于图片的名字不用做任何修改,会有下面的代码进行读取1.png
"""
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

还需要读取1.png图片内容:

#加入html图片
# 指定图片为当前目录
fp = open('1.png', 'rb')      #若要修改图片,只需更改图片名称即可切换
msgImage = MIMEImage(fp.read())
fp.close()

# 定义图片 ID,在 HTML 文本中引用
msgImage.add_header('Content-ID', '<image1>')    #不做任何修改
message.attach(msgImage)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

整体代码如下:

import smtplib
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header

sender = '3268743433@qq.com'
receivers = ['3268743433@qq.com']  # 接收邮件,可设置为你的QQ邮箱或者其他邮箱

# 创建一个带附件的实例
message = MIMEMultipart()
message['From'] = Header("张", 'utf-8')
message['To'] = Header("艳", 'utf-8')
subject = 'Python SMTP html格式加附件邮件测试'
message['Subject'] = Header(subject, 'utf-8')

# 邮件正文内容
mail_msg = """
<p>Python 邮件发送测试...</p>
<p><a href="http://www.runoob.com">这是一个链接</a></p>
<p>图片演示:</p>
<p><img src="cid:image1"></p>
"""
message.attach(MIMEText(mail_msg, 'html', 'utf-8'))

# 构造附件1,传送当前目录下的 test.txt 文件
a=open('zy5.txt', 'rb').read()
att1 = MIMEText(a, 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
# 这里的filename可以任意写,写什么名字,邮件中显示什么名字
att1["Content-Disposition"] = 'attachment; filename="zy5.txt"'
message.attach(att1)

# 构造附件2,传送当前目录下的 runoob.txt 文件
b=open('1.png', 'rb').read()
att2 = MIMEText(b, 'base64', 'utf-8')
att2["Content-Type"] = 'application/octet-stream'
att2["Content-Disposition"] = 'attachment; filename="1.png"'
message.attach(att2)

#加入html图片
# 指定图片为当前目录
fp = open('1.png', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()

# 定义图片 ID,在 HTML 文本中引用
msgImage.add_header('Content-ID', '<image1>')
message.attach(msgImage)

try:
    smtpObj = smtplib.SMTP()
    smtpObj.connect("smtp.qq.com")  # 连接qq邮箱
    smtpObj.login('3268743433@qq.com', 'udfhrmjpxyhcdbbf')  # 登录--发送者账号和口令
    smtpObj.sendmail(sender, receivers, message.as_string())
    print("邮件发送成功")
except smtplib.SMTPException:
    print("Error: 无法发送邮件")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58

实现效果如图:
1

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/701872
推荐阅读
相关标签
  

闽ICP备14008679号