【问题标题】:Trying to grab HTML-code from local file to my email sender试图从本地文件中获取 HTML 代码到我的电子邮件发件人
【发布时间】:2020-12-02 00:07:24
【问题描述】:

我有一个代码将电子邮件地址列表加载到我的电子邮件发件人中,通常我在代码中使用 HTML 代码和纯文本(多部分)发送它,但我想从 . txt / .html 文件改为本地。

这样我可以更好地组织它,代码也会更少。

我在这里尝试使用 pd.read_html,就像 pd.read_excel 适用于我正在上传的列表一样,但我无法让它工作。

代码如下:

import pandas as pd
import time

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

email_list = pd.read_excel('/home/seniori/Documents/Folder/test.xlsx')
template = pd.read_html('/home/seniori/Documents/Folder/test.html')


emails = email_list['EMAIL']

my_list = email_list['EMAIL'].tolist()
print(my_list)
msg = MIMEMultipart()
msg['From'] = "email@example.com"

msg['Subject'] = "Subject"

message = template


message2 = "plain text"
msg.attach(MIMEText(message,'html'))
msg.attach(MIMEText(message2,'plain'))

这是错误:

>>> email_list = pd.read_excel('/home/seniori/Documents/Leadlists/UK/test.xlsx')
>>> template = pd.read_html('/home/seniori/Documents/Leadlists/UK/test.html')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/seniori/anaconda3/lib/python3.8/site-packages/pandas/io/html.py", line 1085, in read_html
    return _parse(
  File "/home/seniori/anaconda3/lib/python3.8/site-packages/pandas/io/html.py", line 915, in _parse
    raise retained
  File "/home/seniori/anaconda3/lib/python3.8/site-packages/pandas/io/html.py", line 895, in _parse
    tables = p.parse_tables()
  File "/home/seniori/anaconda3/lib/python3.8/site-packages/pandas/io/html.py", line 213, in parse_tables
    tables = self._parse_tables(self._build_doc(), self.match, self.attrs)
  File "/home/seniori/anaconda3/lib/python3.8/site-packages/pandas/io/html.py", line 545, in _parse_tables
    raise ValueError("No tables found")
ValueError: No tables found

非常感谢您对此的帮助。提前致谢!

【问题讨论】:

  • HTML 文件中有表格吗?这是 pandas 期望在 HTML 中定义表格的方式:“此函数搜索 元素,并且仅搜索 和 或
    行以及 元素中的每个
    元素桌子。” -- 来自 pandas API 文档

标签: python html pandas email smtplib


【解决方案1】:

如 cmets 所示,read_html 不适用于读取任意 HTML。您可以使用它来加载以 HTML 格式存储的表格。我认为你需要这些方面的东西:

import pandas
import smtplib

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

# I don't have your files, but this should work
# email_list = pandas.read_excel('/home/seniori/Documents/Folder/test.xlsx')
# with open('/home/seniori/Documents/Folder/test.html', "r") as template_file:
#     template = template_file.read()

# Assuming the content of the files looks something like this:
email_list = pandas.DataFrame([
    {
        "EMAIL": "a@b.com",
        "NAME": "Person A"
    },
    {
        "EMAIL": "c@d.com",
        "NAME": "Person C"
    }
])

html_template = """
<div>
<p>Dear {NAME},</p>
<p>Bla <b>bla</b>.</p>
<p>Kind regards,</p>
<p><i>{ME}</i></p>
</div>
"""

text_template = """
Dear {NAME},
Bla bla.
Kind regards,
{ME}
"""


# Create an SMTP server to send the emails
# smtp_server = smtplib.SMTP('localhost')


# Loop the people in your email_list
for _, to in email_list.iterrows():
    print(to)

    # Create a new message
    message = MIMEMultipart()

    # Set the metadata of the message
    message["Subject"] = "My subject here"
    message["From"] = "email@example.com"
    message["To"] = to["EMAIL"]

    # Fill the templates with the parameters
    content_html = html_template.format(**to.to_dict(), **{"ME": "garymaker"})
    content_plain = text_template.format(**to.to_dict(), **{"ME": "garymaker"})

    message.attach(MIMEText(content_html, "html"))
    message.attach(MIMEText(content_plain, "plain"))

    # Send the message
    # smtp_server.send_message(message)
    print(message)


# Stop the SMTP server
# smtp_server.quit()

输出:

EMAIL     a@b.com
NAME     Person A
Name: 0, dtype: object
Content-Type: multipart/mixed; boundary="===============1933032015397261065=="
MIME-Version: 1.0
Subject: My subject here
From: email@example.com
To: a@b.com

--===============1933032015397261065==
Content-Type: text/html; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit


<div>
<p>Dear Person A,</p>
<p>Bla <b>bla</b>.</p>
<p>Kind regards,</p>
<p><i>garymaker</i></p>
</div>

--===============1933032015397261065==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit


Dear Person A,
Bla bla.
Kind regards,
garymaker

--===============1933032015397261065==--

EMAIL     c@d.com
NAME     Person C
Name: 1, dtype: object
Content-Type: multipart/mixed; boundary="===============3497697477590467437=="
MIME-Version: 1.0
Subject: My subject here
From: email@example.com
To: c@d.com

--===============3497697477590467437==
Content-Type: text/html; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit


<div>
<p>Dear Person C,</p>
<p>Bla <b>bla</b>.</p>
<p>Kind regards,</p>
<p><i>garymaker</i></p>
</div>

--===============3497697477590467437==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit


Dear Person C,
Bla bla.
Kind regards,
garymaker

--===============3497697477590467437==--

注意:我没有 SMTP 服务器,所以我已经注释掉了这些行。

【讨论】:

    【解决方案2】:

    我通过添加以下内容解决了它:

    import urllib.request
    
    template = urllib.request.urlopen('file:///home/siri/Folder/test.html').read()
    
    message = template
    
    msg.attach(MIMEText(message,'html', 'utf-8'))
    

    这些是添加到代码中的内容。据我了解(如果我错了,请纠正我) urllib.request-module 使它能够在定义它时读取 UTF-8 编码的 .HTML 文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-06
      • 1970-01-01
      • 2010-10-07
      • 1970-01-01
      • 2020-03-25
      • 1970-01-01
      • 2014-09-07
      相关资源
      最近更新 更多