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

作者:--TINGXIN-- 时间:2022-09-18 11:07:49 

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.

来源:https://www.cnblogs.com/tingxin/p/12961901.html

标签:Python,smtplib,模块,邮件
0
投稿

猜你喜欢

  • JavaScript编写棋盘覆盖代码详解

    2024-04-17 10:30:05
  • 在Python中使用dict和set方法的教程

    2023-12-20 20:29:07
  • Vue.js 利用v-for中的index值实现隔行变色

    2024-04-16 09:35:40
  • python @classmethod 的使用场合详解

    2023-08-02 20:50:35
  • 分享给Python新手们的几道简单练习题

    2021-08-01 22:05:14
  • 无法在Web服务器上启动调试。未将项目配置为进行调试

    2024-03-21 03:11:13
  • python妙用之编码的转换详解

    2022-01-29 05:01:54
  • python实现不同数据库间数据同步功能

    2024-01-18 15:58:52
  • SQL Server利用sp_spaceused如何查看表记录存在不准确的情况

    2024-01-20 07:40:10
  • python四个坐标点对图片区域最小外接矩形进行裁剪

    2022-01-18 02:18:09
  • 用SQL语句查询数据库中某一字段下相同值的记录方法

    2024-01-23 14:49:54
  • MySQL pt-slave-restart工具的使用简介

    2024-01-17 11:42:34
  • 以Python代码实例展示kNN算法的实际运用

    2024-05-22 10:28:42
  • 深入解析PHP 5.3.x 的strtotime() 时区设定 警告信息修复

    2023-11-06 19:25:27
  • python接口测试对修改密码接口进行压测

    2022-05-16 04:26:15
  • Vue父子组件通信全面详细介绍

    2024-06-05 09:21:16
  • python目标检测非极大抑制NMS与Soft-NMS

    2022-05-13 21:05:08
  • canvas 2d 环形统计图手写实现示例

    2023-07-13 16:35:23
  • python使用正则表达式检测密码强度源码分享

    2022-06-26 15:54:42
  • MySQL如何构建数据表索引

    2024-01-14 06:25:26
  • asp之家 网络编程 m.aspxhome.com