Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
127 views
in Technique[技术] by (71.8m points)

python - Iterate through rows and for each one send an email using the email on the last column

I have a dataframe which contains multiple columns with financial and personal information of customers and advisors. Each customer has a Financial Advisor whose email is located in the last column.

First problem I am facing is that various customers have one Financial Advisor in common so I would need a for loop to group all the customers in one email for the respective Financial Advisor.

Also, I want to select only a few columns to send the information and then send it through to the Financial Advisor's email as a table.

To illustrate:

d = {'Name': [John, Sheila, Bryan], 'Product': [Treasury Bond, Treasury Bond, Treasury Bond], 'Quantity': [10,20,30] , 'Financial Advisor's Name':[ Jack, Jack, Claudia], 'Financial Advisor's Email': [[email protected],[email protected],[email protected]]}


    def create_message(send_from, send_to,subject, plain_text_body):
        sender_address = "[email protected]"
        mail_content="This is a test message"
        for a, b, c, in zip(d['Name'], d['Product'], d['Financial Advisor's Name'], d['Quantity']):
            if 

            message = MIMEMultipart('alternative')
            message['From'] = send_from
            message['To'] = COMMASPACE.join(send_to)
            message['Cc'] = cc_to
            message['Subject'] = subject
            message.attach(MIMEText(plain_text_body, 'plain'))
            return message

How do I assign the emails I have on the last column to the sender_address variable ? And how to use an If statement to send just one email to the Financial Advisor with his various customers ?

question from:https://stackoverflow.com/questions/65888886/iterate-through-rows-and-for-each-one-send-an-email-using-the-email-on-the-last

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
# dummy function mimic send email function
def send_email(advisor_name, data):
    print('email to '+advisor_name)
    print(data)

# group the dataframe by Financial Advisor's Name
grouped = df.groupby(['Financial Advisor's Name'])


for advisor_name, group in grouped:
    # suppose we want to send the column Name and Product
    # advisor_name will give you the name of the advisor
    # group is the corresponding group
    send_email(advisor_name, group.loc[:,['Name', 'Product']])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...