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 andemail 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.
Related
I'm struggle with one script using Python.
I'm trying to create a function that saves message with attachment as a .eml file.
Attachment is a excel file that is stored in my local folder.
Right now i have this code that displays good template to send, but it doesn't save this message as a eml file.
It is empty message
def save_mail():
outlook=win32.Dispatch("Outlook.Application")
olNs = outlook.GetNamespace("MAPI")
mail=outlook.CreateItem(0)
mail.To="email#email.com"
mail.Subject="TEST TEST"
mail.HTMLBody = "<p>TEST HTML</p>"
attachment=(file_path)
mail.Attachments.Add(attachment)
mail.SaveAs(path_file + '.eml', 9)
save_mail()
I don't know how I should solve this problem. Please help :)
The Outlook object model doesn't provide any property or method to save the item using the EML file format. EML, short for electronic mail or email, is a file extension for an email message saved to a file in the Internet Message Format protocol for electronic mail messages. It is the standard format used by Microsoft Outlook Express as well as some other email programs, but not Outlook. You may consider using third-party libraries such as Redemption for saving items using the EML file format.
Outlook Object Model does not allow to export in the MIME (EML) format.
You can either build EML file from the scratch (it is a text file) one property at a time, or you can use a library like Redemption (I am its author) - its SafeMailItem and RDOMail objects support olRfc822 format in the SaveAs method in addition to the formats supported by Outlook.
I want to write a python that can sends data to a draft message, but NOT Send the mail directly.
Situation:
I'm a 3D animator. After done some shots, we need to send email(not internet, just in our company) to Lighting Team.
So we have some mail form, but sometimes we need to fix some words manual(version,texture..etc).
like below:
Shot Name : XXXX_seqXXX_scXXX
File Locate(hair + cloth) : please import X:\XX\XX\XX\XXX_ABC.ma
File include:
XXX_hair **(use version 1)**
XXX_cloth
ZZZ_cloth **(No texture)**
any problem please tell me.
Thanks.
Ezylryb
Question is:
Now I can write into a file, but I don't know how to open mail software(win7 Livemail) and create a new mail has these content. I'm try to use smtplib, but it will send mail directly..
Could anyone help me??
Many Thanks!!!!
Ezylryb
==============================
Finally, I write HTML code into a .html file, and a eml file with email address + Title + CC, and use os.startfile to open both files.
And I can copy/paste to draft email ^O^.
Coz we need some chinese words, so use big5 code in eml, and use qupori module to decode MIME words to Chinese words in html file.
A "draft" is a feature which your email client (in this case Win7 livemail) implements by itself. It's more or less just an email in a mailbox that the client hasn't sent yet. Mail folders like your "outbox" etc. are similar.
It's not something that Python has access to. If the mail client uses a standard mailbox format, you might be able to create the email and write it into the mailbox using mailbox module. If not, the best you can do is to write out your draft email into a file and then open that in your mail client and manually edit it.
Looks like when using smtplib I need to know the type of file I would like to attach to an email such as MIMEImage, MIMEText, etc. Is there a way to handle attachments when the file type is unknown?
User's for our website can upload simple images (jpg and tif) or 3d drawing such as autocad or inventor files.
Thanks!
Andrew
I've set up inside of my CPanel to have all emails sent to x#x.com to be piped into a python script of mine. How would I go about having any attachments saved into a specific directory on the server and perhaps see the subject/message of the email itself?
You can use the email package to process MIME-formatted email messages. Use email.parser.FeedParser to parse the message and get back an email.message.Message object:
Treat it like a dictionary to get header fields like Subject.
Use is_multipart() to check whether it is multipart and therefore might have attachments (or it might just be a plain-text + HTML message).
Use the walk() method to recursively walk over all multipart submessages. Submessages with a Content-Disposition header starting with attachment are attachments, and you can get their contents using get_payload().
how can i change the real file extension of executable binary to send that file through gmail smtp protocol (all in python).
Example: "binary" to "binary.jpg"
I'll try this:
import gzip, shutil
src = open('3c7983cb70e9630cc4ee2fe3e1fb16c2', 'rb')
dest = gzip.open('3c7983cb70e9630cc4ee2fe3e1fb16c2.gz.jpg', 'wb')
shutil.copyfileobj(src, dest)
but when I try to send it via gmail smtp happens to this:
smtplib.SMTPDataError: (552, '5.7.0 Our system detected an illegal attachment on your message. Please\n5.7.0 visit http://mail.google.com/support/bin/answer.py?answer=6590 to\n5.7.0 review our attachment guidelines. n18sm433437wbh.23')
Thancks in advance.
According to Google's Policy, binary files are banned.
Gmail won't accept these types of files even if they are sent in a zipped
(.zip, .tar, .tgz, .taz, .z, .gz) format. If this type of message is sent to
your Gmail address, it is bounced back to the sender automatically.
So Google is unzipping your file and most likely checking the file headers to determine the file type (not relying on the extension provided. To get around this you could try uploading it as a password protected ZIP file, since google wouldn't be able to crack it open to scan the contents, assuming they allow that. Another option would be to encrypt the file before sending it, possibly even a very simple XOR encryption might be enough to get past the filtering. You would then need to decrypt upon retrieving the file.