0

I have a list in which there are 2 values. I want to send mail with that list value but every time it is sending a separate mail for each value.

List is:-

file_list = ['D:/Users/SPate233/Downloads/NS dashboard/sql_query\\Book1.csv', 'D:/Users/SPate233/Downloads/NS dashboard/sql_query\\Book2.csv']

Sample code:-

                 for names in file_list:
                    body = "Hi Team, \n\n NS_Dashboard_FieldExecution script failed due to some reason. " \
                            "\n Please check the attached log file.\n Empty Files :- \n %s" %names  + \
                            "\n\n Thanks & Regards, \n NS_Dashboard_FieldExecution Team"
                    emailstr = "echo '{0}' | /bin/mail -a {1} -s '{2}' '{3}'".format(body, msg_tran, sub, receiver)
                    os.system(emailstr)

Output:-

I want a single mail with the name of both the value of the list. like this:-

Hi Team, 

 NS_Dashboard_FieldExecution script failed due to some reason. 
 Please check the attached log file.
 Empty Files :- 
 D:/Users/SPate233/Downloads/NS dashboard/sql_query\\Book1.csv
 D:/Users/SPate233/Downloads/NS dashboard/sql_query\\Book2.csv

 Thanks & Regards, 
 NS_Dashboard_FieldExecution Team

3 Answers 3

1

Because you need to build your body with loop, not looping on the entire mail construction and sending process

Something like this :

body = "Hi Team, \n\n NS_Dashboard_FieldExecution script failed due to some reason. " \
                            "\n Please check the attached log file.\n Empty Files :- \n %s"
for names in file_list:
       body += %names  
body += "\n\n Thanks & Regards, \n NS_Dashboard_FieldExecution Team"
emailstr = "echo '{0}' | /bin/mail -a {1} -s '{2}' '{3}'".format(body, msg_tran, sub, receiver)
os.system(emailstr)
Sign up to request clarification or add additional context in comments.

Comments

1

You are looping on file_names. If you want to send all file_names in one go, then stop looping on file_names file_names = " \n ".join(file_list)

file_names = " \n ".join(file_list)

body = "Text %s Text" %names 
body = "Hi Team, \n\n NS_Dashboard_FieldExecution script failed due to some reason. " \
                            "\n Please check the attached log file.\n Empty Files :- \n %s" %file_names  + \
                            "\n\n Thanks & Regards, \n NS_Dashboard_FieldExecution Team"

emailstr = "echo '{0}' | /bin/mail -a {1} -s '{2}' '{3}'".format(body, msg_tran, sub, receiver)
                    os.system(emailstr)

Comments

0

Rather then looping for each file, you can also convert the list into a string, use

% '\n'.join(file_list)

in place of % names and skip the loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.