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='[email protected]',
    to_emails='[email protected]',
    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.

3
  • Include csv Like a attached? or inside the Body? Commented Dec 3, 2019 at 19:25
  • I want to add it inside of the body Commented Dec 3, 2019 at 19:41
  • I think you want this: stackoverflow.com/questions/44320329/… Commented Dec 3, 2019 at 20:32

1 Answer 1

-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)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.