email dataframe as table in mail body using python

huangapple go评论65阅读模式
英文:

email dataframe as table in mail body using python

问题

以下是代码部分的翻译:

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

email_df = mail_df  # (mail_df 是我需要发送的数据框架)

mail_df.to_string(index=False)
body1 = '''亲爱的员工,

请查看以下详情。
请确认这些条目,并在需要更改时登录 - 
abcd.com。
''' + '\n\n'

mail_df = mail_df[['IDNo', 'Value1', 'Value2', 'Value3']]

host = "host.name.com"

TO = "pqr@gmail.com"

msg = MIMEMultipart()

msg['Subject'] = "邮件主题"
msg['From'] = 'xyz@gmail.com'
html = """
<html>
    <head></head>
        <body>
            {0}
        </body>
</html>
""".format(mail_df.to_html(index=False))

part1 = MIMEText(html, 'html')

msg.attach(part1)

server = smtplib.SMTP(host)
server.starttls()
server.sendmail(msg['From'], TO , msg.as_string())
server.close()

请注意,代码中的注释是我添加的,以帮助您更好地理解代码的功能。如果您需要进一步的帮助,请随时提问。

英文:

I am working on a python project where I am needed to send a mail that contains some text and a dataframe. I need the dataframe to be added to the mail body as a table and not an attachment. The dataframe would not have more than 5-6 columns and 10-15 rows. No matter what I try, the dataframe is getting attached as an html file.

I am sorry for any lapses in the format of the question. Thanks in advance. I am using the below code right now :

'''

    from pretty_html_table import build_table
    from email.mime.text import MIMEText
    from email.mime.multipart import MIMEMultipart
    import smtplib
    
    
    email_df  = mail_df *(mail_df is the dataframe I need to send)
    
    mail_df.to_string(index=False)
    body1 = &#39;&#39;&#39;Dear Employee,

        Please find the below details. 
        Please confirm these entries and incase of any changes needed, Log in to - 
        abcd.com. 
        &#39;&#39;&#39; +&#39;\n\n&#39;
    
    mail_df = mail_df[[&#39;IDNo&#39;,&#39;Value1&#39;,&#39;Value2&#39;,&#39;Value3&#39;]]

    host = &quot;host.name.com&quot;
    
    
    TO = &quot;pqr@gmail.com&quot;

    msg = MIMEMultipart()
    
    msg[&#39;Subject&#39;] = &quot;Mail Subject&quot;
    msg[&#39;From&#39;] = &#39;xyz@gmail.com&#39;
    html = &quot;&quot;&quot;\
            &lt;html&gt;
                &lt;head&gt;&lt;/head&gt;
                    &lt;body&gt;
                        {0}
                    &lt;/body&gt;
            &lt;/html&gt;
    &quot;&quot;&quot;.format(mail_df.to_html(index=False))

    part1 = MIMEText(html, &#39;html&#39;)
    
    
    msg.attach(part1)


    server = smtplib.SMTP(host)
    server.starttls()
    server.sendmail(msg[&#39;From&#39;], TO , msg.as_string())
    server.close()

'''

答案1

得分: 2

尝试这个:

import pandas as pd
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# 将 DataFrame 转换为 HTML 表格
html_table = mail_df.to_html(index=False)

# 电子邮件配置
sender_email = 'your_email@example.com'
receiver_email = 'recipient_email@example.com'
subject = '邮件主题'
smtp_server = 'smtp.example.com'
smtp_port = 587
#smtp_username = 'your_email@example.com'
#smtp_password = 'your_email_password'

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

start = '亲爱的员工,\n\n请查看以下详情。\n如果需要进行任何更改,请确认这些条目并登录到 - abcd.com。'

body = start + '\n\n' + html_table

html = f'<html lang="en">\n\t<head></head>\n\t<body>\n\t\t{body}\n\t</body>\n</html>'

# 将 HTML 表格附加到邮件正文
msg.attach(MIMEText(html, 'html'))

# 连接到 SMTP 服务器并发送邮件
try:
    server = smtplib.SMTP(smtp_server, smtp_port)
    server.ehlo()
    server.starttls()
    #server.login(smtp_username, smtp_password)
    server.sendmail(sender_email, receiver_email, msg.as_string())
    server.close()
    print('邮件发送成功!')
