Python脚本自动发送电子邮件

要编写一个Python脚本来自动发送电子邮件,你可以使用smtplib库来处理SMTP协议,以及email库来构建邮件内容。

  1. 安装必要的库

    通常情况下,smtplib和email库是Python标准库的一部分,因此不需要额外安装。如果你使用的是较旧的Python版本,可能需要确保这些库已安装。

  2. 编写脚本

    以下是一个完整的Python脚本示例,用于发送带有附件的电子邮件

c 复制代码
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders

def send_email(sender_email, sender_password, receiver_email, subject, body, attachment_path):
    # 设置SMTP服务器
    smtp_server = 'smtp.example.com'  # 替换为你的SMTP服务器地址
    smtp_port = 587  # 替换为你的SMTP服务器端口

    # 创建邮件对象
    msg = MIMEMultipart()
    msg['From'] = sender_email
    msg['To'] = receiver_email
    msg['Subject'] = subject

    # 添加邮件正文
    msg.attach(MIMEText(body, 'plain'))

    # 添加附件
    if attachment_path:
        attachment = open(attachment_path, 'rb')
        part = MIMEBase('application', 'octet-stream')
        part.set_payload(attachment.read())
        encoders.encode_base64(part)
        part.add_header('Content-Disposition', f'attachment; filename={attachment_path}')
        msg.attach(part)
        attachment.close()

    # 连接SMTP服务器并发送邮件
    try:
        server = smtplib.SMTP(smtp_server, smtp_port)
        server.starttls()  # 启用TLS加密
        server.login(sender_email, sender_password)
        text = msg.as_string()
        server.sendmail(sender_email, receiver_email, text)
        server.quit()
        print("邮件发送成功")
    except Exception as e:
        print(f"邮件发送失败: {e}")

if __name__ == "__main__":
    # 替换为你的发件人邮箱和密码
    sender_email = 'your_email@example.com'
    sender_password = 'your_password'

    # 替换为收件人邮箱
    receiver_email = 'receiver_email@example.com'

    # 邮件主题和正文
    subject = '测试邮件'
    body = '这是一封测试邮件,包含附件。'

    # 附件路径(可选)
    attachment_path = 'example.txt'  # 替换为你的附件文件路径

    # 发送邮件
    send_email(sender_email, sender_password, receiver_email, subject, body, attachment_path)
  1. 运行脚本
    将上述脚本保存为一个Python文件(例如send_email.py),然后在命令行中运行:

python send_email.py

  1. 注意事项
    SMTP服务器:你需要替换smtp_server和smtp_port为你的电子邮件服务提供商的SMTP服务器地址和端口。例如,Gmail的SMTP服务器是smtp.gmail.com,端口是587。

发件人邮箱和密码:你需要替换sender_email和sender_password为你的发件人邮箱地址和密码。对于Gmail,你可能需要生成一个应用专用密码。

收件人邮箱:替换receiver_email为收件人的邮箱地址。

附件:如果你不需要发送附件,可以将attachment_path设置为None。

  1. 安全性
    密码安全:不要在脚本中硬编码密码,尤其是当你将代码分享或上传到公共仓库时。可以考虑使用环境变量或配置文件来管理敏感信息。

TLS加密:确保使用starttls()来启用TLS加密,以保护邮件内容在传输过程中的安全。

相关推荐
李妍.1 小时前
02Numpy基础(上)
开发语言·python
TheBestRucy1 小时前
Python 九阳神功之贰:面向对象(下)
开发语言·python
2601_965958462 小时前
口腔黏膜脱皮超2周未愈建议及时就医
人工智能·python
微小冷3 小时前
在不同空间中展示数据(欧式、球面、庞加莱圆盘)
python·双曲空间·微分几何·geomstats·庞加莱圆盘·球面
weixin_431600443 小时前
Agent Workflow 学习向:Code 节点,比模板更强的变量加工
后端·python·学习·ai·
讲温控就好了3 小时前
从医疗影像到能源存储——芯片冷却温控技术的跨行业赋能
人工智能·python·能源
DeepVisionary4 小时前
从 Qwen3.8-27B 把原生多模态、原生 FP8、桌面级 Agent 三件事一次集齐,看开源大模型的“工业化拐点“
python·自动化
固定资产管理系统软件5 小时前
商务局RFID固定资产管理系统的应用价值与落地要点解析
大数据·python
TheBestRucy5 小时前
Python 九阳神功:从零筑基到线程飞升
服务器·开发语言·网络·人工智能·python
lancyu6 小时前
关于多轮对话机器人的上下文Token优化和解决方案
人工智能·python·深度学习·机器学习·chatgpt·机器人·prompt