|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
m_text = MIMEText(text, 'html', 'utf-8')这里html改成plain,正文就没法显示图片,html的话,附件的xlsx文件打开内容就是乱码。
求助,怎么让正文显示图片,附件xlsx也正常显示。
- import smtplib
- from pathlib import Path
- from email.mime.application import MIMEApplication
- from email.mime.base import MIMEBase
- from email import encoders
- from email.mime.image import MIMEImage
- from email.mime.text import MIMEText
- from email.mime.multipart import MIMEMultipart
- from email.header import Header
- # 1.发送人账号
- sendAddress = '****@***.****.com'
- # 2.发送人授权码
- password = '****'
- #(乱写的 要去QQ邮箱找自己的哦)
- # 1定义一个可以添加正文和附件的邮件消息对象
- msg = MIMEMultipart()
- #
- To = ['13973519903@139.com']
- # Cc = ['13975789908@139.com', '13787775557@139.com']
- Cc = []
- reciver = To + Cc
- msg['From'] = sendAddress
- msg['To'] = ','.join(To)
- msg['Cc'] = ','.join(Cc)
- msg['subject'] = '邮件标题'
- text = f"""邮件正文
- <img src="cid:0">
- """
- # html文本引入id为0的图片
- m_text = MIMEText(text, 'html', 'utf-8')
- #这里html改成plain,正文就没法显示图片,html的话,附件的xlsx文件打开内容就是乱码
- msg.attach(m_text)
- # 邮件加入图片
- m_img = MIMEBase('image', 'jpg')
- m_img.add_header('Content-Disposition', 'attachment')
- m_img.add_header('Content-ID', '<0>') # 设置图片id为0
- f = open(r"C:\Users\Nothing\Downloads\2023年07月17日精品企宽重复投诉率日通报.png", "rb") # 读取本地图片
- m_img.set_payload(f.read())
- encoders.encode_base64(m_img)
- msg.attach(m_img)
- # 添加excel附件
- # xlsx = MIMEApplication(open(r"C:\Users\Nothing\Downloads\2023年07月17日精品企宽重复投诉率日通报.xlsx", 'rb').read()) # 打开Excel,读取Excel文件
- xlsx = MIMEText(open(r"C:\Users\Nothing\Downloads\2023年07月17日精品企宽重复投诉率日通报.xlsx", 'rb').read(), 'base64', 'utf-8')
- # xlsx["Content-Type"] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' # 设置内容类型
- xlsx["Content-Type"] = 'application/octet-stream'
- # xlsx['Content-Disposition'] = 'attachment;filename="2023年07月17日精品企宽重复投诉率日通报.xlsx"'
- xlsx.add_header('Content-Disposition', 'attachment', filename="2023年07月17日精品企宽重复投诉率日通报.xlsx")
- # encoders.encode_base64(xlsx)
- msg.attach(xlsx)
- # 3.连通服务器
- # 465端口就是
- server = smtplib.SMTP_SSL('****.*****.com', 465)
- print(server)
- # 4.登录
- loginResult = server.login(sendAddress, password)
- print(loginResult)
- # (235, b'Authentication successful') 这样子的代表你登陆成功了
- # 235响应成功状态码
- # 发送邮件
- try:
- server.sendmail(sendAddress, reciver, msg.as_string())
- print("发送成功")
- email = '成功了'
- server.quit()
- except:
- print("发送失败")
- email = ''
- server.quit()
复制代码
在使用 smtplib 发送邮件时,可以通过嵌入图片作为正文的方式来显示图片,同时保证附件的正常显示。以下是修改后的代码示例:
- import smtplib
- from pathlib import Path
- from email.mime.application import MIMEApplication
- from email.mime.base import MIMEBase
- from email import encoders
- from email.mime.image import MIMEImage
- from email.mime.text import MIMEText
- from email.mime.multipart import MIMEMultipart
- from email.header import Header
- # 1. 发送人账号
- sendAddress = '****@***.****.com'
- # 2. 发送人授权码
- password = '****' # (请替换为自己的授权码)
- # 收件人列表
- To = ['13973519903@139.com']
- Cc = []
- reciver = To + Cc
- # 创建邮件消息对象
- msg = MIMEMultipart()
- msg['From'] = sendAddress
- msg['To'] = ','.join(To)
- msg['Cc'] = ','.join(Cc)
- msg['Subject'] = '邮件标题'
- # 添加邮件正文
- text = """
- <html>
- <body>
- <p>邮件正文</p>
- <img src="cid:0">
- </body>
- </html>
- """
- m_text = MIMEText(text, 'html', 'utf-8')
- msg.attach(m_text)
- # 添加图片附件
- with open(r"C:\Users\Nothing\Downloads\2023年07月17日精品企宽重复投诉率日通报.png", "rb") as f:
- m_img = MIMEImage(f.read())
- m_img.add_header('Content-ID', '<0>') # 设置图片id为0
- msg.attach(m_img)
- # 添加Excel附件
- xlsx_path = r"C:\Users\Nothing\Downloads\2023年07月17日精品企宽重复投诉率日通报.xlsx"
- xlsx = MIMEApplication(open(xlsx_path, 'rb').read())
- xlsx.add_header('Content-Disposition', 'attachment', filename=Path(xlsx_path).name)
- msg.attach(xlsx)
- # 连接邮件服务器并发送邮件
- try:
- server = smtplib.SMTP_SSL('****.*****.com', 465) # 这里请替换为邮件服务器的地址
- loginResult = server.login(sendAddress, password)
- print(loginResult) # (235, b'Authentication successful')
- server.sendmail(sendAddress, reciver, msg.as_string())
- server.quit()
- print("发送成功")
- except Exception as e:
- print("发送失败:", e)
复制代码
请注意替换 sendAddress、 password 和邮件服务器地址为你自己的信息,并确保图片和附件文件的路径正确。同时,邮件正文中的图片引用时,使用了 cid:0,需要根据实际情况进行调整。
如果还有其他问题,请随时提问。
|
|