-1

I Have a dataframe (df) like below

Subject                       To_Address       CC_Address                       Backup_Address
Invoice 2345 is invalid       xyz@gmail.com     koke@gmail,ali@gmail.com         a@gmail.com
Invoice 3456 is invalid       abc@gmail.com     jack@gmail.com,ali.gmail.com     b@gmail.com
Invoice 3673 is invalid       bcd@gmail.com     peach@gmail.com,ali.gmail.com    e@gmail.com
Invoice 8978 is invalid                         rock@gmail.com,ali.gmail.com     c@gmail.com
Invoice 8078 is invalid                         rock@gmail.com,ali.gmail.com     d@gmail.com
Invoice 3173 is invalid       mnb@gmail.com     mone@gmail.com,ali.gmail.com     f@gmail.com

I need to send unique email for each row of the dataframe with

Email Subject = df[Subject] ,Email To Address =df[To_Address], Email_CC_Address = df[CC_Address]

Also Expection condition ned to be added, Email To Address is null or NaN then Email To Address =df[Backup_Address] should be passed

How can this be done in python.

My current code which runs with static values

def create_message(send_from, send_to,cc_to,subject, plain_text_body):
    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


subject = "Invoice is invalid"
sender_address = "sender@gmail.com.com"
to = "to@gmail.com"
cc_to = "cc@gmail.com"
mail_content="This is a test message"

message = create_message(sender_address,[to],cc_to,subject,mail_content)

send_message(message)
Rahul rajan
  • 1,186
  • 4
  • 18
  • 32
  • If it's a CSV/TSV file, maybe using plain CSV module is a better option than using Pandas here. – Arunmozhi Jun 23 '20 at 04:37
  • @Arunmozhi, I write the pandas dataframe to CSV file, I not sure how to iterate and pass the values and send indivual emails. – Rahul rajan Jun 23 '20 at 04:39
  • Don't create a multipart container if the message only actually has one body part. Like its name suggests, multipart is for when you have more than one body part, like a main message and an attachment. – tripleee Jun 23 '20 at 04:39
  • Does this answer your question? [How to iterate over rows in a DataFrame in Pandas](https://stackoverflow.com/questions/16476924/how-to-iterate-over-rows-in-a-dataframe-in-pandas) – Aditya Jun 23 '20 at 04:41

2 Answers2

2

Try looping through the rows and multiple columns in parallel in the dataframe with zip:

def create_message(send_from, send_to, cc_to,subject, plain_text_body):
    sender_address = "sender@gmail.com.com"
    mail_content="This is a test message"
    for a, b, c, d in zip(df['Subject'], df['To_Address'], df['CC_Address'], df['Backup_Address']):
        if b == '':
            send_to = d
        else:
            send_to = b
        subject = a
        cc_to = c
        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


message = create_message(sender_address,[to],cc_to,subject,mail_content)

send_message(message)
David Erickson
  • 16,433
  • 2
  • 19
  • 35
0

To iterate over a Pandas DataFrame, you can use DataFrame.iterrows().

But Iteration over a df isn't something that's recommended as it's considered as an "Anti-Pattern"..

Recommended Reading here

Aditya
  • 2,380
  • 2
  • 14
  • 39