如何用colab从gmai.com发邮件到hotmail.com/yahoo.com,文字和图片变得不正常

问题描述 投票:0回答:1

我想将邮件从“[email protected]”发送到多个电子邮件,如 gmail 或 hotmail、yahoo 等

但是,当我发送这条消息时。 hotmail 的文字变成了几个 html 文件,而不是真正的文字。当我用我的iphone阅读这个hotmail时,“address.png”的图片变成了随机数。

有谁知道如何缓解这些问题? 我希望电子邮件包含明文文字和图片。

import numpy as np
import os
import pandas as pd
import csv
from string import Template
import smtplib
from pathlib import Path
from email import policy
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from google.colab import drive 

drive.mount('/content/drive')
df=pd.read_csv('/content/drive/MyDrive/inform_test.csv')
a=np.shape(df)
for k in range(0,a[0]):
   content = MIMEMultipart()  
   content["subject"] = "title"  
   content["from"] = "[email protected]"  
   content["to"] = df.iloc[k,1]
   content.attach(
                MIMEText(df.iloc[k,0],"html"))  
   main_content = "hello world" 
   content.attach(
                MIMEText(main_content,"html"))  
   content.attach(
                MIMEText("<br>","html"))  
   content.attach(
                MIMEText("<br>","html"))   
   content.attach(
                MIMEText("<br>","html"))   
   content.attach(
                MIMEText("phone","html"))                           
   content.attach(
                MIMEText("best regard","html")) 
   content.attach(MIMEImage(Path("/content/drive/MyDrive/mail_test/address.png").read_bytes()))
                        
   #print(k)
   with smtplib.SMTP(host="smtp.gmail.com", port="587") as smtp:  
      try:
          smtp.ehlo()  
          smtp.starttls()  
          smtp.login("[email protected]", "aasjwgeaymtajuks")  
          smtp.send_message(content)  
          print("successful")
      except Exception as e:
          print("Error message: ", e)
python gmail html-email hotmail mimemultipart
1个回答
0
投票

尝试使用 SMTP。它是一个标准的 Python 包。而且语法非常简单。 教程链接 - https://www.youtube.com/watch?v=JRCJ6RtE3xU

© www.soinside.com 2019 - 2024. All rights reserved.