except Exception as e:
    print('发送邮件时出现问题。')
    print(e)
英文:

Try this:

import pandas as pd
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText


# Convert DataFrame to HTML table
html_table = mail_df.to_html(index=False)

# Email configuration
sender_email = &#39;your_email@example.com&#39;
receiver_email = &#39;recipient_email@example.com&#39;
subject = &#39;Mail Subject&#39;
smtp_server = &#39;smtp.example.com&#39;
smtp_port = 587
#smtp_username = &#39;your_email@example.com&#39;
#smtp_password = &#39;your_email_password&#39;

# Construct the email
msg = MIMEMultipart()
msg[&#39;From&#39;] = sender_email
msg[&#39;To&#39;] = receiver_email
msg[&#39;Subject&#39;] = subject

start = &#39;Dear Employee,\n\nPlease find the below details.\nPlease confirm these entries and Log in to - abcd.com if any changes are needed.&#39;

body = start + &#39;\n\n&#39; + html_table

html = f&#39;&lt;html lang=&quot;en&quot;&gt;\n\t&lt;head&gt;&lt;\\head&gt;\n\t&lt;body&gt;\n\t\t{body}\n\t&lt;\\body&gt;\n&lt;\\html&gt;&#39;

# Attach the HTML table to the email body
msg.attach(MIMEText(html, &#39;html&#39;))

# Connect to the SMTP server and send the email
try:
    server = smtplib.SMTP(smtp_server, smtp_port)
    server.ehlo()
    server.starttls()
    #server.login(smtp_username, smtp_password)
    server.sendmail(sender_email, receiver_email, msg.as_string())
    server.close()
    print(&#39;Email sent successfully!&#39;)
except Exception as e:
    print(&#39;Something went wrong while sending the email.&#39;)
    print(e)

答案2

得分: 1

你可以使用EmailMessage接口来简化它(自Python 3.3起可用)

from email.message import EmailMessage
import smtplib

# 在HTML中编写标题
body1 = '''<p>亲爱的员工,</p>
    <p>请查看以下详细信息。<br>
    如有任何更改需要,请登录到abcd.com。</p>
'''

host = "host.name.com"

TO = "pqr@gmail.com"

msg = EmailMessage()

msg['Subject'] = "邮件主题"
msg['From'] = 'xyz@gmail.com'
msg['To'] = TO

# 在同一HTML块中编写标题和表格
html = '''\
        <html>
            <head></head>
                <body>
                    {0}
                    {1}
                </body>
        </html>
'''.format(body1, mail_df.to_html(index=False))

# 将HTML设置为邮件内容
msg.set_content(html, subtype='html')

server = smtplib.SMTP(host)
server.starttls()

# 发送邮件
server.send_message(msg)
server.close()
英文:

You could make it much simpler with the EmailMessage interface (around since Python 3.3)

from email.message import EmailMessage
import smtplib

# Write the header in HTML
body1 = &#39;&#39;&#39;&lt;p&gt;Dear Employee,&lt;/p&gt;
    &lt;p&gt;Please find the below details.&lt;br&gt;
    Please confirm these entries and incase of any changes needed, Log in to abcd.com.&lt;/p&gt;
&#39;&#39;&#39;


host = &quot;host.name.com&quot;

TO = &quot;pqr@gmail.com&quot;

msg = EmailMessage()

msg[&#39;Subject&#39;] = &quot;Mail Subject&quot;
msg[&#39;From&#39;] = &#39;xyz@gmail.com&#39;
msg[&#39;To&#39;] = TO

# write the header and the table in the same html bloc
html = &quot;&quot;&quot;\
        &lt;html&gt;
            &lt;head&gt;&lt;/head&gt;
                &lt;body&gt;
                    {0}
                    {1}
                &lt;/body&gt;
        &lt;/html&gt;
&quot;&quot;&quot;.format(body1, mail_df.to_html(index=False))

# set that html as the content of the message
msg.set_content(html, subtype=&#39;html&#39;)


server = smtplib.SMTP(host)
server.starttls()

# and send it...                        
server.send_message(msg)
server.close()

huangapple
  • 本文由 发表于 2023年6月5日 14:21:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76403938.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定