簡體   English   中英

如何使用 python 從帶有 Sendgrid 的 csv 文件中通過電子郵件發送數據

[英]How to email out data from a csv file with Sendgrid using python

我正在使用 Sendgrid 向特定部門發送電子郵件,但我希望電子郵件包含來自 csv 文件的數據。 根據我的理解,Sendgrid 使用 HTML。 如何抓取csv 文件並使用 Sendgrid 發送它?

message = Mail(
    from_email='noreply@gmail.com',
    to_emails='test@gmail.com',
    subject='New User CAF',
    html_content= """<p>This is to inform IT that {Employee Name} will be starting at {PC} on {Effective Date}. Their supervisor is {Supervisor} and their manager is {Manager 2 Name}. Their title is {Title}.</br>
    </br>
    Office 365: {O365}</br>
    Laptop: {Computer}
    """)

with open("contacts.csv") as file:
        reader = csv.reader(file)
        # skip first header row
        next(reader)

我嘗試使用 csv 庫但收到錯誤。 我確實更改了這篇文章的電子郵件地址。

您可以使用 Pandas 讀取 csv 並使用 io 解析為 html 字符串……然后在您的 html_content 中添加變量 html_str……使用 Pandas 只需安裝 pip install pandas

import pandas as pd
import io

str_io = io.StringIO()
df = pd.read_csv('file.csv')
df.to_html(buf=str_io)
html_str = str_io.getvalue()
print(html_str)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM