2

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.

Roberto Gonzalez
  • 37
  • 1
  • 2
  • 9

1 Answers1

-1

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)
GiovaniSalazar
  • 1,999
  • 2
  • 8
  • 15