问题
I am using aiohttp
. I have an api which handles Mailgun routed data. The emails have multiple attachments. I am not able to read all of the attachments. It just gives me a single one.
data
is what I receive.
str(list(data.keys()))
gives me the list - ['Content-Type', 'Date', 'Dkim-Signature', 'From', 'Message-Id', 'Mime-Version', 'Received', 'Received', 'Received', 'Subject', 'To', 'X-Envelope-From', 'X-Mailgun-Incoming', 'X-Received', 'attachment-count', 'body-html', 'body-plain', 'from', 'message-headers', 'recipient', 'sender', 'signature', 'stripped-html', 'stripped-signature', 'stripped-text', 'subject', 'timestamp', 'token', 'attachment-1']
str(data.get('attachment-count')
gives me 2/3/4 when I send multiple files in the email - which is fine. But there is only one key as attachment-1
.
Doubts:
Does attachment-1
in the keys tell that there is only one file in the data
?
If there are multiple files, does that mean there are keys as - attachment-1
, attachment-2
....
How do I retrieve all the files from the email?
I tried looking for the Mailgun's documentation but did not get concrete help for reading the files. Can someone redirect me to some code for the same. Other fields as from
, subject
is just fine.
This random thing I tried also reads just a single file. This seems wrong, though.
form_data_content_type = [(v.name, v.content_type, v.filename) if (
v and hasattr(v, 'content_type') and hasattr(v, 'filename')) else None for v in
data.values()]
logger.info("No. of attachments " + str(len(form_data_content_type)) # returns 1
* UPDATE *
I tried running a flask server and tested emails with multiple files:
print(request.files)
prints ImmutableMultiDict([('attachment-2', <FileStorage: 'Screen Shot 2015-09-02 at 10.18.37 am.png' ('image/png')>), ('attachment-1', <FileStorage: 'Screen Shot 2015-09-02 at 10.18.36 am.png' ('image/png')>)])
which shows there are two files indeed.
Now there is certainly an issue with how aiohttp is handling the mailgun's data:
printing request.post()
gives only one file - 'attachment-1': Field(name='attachment-1', filename='Screen Shot 2015-09-02 at 10.40.18 am.png', file=<_io.BufferedRandom name=10>, content_type='image/png')
. There is no attachment-2, god knows why!
回答1:
as im not familair with python or aiohttp i cant give you the code example, but this is how it works.
POST Mailgun sends a post request with all relevant information as an array,
Files
it also sends files along with the request, in php you can loop through the super-globe $_FILES
to retrieve the attachments,
Inline Embedded Pictures
when someone sends a inline picture for example myimg.jpg you need to map the attachment to embedd and that why you only see attachment-1
even though there are more attachments only one of them are embedded.
'attachment-count' => '3',
Tells you how many attachments there are, in this case 3, in php you'd find it in $_FILES,
content-id-map' => '{"<ii_jgl9284x0_1631307716495e75>": "attachment-1"}',
Points to the embedded img
<img src="cid:ii_jgl9284x0_1631307716495e75" width="190" height="114">
来源:https://stackoverflow.com/questions/32332473/how-do-i-get-the-attachments-from-an-incoming-mailgun-route