Python基于smtplib模块发送邮件代码实例

 更新时间:2020年05月29日 11:19:21   作者:--TINGXIN--  
这篇文章主要介绍了Python基于smtplib模块发送邮件代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

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

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

email模块下有mime包,mime英文全称为“Multipurpose Internet Mail Extensions”,即多用途互联网邮件扩展,是目前互联网电子邮件普遍遵循的邮件技术规范。

该mime包下常用的有三个模块:text,image,multpart。

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

#邮件服务器信息
smtp_server = "smtp.qq.com"
port = 465 # For starttls
sender_email = "12345689@qq.com"
password="" #get password from mailsetting

#发送邮件信息,可以发送给多个收件人
receivers=["12345689@163.com","12345689@qq.com"]
subject="This is import Python SMTP 邮件(文件传输) 多媒体测试"

# message = MIMEText(text, "plain", "utf-8") #文本邮件
message = MIMEMultipart()
message["Subject"] = Header(subject, "utf-8")
message["from"] = sender_email
message["to"] = ",".join(receivers)
# 邮件正文内容
text="""
Dear Sir:
how are you ? \n
for detail information pls refer to attach1。\n
The files you need are as followed.\n
If you have any concern pls let me known.\n
enjoy your weekend.\n
BEST REGARDS \n
"""
# message.attach(MIMEText('for detail information pls refer to attach1。\n The files you need are as followed. \n If you have any concern pls let me known. \n enjoy your weekend', 'plain', 'utf-8')
message.attach(MIMEText(text,'plain','utf-8'))

# 构造附件1
attach_file1='IMG1965.JPG'

attach1 = MIMEText(open(attach_file1, 'rb').read(), 'base64', 'utf-8')
attach1["Content-Type"] = 'application/octet-stream'
attach1["Content-Disposition"] = 'attachment; filename={0}'.format(attach_file1)
message.attach(attach1)

# 构造附件2
attach_file2='YLJ.jpg'
attach2 = MIMEText(open(attach_file2, 'rb').read(), 'base64', 'utf-8')
attach2["Content-Type"] = 'application/octet-stream'
attach2["Content-Disposition"] = 'attachment; filename={0}'.format(attach_file2)
message.attach(attach2)

# Try to log in to server and send email
# server = smtplib.SMTP_SSL(smtp_server,port)
server = smtplib.SMTP_SSL(smtp_server,port)

try:
  server.login(sender_email, password)
  server.sendmail(sender_email,receivers,message.as_string())
  print("邮件发送成功!!!")
  print("Mail with {0} & {1} has been send to {2} successfully.".format(attach_file1,attach_file2,receivers))
except Exception as e:
  # Print any error messages to stdout
  print("Error: 无法发送邮件")
  print(e)
finally:
  server.quit()

结果

邮件发送成功!!!

Mail with IMG1965.JPG & IMG1963.jpg has been send to ['12345689@163.com', '12345689@qq.com'] successfully.

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • python 根据pid杀死相应进程的方法

    python 根据pid杀死相应进程的方法

    下面小编就为大家带来一篇python 根据pid杀死相应进程的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-01-01
  • Python脚本破解压缩文件口令实例教程(zipfile)

    Python脚本破解压缩文件口令实例教程(zipfile)

    这篇文章主要给大家介绍了关于Python脚本破解压缩文件口令(zipfile)的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Python具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2020-06-06
  • Python深度学习albumentations数据增强库

    Python深度学习albumentations数据增强库

    下面开始albumenations的正式介绍,在这里我强烈建议英语基础还好的读者去官方网站跟着教程一步步学习,而这里的内容主要是我自己的一个总结以及方便英语能力较弱的读者学习
    2021-09-09
  • python读取文本中数据并转化为DataFrame的实例

    python读取文本中数据并转化为DataFrame的实例

    下面小编就为大家分享一篇python读取文本中数据并转化为DataFrame的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-04-04
  • Mac安装python3的方法步骤

    Mac安装python3的方法步骤

    这篇文章主要介绍了Mac安装python3的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-08-08
  • PyCharm在win10的64位系统安装实例

    PyCharm在win10的64位系统安装实例

    给大家介绍一下在win10的64位系统中安装PyCharm的操作过程以及需要注意的地方。
    2017-11-11
  • 如何利用python多线程爬取天气网站图片并保存

    如何利用python多线程爬取天气网站图片并保存

    最近做个天 气方面的APP需要用到一些天气数据,所以下面这篇文章主要给大家介绍了关于如何利用python多线程爬取天气网站图片并保存的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2021-11-11
  • 利用python控制Autocad:pyautocad方式

    利用python控制Autocad:pyautocad方式

    这篇文章主要介绍了利用python控制Autocad:pyautocad方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-06-06
  • Python中venv虚拟环境超详细讲解

    Python中venv虚拟环境超详细讲解

    虚拟环境是一个独立的Python环境,它与系统的全局Python环境隔离,这篇文章主要介绍了Python中venv虚拟环境的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2025-04-04
  • python3反转字符串的3种方法(小结)

    python3反转字符串的3种方法(小结)

    这篇文章主要介绍了python3反转字符串的3种方法(小结),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-11-11

最新评论