I'm trying to build a python mailserver that rewrites certain URLs (given in a text file).
Plaintext messages work without problems, but multipart (HTML) E-Mails are a bit of concern to me. I can't find a proper way to rebuild the message after modifying the body and replace functions do not exist for email in python.
What I have currently (for plaintext messages, currently without replacement):
if(not msg.is_multipart()):
for part in msg.walk():
parttype = part.get_content_type()
partmaintype = parttype.split("/")[0]
partsubtype = parttype.split("/")[1]
parttype=partmaintype + "/" + partsubtype
bodypart = part.get_body(('plain','html'))
#bodypart = bodypart.get_content().encode()
bodypart = bodypart.get_content()
print(bodypart)
newmsg.set_content(bodypart)
#print(newmsg)
newdata = bytes(newmsg)
The _structure(msg) function is in knowledge to me, but I cannot find a way to preserve this message structure (to rebuild the email with a new message object). I also know about the msg.walk() function, but when used with multipart message contents, the subcontents occur twice.
Thanks a lot in advance!
Related
An email comes in with the following header from an #outlook.com address
Message-ID:
<DM5PR1101MB2074A897F300D11D32C71B75EC049#DM5PR1101MB2074.namprd11.prod.outlook.com>
To respond with Python I put that message identifier in the In-Reply-To header
from email.message import EmailMessage
msg = EmailMessage()
msg['In-Reply-To'] = '<DM5PR1101MB2074A897F300D11D32C71B75EC049#DM5PR1101MB2074.namprd11.prod.outlook.com>'
However, when I print this message the header appears as follows:
In-Reply-To: =?utf-8?q?=3CDM5PR1101MB2074A897F300D11D32C71B75EC049=40DM5PR11?=
=?utf-8?q?01MB2074=2Enamprd11=2Eprod=2Eoutlook=2Ecom=3E?=
And when it arrives in Outlook it's not put in the same conversation thread as the original message.
Does anyone know why this may be happening and/or know of any work-around?
I suspect that this is happening because Python is trying to split the header across multiple lines since this header is above the RFC-specified line length and when it splits across multiple lines it starts each line with '?utf-8?q and converts the < character to ?=3C, etc... Whereas Outlook is expecting the header to be of the format
In-Reply-To':
<DM5PR1101MB2074A897F300D11D32C71B75EC049#DM5PR1101MB2074.namprd11.prod.outlook.com>'
Perhaps if someone knows how to force the header to be in this format that would suffice as a work-around.
Any help would be greatly appreciated :)
I am working on sending emails with python and have a decent grasp on that however I am struggling with using info from a CSV File.
I have a piece of code:
s.login('example#gmail.com', 'expassword')
that when used with a preset login and password works, it sends the email to my recipient, which is later set in my code, however as soon as I use the code:(chooses a random email and password from a CSV to send from(for a spam email demo))
with open ('C:/Filepath/randomsender.csv') as file:
reader = csv.reader(file)
sender = random.choice(list(reader))
followed by
s.login(sender)
I get an auth error because it cannot read the chosen line properly even though the result of sender is formatted the same as:
'example#gmail.com', 'expassword'
If you want to pass list elements as position param, you need to unpack it.
this might help
e.g.
s.login(*sender)
I'm trying to upload a PDF as an attachment to a Trello card using python-requests. I've been unable to get the request in the function below to return anything other than 400: Error parsing body despite significant tweaks (detailed below).
I should note that I'm able to create cards and add URL attachments to them (neither of which require a file upload) without any problems.
Here's the code that handles the POST of the file:
def post_pdf(session, design, card_id):
attachment = {
"name": design["campaign_title"] + " - Combined PDF",
"mimeType": "application/pdf"
}
pdf_post = session.post(
url = "https://api.trello.com/1/cards/" + card_id + "/attachments",
files = {"file": open("combined_pdf.pdf", "rb")},
data = attachment
)
The authentication key and token are set Session params when the session was created, so they're not added here.
Also, in the actual code, the POST is handled by a wrapper function that adds some boilerplate error-checking and rate limiting to the request, as well as more-verbose error dumps when a request fails, but I've confirmed (in the above example) that the same error persists without the wrapper.
Adjustments I've tried
Substituting data = attachment with json = attachment
Substituting data = attachment with params = attachment
Omitting attachment completely and POSTing the file with no associated data
Adding stream = True to the request parameters (this doesn't seem to matter for uploads, but I figured it couldn't hurt to try)
Encoding the file as base64 (this encoding has been required elsewhere; I was grasping at straws)
Encoding the file as base64, combined with the above tweaks to data / json / params
Note: The PDF file is potentially a source of the problem - it's generated by converting several images to PDF format and then concatenating them with pdfunite, so I could well have made mistakes in its creation that are causing Trello to reject the file. What seems to confirm this is that Googling for Trello "Error parsing body" returns two hits, only one of which deals with Trello, and neither of which are useful. This leads me to think that this is a particularly odd / rare error message, which means to me that I've made some kind of serious error encoding the file.
However, the PDF file opens properly on my (and my coworkers') systems without any error messages, artifacts, or other strange behavior. More importantly, trying this with other "known good" PDFs also fails, with the same error code. Because the file's contents fall within the bounds of "company property / information", I'd like to avoid posting it (and / or the raw request body), but I'll do so if there's agreement that it's causing the problem.
I found the solution: the Content-Type header was set incorrectly due to a session-wide setting ( Session.headers.update({"Content-Type": "application/json"}) ) overriding the multipart/form-data header when the upload request was sent. This caused Trello to reject the body. I solved the problem by removing the session-level header, which allowed requests to modify the content type for each request.
I need to generate a MIME attachment that contains a Base64 encoded file. However what I need to also allow for is encoding the attachment WITHOUT any new lines. The code I have is as followed:
msg_obj = MIMEMultipart()
msg_atch = MIMEBase(mime_type, mime_subtype)
msg_atch.set_payload(file_data)
Encoders.encode_base64(msg_atch)
msg_obj.attach(msg_atch)
What I have tried to perform to remove the new lines in the attach base64 message was this:
msg_obj = MIMEMultipart()
msg_atch = MIMEBase(mime_type, mime_subtype)
msg_atch.set_payload(file_data)
Encoders.encode_base64(msg_atch)
msg_atch.strip()
msg_obj.attach(msg_atch)
However this failed to change the results of the data. If anyone has any ideas on how to allow for this, it would be great.
I noticed in the penultimate line of your 2nd sample code, you call the msg_atch.strip() function. The problem with this is that there isn't any function strip() of MIMEBase.
What you probably want to do is something along the lines of this:
msg_obj = MIMEMultipart()
msg_atch = MIMEBase(mime_type, mime_subtype)
msg_atch.set_payload(file_data)
Encoders.encode_base64(msg_atch)
msg_atch._payload = msg_atch._payload.replace('\n','')
msg_obj.attach(msg_atch)
The MIMEBase._payload string is the actual (in this case, base64) content used by the attachment.
This code will take the inner content of the MIMEBase attachment and eliminate the extra newlines - including the ones inside to provide nice formatting of base64 text for "human readability" (my question is why they even bother). If you just want to get rid of the newlines at the end, just use msg_atch._payload = msg_atch._payload.rstrip('\n').
Keep in mind that the header of the attachment (Content-Type: application/octet-stream and MIME-Version: 1.0 are parts) require these newlines.
Also, try to remember that you shouldn't normally be editing internal variables in this fashion. However, one of the things I find nice about Python is that there are really no private members of a class, so you can modify whatever you want in a class. We can do whatever we want, especially if it's a special condition.
Happy Coding!
I want to make postfix send all emails to a python script that will scan the emails.
However, how do I pipe the output from postfix to python ?
What is the stdin for Python ?
Can you give a code example ?
Rather than calling sys.stdin.readlines() then looping and passing the lines to email.FeedParser.FeedParser().feed() as suggested by Michael, you should instead pass the file object directly to the email parser.
The standard library provides a conveinience function, email.message_from_file(fp), for this purpose. Thus your code becomes much simpler:
import email
msg = email.message_from_file(sys.stdin)
To push mail from postfix to a python script, add a line like this to your postfix alias file:
# send to emailname#example.com
emailname: "|/path/to/script.py"
The python email.FeedParser module can construct an object representing a MIME email message from stdin, by doing something like this:
# Read from STDIN into array of lines.
email_input = sys.stdin.readlines()
# email.FeedParser.feed() expects to receive lines one at a time
# msg holds the complete email Message object
parser = email.FeedParser.FeedParser()
msg = None
for msg_line in email_input:
parser.feed(msg_line)
msg = parser.close()
From here, you need to iterate over the MIME parts of msg and act on them accordingly. Refer to the documentation on email.Message objects for the methods you'll need. For example email.Message.get("Header") returns the header value of Header.