0
str_htmlHeader = "<!DOCTYPE HTML PUBLIC>\n<html>\n";
str_htmlHeader += "<head>\n\t<title>Audi Cloud Services</title>\n</head>\n\n";
str_htmlHeader += "<body>\n\n<h1>Nightly build test results</h1>\n";
str_htmlFooter = "\n</body>\n\n</html>";

for root, dirnames, filenames in os.walk(r'\\ac-srvfile01\_Embedded\VCon1\proj_customer\337159_Audi_ACR_and_TSSS\pcm-audio'):
    for filename in fnmatch.filter(filenames, '*.html'):
        reportContent = open(os.path.join(root,filename)).read()
        attachment = MIMEText(str_htmlHeader+reportContent+str_htmlFooter, 'html')
        msg.attach(attachment)

        #msg.attach(MIMEText(open(filename).read(), "text/html"))

I am sending the message to a concerned person but the email is going to the respected person in a different email. I want to collect all the reports and send it as a single email. but the above code is sending the reports as a different email. Can someone help me how to fix that?

sam
  • 203
  • 4
  • 17
  • message format sending is correct. I am able to send the message now. but message is sending as a separate email but I want to send it as a single email. I edited my code – sam Jan 31 '16 at 14:45
  • This code sends a single email message with a single HTML part. The multipart container is useless but not harmful. It is unclear what other parts you are hoping this message should contain, and what other messages are possibly being sent in code you are still not showing us. – tripleee Jan 31 '16 at 15:07
  • firstly, it reads the html message and send it as a message to the concerned person. I want to collect all the html messages or several messages at a time and send it once as an email. instead of sending as an separate email. I edited my code. Once it reads a html message then it is sent as an email. but I want to read several or all html messages and send it as a single email – sam Jan 31 '16 at 15:18

1 Answers1

1

You want to create a single container message, then add MIME parts to it in the loop.

The multipart/alternative container is not suitable for this, as it indicates that the client should select one of the parts for display, and ignore the rest. I have used multipart/related instead. You could perhaps prefer multipart/mixed which does not imply a relationship of some sort between the parts.

str_htmlHeader = '''<!DOCTYPE HTML PUBLIC>
<html>
<head><title>Audvices</title></head>
<body><h1>Nightt results</h1>'''
str_htmlFooter = '''"
</body>\n\n</html>'''

msg = MIMEMultipart('related')
msg['From'] = 'ucd_test'
msg['To'] = 'hemappa@nce.com'
msg['Subject'] = 'AUDICES'

for root, dirnames, filenames in os.walk(r'\\ac-srvR_and_TSSS\pcm-audio'):
    for filename in fnmatch.filter(filenames, '*.html'):
    reportContent = open(os.path.join(root,filename)).read()
    attachment = MIMEText(str_htmlHeader+reportContent+str_htmlFooter, 'html')
    attachment.add_header("Content-Disposition", "attachment",\
                filename=os.path.basename(filename))
    msg.attach(attachment)

server = smtplib.SMTP('eu-smtp.nuance.com')
server.ehlo()
#server.starttls()
#server.login(username,password)
server.sendmail(fromaddress,toaddress.split(','),msg.as_string())
server.quit()
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • I modified my code as above. but it is not working. thanks for the reply. – sam Jan 31 '16 at 17:21
  • it is just sending an email without any content in it. – sam Jan 31 '16 at 17:41
  • Then I imagine the `os.walk` is not finding any matching files. Your question did not indicate any problem with that, so I assumed that part was working. – tripleee Jan 31 '16 at 18:22
  • I am getting the error as : Traceback (most recent call last): File "Test_Report.py", line 26, in reportContent = open(filename).read() IOError: [Errno 2] No such file or directory: 'report_email.html' errorlevel is 1 – sam Feb 01 '16 at 08:21
  • Updated with a quick fix. Thanks for following up. – tripleee Feb 01 '16 at 08:35
  • I dont want to attach the html file in an email. just I want the content of the html in the body of the email. – sam Feb 01 '16 at 08:37
  • Then take out the "Content-disposition: attachment" you explicitly put in. – tripleee Feb 01 '16 at 08:40
  • check my code now . only one file is shown in the body of the message other file is attached. how to solve this ? – sam Feb 01 '16 at 09:15
  • Don't do the "add_header()" if you don't have a header to add. – tripleee Feb 01 '16 at 09:17
  • You still have `multipart("alternative")` even though I have repeatedly explained why this is not what you want. – tripleee Feb 01 '16 at 09:20
  • I din edit there but I already did that but still I am getting this problem - only one file is shown in the body of the message other file is attached. – sam Feb 01 '16 at 09:24