I am working on a project where I will have to iterate through a csv file. I want to see if the 4 column(3rd slot) of each row will have an email address already in my dictionary that has the person's name and email address. If they are I want to send them back an attachment. I'm not very familiar with Python, so I want to see if I'm heading in the right direction
Here is a sample code:
import csv
with open("file.csv") as csv_file:
for row in csv.reader(csv_file, delimiter=','):
if row[3] in data_dict:
email = EmailMessage('subject', 'body', [address@something.com])
email.attach_file('/folder/name.csv')
email.send()
When involved with attachments I'd advise the yagmail package (full disclose: I'm the developer)
Obtain it by running:
pip install yagmail # for python 2
pip3 install yagmail # for python3
Then:
import yagmail
yag = yagmail.SMTP('myemail', 'mypassword')
with open("file.csv") as csv_file:
for row in csv.reader(csv_file, delimiter=','):
row = row.split(',')
if row[3] in data_dict:
contents = ['See my file attached', '/folder/name.csv']
yag.send(row[3] + '@email.com', 'subject', contents)
Note that anything that can be opened as a file will be attached (when passed to the contents
argument), so if /folder/name.csv
is a valid path in that list, it will be sent as an attachment.
What about something like this?
with open("file.csv") as f:
for line in f.readlines():
cols = line.split(',')
if cols[3] in data_dict:
email = EmailMessage('subject', 'body', [address@something.com])
email.attach_file('/folder/name.csv')
email.send()
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.