简体   繁体   中英

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

I am using Sendgrid to send out emails to specific departments but I want the email to include data from a csv file. From my understanding, Sendgrid works with HTML. How would it be possible to scrape a csv file and send it using 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)

I tried using the csv library but received an error. I did change the email address for this post.

You can use pandas for read the csv and parse to html string using io...and later to add variable html_str inside of your html_content...for use pandas just install with 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM