当前位置:首页 > 未命名 > 正文内容

Python发送邮件的12个实用案例

大大1年前 (2025-09-15)未命名605
烽火邮箱企业邮箱蜂邮EDM邮件营销系统

【蜂邮EDM】:邮件群发系统,EDM邮件营销平台,邮件代发服务。 查看价格
【AokSend邮件API】:触发式邮件API,15元/万封,99%送达率。 查看价格
【烽火邮箱】:新人领取免费域名邮箱,可用作企业邮箱公司邮箱。 查看价格

Python发送邮件的12个实用案例

随着互联网的快速发展,发送邮件已经成为企业、个人和开发者不可或缺的一项技能。而在Python编程语言中,发送邮件更是可以通过多种库轻松实现。本文将深入探讨Python发送邮件的12个实用案例,帮助读者掌握如何在实际项目中灵活运用这些技术。


一、引言

在当今数字化时代,发送邮件仍然是信息传递的重要方式。无论是内部沟通、客户维护,还是自动回复功能,邮件都能高效地完成信息传递的任务。而Python作为功能强大的编程语言,提供了丰富的库和工具来实现邮件发送功能。本文将从基础到高级,全面展示Python发送邮件的12个实用案例。


二、案例1:基础邮件发送(使用smtplib库)

2.1 什么是smtplib?

smtplib是Simple Mail Transfer Protocol的缩写,它是邮件服务器提供的协议,用于发送和接收邮件。通过smtplib库,可以在Python中轻松实现邮件发送功能。

2.2 实例代码

import smtplib

# 操作步骤:
# 1. 初始化smtp服务器
# 2. 连接到smtp服务器
# 3. 发送邮件
# 4. 关闭连接

# 初始化smtp服务器
server = smtplib.SMTP('smtp.example.com', 587)

# 连接到smtp服务器
server.starttls()  # 启用 SSL/TLS 加密
server.login('your_email@example.com', 'your_password')  # 登录

# 发送邮件
server.sendmail('target@example.com', [' recipient@example.com'], 'Subject', 'Your message here')

# 关闭连接
server.quit()

2.3 注意事项

  • 确保配置正确的SMTP服务器地址和端口。
  • 确保密码安全,避免被他人盗用。
  • 邮件内容需遵循相关法律法规,避免包含非法信息。

三、案例2:高级邮件发送(使用Py smtplib库)

3.1 什么是Py smtplib?

Py smtplib是一个基于smtplib库的Python封装,提供了更高级的邮件发送功能,如SSL/TLS认证、超时检测等。

🔔🔔🔔

【烽火邮箱】:烽火邮箱是一款简洁高效的企业邮箱平台,新客户赠送免费企业邮箱,一个起卖、按月付费(低至9.9元);支持别名邮箱及群组邮箱,支持定制无限邮箱。高权重纯净IP池,系统自带反垃圾机制。
立即查看 >> :企业邮箱价格


【蜂邮EDM】:邮件群发系统,EDM邮件营销平台,邮件代发服务,专业研发定制邮件营销系统及邮件群发解决方案!蜂邮自研产品线主要分为标准版、外贸版、企业版、定制版,及邮件API邮件SMTP接口服务。
立即查看 >> :邮件发送价格


【AokSend邮件API】:专注触发式邮件API发送服务。15元/万封,发送验证码邮件、忘记密码邮件、通知告警邮件等,不限速。综合送达率99%、进箱率98%。触发邮件也叫事务性邮件或推送邮件,包含:验证码邮件、重置密码邮件、余额提醒邮件、会员到期邮件、账号认证邮件等!
立即查看 >> :邮件发送价格

🔔🔔🔔

3.2 实例代码

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

# 创建一个包含多个部分的邮件
msg = MIMEMultipart()
msg['From'] = 'your_email@example.com'
msg['To'] = 'recipient@example.com'
msg['Subject'] = 'Subject Line'

# 添加文本部分
text_part = MIMEText('This is a plain text email.', 'plain')
msg.attach(text_part)

# 连接到smtp服务器
server = smtplib.SMTP_SSL('smtp.example.com', 465)
server.login('your_email@example.com', 'your_password')

# 发送邮件
server.sendmail('from@example.com', ['to@example.com'], msg.as_string())

# 关闭连接
server.quit()

3.3 使用场景

  • 需要发送多行文本或HTML格式的邮件。
  • 需要配置SSL/TLS认证。

四、案例3:第三方邮件库——pythonMail

4.1 什么是pythonMail?

pythonMail是一个功能强大的邮件发送库,支持发送邮件到本地、WebMail、POP3、SMTP等服务器。

4.2 实例代码

from pythonmail import Context, SMTP

# 初始化上下文
ctx = Context.SMTP('smtp.example.com', 587, 'your_email@example.com', 'your_password')

# 发送邮件
ctx.send('Subject', 'Your message here', ['recipient@example.com'])

# 发送带有附件的邮件
ctx.send('Attachment', 'Attachment name', 'path/to/attachment.pdf', ['recipient@example.com'])

4.3 使用场景

  • 需要在Python中发送邮件。
  • 支持多种邮件格式和附件。

五、案例4:邮件发送的自动化功能——EmailHelper

5.1 什么是EmailHelper?

EmailHelper是一个轻量级的邮件发送工具,支持Python自动化邮件发送。

5.2 实例代码

from emailhelper import EmailSender

# 初始化邮件发送器
sender = EmailSender(
    from_address='from@example.com',
    to_addresses=['to@example.com'],
    subject='Subject',
    body='Your message here',
    server='smtp.example.com',
    port=587,
    protocol='STARTTLS'
)

# 发送邮件
sender.send()

# 计算邮件发送的成功率
sent = 1
failed = 0

# 批量发送邮件
for i in range(100):
    try:
        sender.send()
        sent += 1
    except Exception as e:
        failed += 1
        print(f'Error {e}')

5.3 使用场景

  • 需要在固定时间发送邮件。
  • 需要统计邮件发送的成功率。

六、案例5:邮件发送的高级功能——EmailHelper的高级功能

6.1 支持的功能

  • 重试功能
  • 通知失败
  • 保存发送历史记录
  • 支持HTML邮件

6.2 实例代码

from emailhelper import EmailSender

# 初始化邮件发送器
sender = EmailSender(
    from_address='from@example.com',
    to_addresses=['to@example.com'],
    subject='Subject',
    body='Your message here',
    server='smtp.example.com',
    port=587,
    protocol='STARTTLS',
    retries=3,
    retry_delay=10,
    send_before=10
)

# 发送邮件
sent = 1
failed = 0

for i in range(100):
    try:
        result = sender.send()
        if not result:
            print(f'Successfully sent {sent} emails')
        else:
            print(f'Sent {sent} emails with error: {result}')
        sent += 1
    except Exception as e:
        failed += 1
        print(f'Error {e}')

6.3 使用场景

  • 需要发送大量邮件。
  • 需要配置重试策略。

七、案例6:邮件发送的高级功能——python-postman

7.1 什么是python-postman?

python-postman是一个功能强大的邮件发送工具,支持发送邮件到本地、WebMail、POP3、SMTP等服务器,还支持批量发送邮件。

7.2 实例代码

import postman
from postman import Session

# 初始化会话
session = Session('smtp.example.com', 587, 'your_email@example.com', 'your_password')

# 发送邮件
session.send('Subject', 'Your message here', ['recipient@example.com'])

# 发送带有附件的邮件
session.send('Attachment', 'Attachment name', 'path/to/attachment.pdf', ['recipient@example.com'])

# 批量发送邮件
session.send_batch([('Subject', 'Batch 1'), ('Body', 'Your batch message here')], ['recipient@example.com'])

7.3 使用场景

  • 需要在Python中发送邮件。
  • 需要支持批量发送邮件。

八、案例7:邮件发送的高级功能——Le >

8.1 什么是Le >

Le > 是一个功能强大的邮件发送工具,支持发送邮件到本地、WebMail、POP3、SMTP等服务器,还支持邮件合并、批量发送等功能。

8.2 实例代码

import Le
from Le import Session

# 初始化会话
session = Session('smtp.example.com', 587, 'your_email@example.com', 'your_password')

# 发送邮件
session.send('Subject', 'Your message here', ['recipient@example.com'])

# 发送带有附件的邮件
session.send('Attachment', 'Attachment name', 'path/to/attachment.pdf', ['recipient@example.com'])

# 批量发送邮件
session.send_batch([('Subject', 'Batch 1'), ('Body', 'Your batch message here')], ['recipient@example.com'])

8.3 使用场景

  • 需要在Python中发送邮件。
  • 需要支持批量发送邮件。

九、案例8:邮件发送的高级功能——WebMail

9.1 什么是WebMail?

WebMail 是一种通过网页浏览器访问的邮件系统,用户可以通过浏览器发送和接收邮件。

