英文:
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 = '''Dear Employee,
Please find the below details.
Please confirm these entries and incase of any changes needed, Log in to -
abcd.com.
''' +'\n\n'
mail_df = mail_df[['IDNo','Value1','Value2','Value3']]
host = "host.name.com"
TO = "pqr@gmail.com"
msg = MIMEMultipart()
msg['Subject'] = "Mail 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()
'''
答案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 = 'your_email@example.com'
receiver_email = 'recipient_email@example.com'
subject = 'Mail Subject'
smtp_server = 'smtp.example.com'
smtp_port = 587
#smtp_username = 'your_email@example.com'
#smtp_password = 'your_email_password'
# Construct the email
msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = receiver_email
msg['Subject'] = subject
start = 'Dear Employee,\n\nPlease find the below details.\nPlease confirm these entries and Log in to - abcd.com if any changes are needed.'
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>'
# Attach the HTML table to the email body
msg.attach(MIMEText(html, 'html'))
# 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('Email sent successfully!')
except Exception as e:
print('Something went wrong while sending the email.')
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 = '''<p>Dear Employee,</p>
<p>Please find the below details.<br>
Please confirm these entries and incase of any changes needed, Log in to abcd.com.</p>
'''
host = "host.name.com"
TO = "pqr@gmail.com"
msg = EmailMessage()
msg['Subject'] = "Mail Subject"
msg['From'] = 'xyz@gmail.com'
msg['To'] = TO
# write the header and the table in the same html bloc
html = """\
<html>
<head></head>
<body>
{0}
{1}
</body>
</html>
""".format(body1, mail_df.to_html(index=False))
# set that html as the content of the message
msg.set_content(html, subtype='html')
server = smtplib.SMTP(host)
server.starttls()
# and send it...
server.send_message(msg)
server.close()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论