使用smtplib模块来发送邮件

使用smtplib模块来发送邮件

微信搜索 zze_coding 或扫描 👉 二维码关注我的微信公众号获取更多资源推送:

文本邮件

import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.utils import formataddr
# 邮箱服务器
mail_host = "smtp.qq.com"
# 发件人
sender = "xxx@qq.com"
# 密码
sender_pass = "xxx"
# 收件人,可有多个
receivers = ("xxx@qq.com",)
# 邮件内容
message = MIMEText("嘿嘿嘿", "plain", "utf-8")

message["From"] = formataddr(("ZZE", sender,))
message["To"] = ','.join(receivers)
# 邮件标题
subject = '邮件测试'
message['Subject'] = Header(subject, 'utf-8')

try:
    # 明文发送
    # smtpObj = smtplib.SMTP()
    # smtpObj.connect(mail_host, 25)
    # SSL 加密发送
    smtp_obj = smtplib.SMTP_SSL(mail_host)
    smtp_obj.connect(mail_host, 465)
    smtp_obj.login(sender, sender_pass)
    smtp_obj.sendmail(sender, receivers, message.as_string())
    smtp_obj.quit()
    print("邮件发送成功")
except smtplib.SMTPException:
    print("Error: 无法发送邮件")

图片.png

富文本邮件

import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.utils import formataddr
# 邮箱服务器
mail_host = "smtp.qq.com"
# 发件人
sender = "xxx@qq.com"
# 密码
sender_pass = "xxx"
# 收件人,可有多个
receivers = ("xxx@qq.com",)
# HTML 内容
mail_msg = """
<p>HTML邮件测试</p>
<table border='1'>
<thead>
  <tr>
    <th>序号</th>
    <th>姓名</th>
    <th>爱好</th>
  </tr>
  </thead>

  <tbody>
  <tr>
    <td>1</td>
    <td>小明</td>
    <td>打球</td>
  </tr>
  <tr>
    <td>2</td>
    <td>小红</td>
    <td>发豪</td>
  </tr>
  </tbody>
</table>
"""

# 邮件内容
message = MIMEText(mail_msg, "html", "utf-8")

message["From"] = formataddr(("ZZE", sender,))
message["To"] = ','.join(receivers)
# 邮件标题
subject = '邮件测试'
message['Subject'] = Header(subject, 'utf-8')

try:
    # smtpObj = smtplib.SMTP()
    # smtpObj.connect(mail_host, 25)
    smtp_obj = smtplib.SMTP_SSL(mail_host)
    smtp_obj.connect(mail_host, 465)
    smtp_obj.login(sender, sender_pass)
    smtp_obj.sendmail(sender, receivers, message.as_string())
    smtp_obj.quit()
    print("邮件发送成功")
except smtplib.SMTPException:
    print("Error: 无法发送邮件")

图片.png

带附件的邮件

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

# 邮箱服务器
mail_host = "smtp.qq.com"
# 发件人
sender = "xxx@qq.com"
# 密码
sender_pass = "xxx"
# 收件人,可有多个
receivers = ("xxx@qq.com",)
# 创建一个带附件的实例
message = MIMEMultipart()
# 邮件正文内容
message.attach(MIMEText('这是带附件的邮件发送测试', 'plain', 'utf-8'))
# 附件 1 的路径
file1_path = 'test1.csv'
# 附件 2 的路径
file2_path = 'test2.csv'

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

# 构造附件 2,传送当前目录下的 test2.csv 图片
att2 = MIMEText(open(file2_path, 'rb').read(), 'base64', 'utf-8')
att2["Content-Type"] = 'application/octet-stream'
att2["Content-Disposition"] = 'attachment; filename="{}"'.format(file2_path)
message.attach(att2)

message["From"] = formataddr(("ZZE", sender,))
message["To"] = ','.join(receivers)
# 邮件标题
subject = '带附件的邮件测试'
message['Subject'] = Header(subject, 'utf-8')

try:
    # smtpObj = smtplib.SMTP()
    # smtpObj.connect(mail_host, 25)
    smtp_obj = smtplib.SMTP_SSL(mail_host)
    smtp_obj.connect(mail_host, 465)
    smtp_obj.login(sender, sender_pass)
    smtp_obj.sendmail(sender, receivers, message.as_string())
    smtp_obj.quit()
    print("邮件发送成功")
except smtplib.SMTPException:
    print("Error: 无法发送邮件")

图片.png

带图片的富文本邮件

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

# 邮箱服务器
mail_host = "smtp.qq.com"
# 发件人
sender = "xxx@qq.com"
# 密码
sender_pass = "xxx"
# 收件人,可有多个
receivers = ("xxx@qq.com",)
# 创建一个根实例
msg_root = MIMEMultipart("related")
# 创建一个存放正文的实例,并添加进根实例
msg_alternative = MIMEMultipart('alternative')
msg_root.attach(msg_alternative)

mail_msg = """
<p>Python 邮件发送测试...</p>
<p>图片演示:</p>
<p><img src="cid:0"></p>
"""
msg_alternative.attach(MIMEText(mail_msg, 'html', 'utf-8'))

# 创建图片实例,并添加进根实例
fp = open('zze.png', 'rb')
msg_image = MIMEImage(fp.read())
fp.close()

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

msg_root["From"] = formataddr(("ZZE", sender,))
msg_root["To"] = ','.join(receivers)
# 邮件标题
subject = 'HTML 带图片的邮件测试'
msg_root['Subject'] = Header(subject, 'utf-8')

try:
    # smtpObj = smtplib.SMTP()
    # smtpObj.connect(mail_host, 25)
    smtp_obj = smtplib.SMTP_SSL(mail_host)
    smtp_obj.connect(mail_host, 465)
    smtp_obj.login(sender, sender_pass)
    smtp_obj.sendmail(sender, receivers, msg_root.as_string())
    smtp_obj.quit()
    print("邮件发送成功")
except smtplib.SMTPException:
    print("Error: 无法发送邮件")

图片.png

Copyright: 采用 知识共享署名4.0 国际许可协议进行许可

Links: https://www.zze.xyz/archives/python-smtplib.html

Buy me a cup of coffee ☕.