I am using the csv library to pull data to into an email body. I am pulling certain columns from the csv for the body. I am using a junk gmail account to test. I\'m just confuse
Probably if you use pandas for to handle the csv could be more easy ..for install pandas just pip install pandas
import pandas as pd
import io
import smtplib, ssl
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# read csv using pandas and then convert the csv to html string ....this code later will add into body
str_io = io.StringIO()
df = pd.read_csv('test.csv')
df.to_html(buf=str_io)
table_html = str_io.getvalue()
print(table_html)
sender_email = "mymail@gmail.com"
receiver_email = "anothermail@gmail.com"
password = "mypass"
message = MIMEMultipart("alternative")
message["Subject"] = "Subject: Your Title"
message["From"] = sender_email
message["To"] = receiver_email
text = """\
Subject: Your Title"""
html = """\
<html>
<body>
<p>{table_html}</p>
</body>
</html>
""".format(table_html=table_html)
part1 = MIMEText(text, "plain")
part2 = MIMEText(html, "html")
message.attach(part1)
message.attach(part2)
# Send email
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
server.login(sender_email, password)
server.sendmail(
sender_email, receiver_email, message.as_string()
)