简体   繁体   中英

python CGI email dynamic output as a text file attachment

I am trying to send an email using python that has the standard To From, body, etc. However this is where my question comes in. My python script takes information in a string and manipulates it(adds,removes, formats, etc) I am trying to take this output and send it as an attachment on-the-fly. I know I can dump the output to a text file and then attach it to the email. However, due to the security of this CGI script I cannot write to the CGI directory. Is there a way to dynamically create a textfile object and send it as an attachment?

Using Python SMTP module.

I cannot create a traditional static text file due to permissions. I would like to see if Python is able to create a text file upon execution that i can populate with data and then attach to the email smtp module.

The last example on this page shows how to create and send multi-part MIME message that contains text and HTML parts using the smtplib and email modules. It should be easily adaptable to your need to send a message as an attachment (ideally the attachment should have a content-type of message/rfc822 ).

Are you using smtplib and email.mime.multipart modules? the attach() method of the MIMEMultipart class accepts chunk of data to be attached, not a file.

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