用python代码发送带excel附件的电子邮件(每天一个python小项目)

需要开启SMTP服务:
在这里插入图片描述

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

# 配置邮箱服务器信息
mail_host = "smtp.163.com"   # 设置服务器
mail_user = "ABC"     # 用户名
mail_pass = "XXXXXXXXXXXX"  # 密码不是登录密码,是IMAP口令

# 配置发件人、收件人信息
sender = 'ABC@163.com' # 发件人邮箱
receivers = ['1XXXXXXXX7@qq.com']  # 接收邮件,可设置为多个邮箱


def message_config():
    """
    配置邮件信息
    :return: 消息对象
    """
    # 第三方 SMTP 服务
    mail_msg = """
        <h2>欢迎来到百度</h2>
        <p><a href="http://www.baidu.com">百度官方网址</a></p>
        <a><img src="https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png"></a>
    """
    content = MIMEText(mail_msg, 'html', 'utf-8')
    message = MIMEMultipart() # 多个MIME对象
    message.attach(content)  # 添加内容
    message['From'] = Header("FU", 'utf-8') # 发件人
    message['To']   = Header("老傅", 'utf-8')  # 收件人
    message['Subject'] = Header('Python课程数据', 'utf-8') # 主题
    # 添加Excel类型附件
    file_name = 'D:/桌面/test.xlsx' # 文件名
    file_path = os.path.join(file_name)        # 文件路径
    xlsx = MIMEApplication(open(file_path, 'rb').read())  # 打开Excel,读取Excel文件
    xlsx["Content-Type"] = 'application/octet-stream'     # 设置内容类型
    xlsx.add_header('Content-Disposition', 'attachment', filename=file_name) # 添加到header信息
    message.attach(xlsx)
    return message

def send_mail(message):
    """
    发送邮件
    :param message: 消息对象
    :return: None
    """
    try:
        smtpObj = smtplib.SMTP_SSL(mail_host) # 使用SSL连接邮箱服务器
        smtpObj.login(mail_user, mail_pass)   # 登录服务器
        smtpObj.sendmail(sender, receivers, message.as_string()) # 发送邮件
        print("邮件发送成功")
    except Exception as e:
        print(e)

if __name__ == "__main__":
    print("开始执行")
    message = message_config() # 调用配置方法
    send_mail(message)         # 发送邮件
    print("执行结束")

运行结果:
在这里插入图片描述

  • 2
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于 Python 的示例代码,用于从 Excel 文件中读取电子邮件地址和内容,并将内容作为附件发送给相应的电子邮件地址: ```python import pandas as pd import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.application import MIMEApplication # 读取 Excel 文件中的电子邮件地址和内容 df = pd.read_excel("file.xlsx", sheet_name="Sheet1") # SMTP 服务器设置 smtp_server = "smtp.example.com" smtp_port = 587 smtp_username = "your_username" smtp_password = "your_password" # 登录 SMTP 服务器 smtp = smtplib.SMTP(smtp_server, smtp_port) smtp.starttls() smtp.login(smtp_username, smtp_password) # 循环遍历每个电子邮件地址并发送邮件 for index, row in df.iterrows(): to_email = row["Email"] subject = "Your subject here" body = row["Content"] # 构建邮件主体 message = MIMEMultipart() text = MIMEText(body) message.attach(text) # 添加附件 with open("attachment_file", "rb") as f: attach = MIMEApplication(f.read(), _subtype="pdf") attach.add_header("Content-Disposition", "attachment", filename="attachment_filename") message.attach(attach) message["Subject"] = subject message["From"] = smtp_username message["To"] = to_email # 发送邮件 smtp.sendmail(smtp_username, to_email, message.as_string()) # 关闭 SMTP 连接 smtp.quit() ``` 请注意,您需要将代码中的以下值替换为适当的值: - `file.xlsx`:包含电子邮件地址和内容的 Excel 文件名。 - `Sheet1`:包含电子邮件地址和内容的工作表名称。 - `smtp_server`:您的 SMTP 服务器的主机名或 IP 地址。 - `smtp_port`:您的 SMTP 服务器的端口号。 - `smtp_username`:您的 SMTP 服务器的用户名。 - `smtp_password`:您的 SMTP 服务器的密码。 - `attachment_file`:要附加到电子邮件的文件路径和名称。 - `attachment_filename`:要附加到电子邮件的文件名称。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值