kootenpv / yagmail

Send email in Python conveniently for gmail using yagmail
MIT License
2.64k stars 263 forks source link

How Can I use yagmail to send to a csv list? #180

Open eduardolc21 opened 3 years ago

eduardolc21 commented 3 years ago
yag = yagmail.SMTP('xxxx@gmail.com', 'mypass')

with open("hoja2.csv","r") as csvfile:
    csv_reader = csv.reader(csvfile)
    next(csv_reader)

    for line in csv_reader:
        for name, email, message in tuple(line.strip().split(',') for line in csvfile):
            yag.send(email, subject = name, contents = message)
kootenpv commented 3 years ago

Yea, what is wrong with that?

eduardolc21 commented 3 years ago

I was trying with the code below, but It is not working. PS. I am new to python. Thanks for your answer.

yag = yagmail.SMTP('xxxx@gmail.com', 'mypass')

with open("hoja2.csv","r") as csvfile: csv_reader = csv.reader(csvfile) next(csv_reader)

for line in csv_reader:
    for name, email, message in tuple(line.strip().split(',') for line in csvfile):
        yag.send(email, subject = name, contents = message)
kootenpv commented 3 years ago

It looks like it should just work. Only the yagmail import seems to miss. What's the error you get?

eduardolc21 commented 3 years ago

#Thanks for your answer and comments import yagmail import csv sender_email = "xxxx@gmail.com" subject = "Take a look" sender_password = input(f"Please enter the password {sender_email}:\n") yag = yagmail.SMTP(user = sender_email, password = sender_password) contents = ["This is your first paragraph in our email", "As you can see" ] with open("contacts_file.csv") as csvfile: csv_reader = csv.reader(csvfile) next(csv_reader)

for line in csv_reader:
     for name, email, grade in line.strip().split(','):
        yag.send(email, subject = name, contents = contents)

AttributeError Traceback (most recent call last) in () 7 8 for line in csv_reader: ----> 9 for name, email, grade in line.strip().split(','): 10 yag.send(email, subject = name, contents = contents)

AttributeError: 'list' object has no attribute 'strip'

kootenpv commented 3 years ago

In your case it means line is a list and not a string.