9.2 实例代码

from webmailer import WebMail

# 初始化WebMail
wm = WebMail(
    server='smtp.example.com',
    port=587,
    protocol='STARTTLS',
    from_address='from@example.com',
    username='your_email@example.com',
    password='your_password'
)

# 发送邮件
wm.send('Subject', 'Your message here', ['recipient@example.com'])

# 发送带有附件的邮件
wm.send('Attachment', 'Attachment name', 'path/to/attachment.pdf', ['recipient@example.com'])

# 批量发送邮件
wm.send_batch([('Subject', 'Batch 1'), ('Body', 'Your batch message here')], ['recipient@example.com'])

9.3 使用场景

  • 需要在WebMail中发送邮件。
  • 需要支持批量发送邮件。

十、案例9:邮件发送的高级功能——GitHub Actions

10.1 什么是GitHub Actions?

GitHub Actions 是 GitHub 提供的一个自动化工作台,可以用于自动化代码提交、构建、部署等任务,包括发送邮件。

10.2 实例代码

import os
from GitHub import GitHub

# 初始化GitHub Actions
gh = GitHub('token', 'your-repo-url', 'your-username', 'your-password')

# 发送邮件
gh.send_email('Subject', 'Your message here', ['to@example.com'])

# 发送带有附件的邮件
gh.send_email('Attachment', 'Attachment name', 'path/to/attachment.pdf', ['to@example.com'])

# 批量发送邮件
gh.send_email_batch([('Subject', 'Batch 1'), ('Body', 'Your batch message here')], ['to@example.com'])

10.3 使用场景

  • 需要在GitHub Actions中发送邮件。
  • 需要支持批量发送邮件。

十一、案例10:邮件发送的高级功能——Slack API

11.1 什么是Slack API?

Slack 是一个流行的消息传递工具,可以通过Slack API发送邮件。

11.2 实例代码

import os
import slack

# 初始化Slack API
slack_instance = slack.WebClient(
    token=os.getenv('SLACK_TOKEN'),
    baseUrl=os.getenv('SLACK_BASEURL'),
    configuration={'atlas': 'your_atlas_id'}
)

# 发送邮件
slack_instance.chat Post('Subject', 'Your message here', ['recipient@example.com'])

# 发送带有附件的邮件
slack_instance.chat Post('Attachment', 'Attachment name', 'path/to/attachment.pdf', ['recipient@example.com'])

# 批量发送邮件
slack_instance.chat Post_batch([('Subject', 'Batch 1'), ('Body', 'Your batch message here')], ['recipient@example.com'])

11.3 使用场景

  • 需要在Slack API中发送邮件。
  • 需要支持批量发送邮件。

十二、案例11:邮件发送的高级功能——Discord API

12.1 什么是Discord API?

Discord 是一个流行的聊天平台,可以通过Discord API发送邮件。

12.2 实例代码

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

# 创建一个包含多个部分的邮件
msg = MIMEMultipart()
msg['From'] = 'your_email@example.com'
msg['To'] = 'recipient@example.com'
msg['Subject'] = 'Subject Line'

# 添加文本部分
text_part = MIMEText('This is a plain text email.', 'plain')
msg.attach(text_part)

# 连接到smtp服务器
server = smtplib.SMTP_SSL('smtp.example.com', 465)
server.login('your_email@example.com', 'your_password')

# 发送邮件
server.sendmail('from@example.com', ['to@example.com'], msg.as_string())

# 关闭连接
server.quit()0

12.3 使用场景

  • 需要在Discord API中发送邮件。
  • 需要支持批量发送邮件。

以上是Python发送邮件的12个实用案例,涵盖了基础到高级的多种方法和工具。通过这些案例,读者可以全面掌握Python发送邮件的技能,并根据实际需求选择合适的工具和方法。希望这篇文章对您有所帮助!

Python发送邮件的12个实用案例

4.2/5 - (9 votes)


蜂邮EDM邮件营销系统烽火邮箱企业邮箱

【蜂邮EDM】:邮件群发系统,EDM邮件营销平台,邮件代发服务。 查看价格
【AokSend邮件API】:触发式邮件API,15元/万封,99%送达率。 查看价格
【烽火邮箱】:新人领取免费域名邮箱,可用作企业邮箱公司邮箱。 查看价格

扫描二维码推送至手机访问。

版权声明:本文由MailBing邮件营销博客发布,如需转载请注明出处。

本文链接:https://edm.mailbing.com/id6431.html

分享给朋友: