简体   繁体   中英

How to send automatic email through python with data from a pandas DF

I have a pandas df with names, emails and others informations. I want to use the email columns to send automatic emails through python.

So, firstly I import the libraries:

import smtplib
import email.message
import pandas as pd

After I upload my excel file as as df:

df = pd.read_excel('archive.xlsx')

thirdly, I created I function called send_email():

def send_email(row):

Inside this function I do an iteration over the rows of my df:

for row in range(0, len(df)):
    office = df['OFFICE'].iloc[row]
    name = df['NAME'].iloc[row]
    email = df['EMAIL'].iloc[row]

Then a variable with the message in HTML:

text = """
<html>        
    <p>blablbalbalblablablablablablabalbalabalbalbalbalbalbalbalbal</p>
</html>
"""

And finally I try to send my email:

    msg = email.message.Message()
    msg['Subject'] = "Subject of my email to {name}" # here should go a different name from my df in each iteraction
    msg['From'] = 'myemail@thing.com'
    msg['To'] = '{email}' # here should go a different email from my df in each iteraction
    password = 'password from google security password stuffs' 
    msg.add_header('Content-Type', 'text/html')
    msg.set_payload(text)

    s = smtplib.SMTP('smtp.gmail.com: 587')
    s.starttls()
    # Login Credentials for sending the mail
    s.login(msg['From'], password)
    s.sendmail(msg['From'], [msg['To']], msg.as_string().encode('utf-8'))
return print('Your email has been sent')

send_email()

However it happens anything. All the time shows "TypeError: send_email() missing 1 required positional argument: 'row'" but "row" is defined. Where precisely am I doing wrong in my code?

Your function definition

def send_email(row):

stipulates exactly one argument, whilst you are calling

send_email()

that is furnish it with zero arguments. Either rework definition that it does not require any argument or furnish call with suitable argument.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM