IMAP attachment retrieving command - imap

I am working on a mail client using IMAP and I am looking for the command for receiving the attachments of a message.

All message info is retrieved using the FETCH command. You have two options on how to use it, however.
First, you can retrieve the entire email message, verbatim. In that case, you're going to need to include a MIME parser in your client to figure out the structure of the message. (Each platform has at least one or two popular MIME parsers; since you haven't told us what you're coding in, I can't recommend one for you.) Once you get the message structure from your MIME parser, you'll need some client logic to determine which parts are attachments. It's worth looking at RFC 2183 to get you started. In general, parts with a Content-Disposition starting with "attachment" are going to be attachments, but all mail client authors go through a phase of trial and error getting it right. In order to download the entire email message, you'd issue the IMAP command
$ UID FETCH <uid> BODY.PEEK[]
Second, you can have the IMAP server parse the message structure for you by issuing a FETCH BODYSTRUCTURE (note: no square brackets). You'll have to parse the returned BODYSTRUCTURE data yourself; the IMAP RFC explains the format and gives a few examples.
# message, no attachments:
("TEXT" "PLAIN" ("CHARSET" "ISO-8859-1" "FORMAT" "flowed") NIL NIL "7BIT" 1469 50 NIL NIL NIL NIL)
# message, one attachment
(("TEXT" "PLAIN" ("CHARSET" "US-ASCII") NIL NIL "QUOTED-PRINTABLE" 56 1 NIL NIL NIL NIL)("AUDIO" "X-WAV" ("NAME" "voicemail.wav") NIL NIL "BASE64" 152364 NIL ("attachment" ("FILENAME" "voicemail.wav")) NIL NIL) "MIXED" ("BOUNDARY" "----_=_NextPart_001_01C4ACB3.5AA7B8E2") NIL NIL NIL)
Once you've determined which parts you're interested in, you can issue a FETCH for the displayable message body. Your client can then just list the message attachments (parsed out of the BODY response) and can then go back and FETCH them if the user clicks on them. So the IMAP commands you'd be issuing would be along the lines of:
$ UID FETCH <uid> (BODY ENVELOPE) # get structure and header info
$ UID FETCH <uid> (BODY[1]) # retrieving displayable body
$ UID FETCH <uid> (BODY[2]) # retrieving attachment on demand

I believe what you are looking for is the IMAP v4 FETCH command.

You could use Context.IO's files resource to quickly and easily fetch attachments.
http://context.io/docs/2.0/accounts/files#get

Related

how to prevent message creation api to resend complete message

I use graph api to create a message with attachment(s).
All runs fine but the server send back the complete message in response, including the attachments.
Is there a way to only get the message id in the response ?
I try with :
string webApiUrl = $"{_apiUrl}v1.0/users/{senderId}/messages?$select=id"
but I still get the whole message with 98ko of attachment.
You can do it using the prefer:return=minimal header in the request which will mean you will just get a 204 response. However the id of the item that was created will be returned in the location header (the response should really have the OData-EntityId if they are following the oData spec to the letter, also I'm not sure why it return the Outlook v2 location rather then the graph but the message Id is the same between them)

Influxdb [[inputs.http]] - Status code 411 (Length Required)

Telegraf plugin:
HTTP Input Plugin
I'm trying to use telegraf to collect data from an vendor API.
test.conf file looks like this:
[[inputs.http]] urls = ["https://10.10.10.10"] method = "POST" body = '{"F_":"LOGIN","DATA":{"ID":"user","PWD":"password"}}'
When debugging i can see this is the error i get:
[inputs.http] Error in plugin: [url=https://10.10.10.10]: received status code 411 (Length Required), expected any value out of [200]
The API documentation for my vendor API states that the field "Content-Length" and "Host" are mandatory, but I can find now way to enable that in the plugin.
I have also tried using the http_response plugin, and I am able to get the JSON reply, but unfortuately i have not been able to find a good way to get the response_body_field JSON appended to the influxdb.
Does anybody know if either:
I can enable the two headers dynamicly(The POST lenght will vary)
or:
use the response_body_field from http_response and smoothly parse it to the influxdb?

Why is json response NULL when I send video file larger than 20MB via POST?

I am creating a POST request and using it to send a video to the server. On the server side, I decode the video, and save it to a file directory. IF the video sent is under 20MB everything works as expected and I get a valid JSON response, otherwise my response dictionary is NULL or returns "The operation couldn’t be completed. (Cocoa error 3840.)"
$result = mysqli insert statement;
$videoDirectory = 'userVideos/'.$unique_id.'.mp4';
$decodedVideo =base64_decode($video);
file_put_contents($videoDirectory, $decodedVideo);
if (!$result['error'])
{
$e = "register into Str33trider successfully";
print json_encode(array('results'=>$videoCaption));
exit();
}
I've even edited my apache config file
<IfModule mod_php5.c>
php_value post_max_size 200M
php_value upload_max_filesize 200M
php_value memory_limit 320M
php_value max_file_uploads 200M
php_value max_execution_time 30000
php_value max_input_time 259200
php_value session.gc_maxlifetime 1200
</IfModule>
When you receive a response for a POST request, first check the status code.
If the POST request succeeded:
If status code equals 200 (OK) or 204 (No Content) the response body is likely empty or it describes the result of the operation. With either status codes, the request hasn't created a resource which can be identified by a URI.
If status code equals 201 (Created) the request created a resource on the server and the response body may describe the result of the operation, and the response should contain a location header where the new resource can be located.
Usually, the web service API describes the details about the response body (if any) and its content type and character encoding. Possibly, there are more than one format that can be send, e.g. JSON or XML.
If the POST request failed:
The server will send a corresponding status code and optionally a response body containing details about the error. Oftentimes, the server may send a response body in a content type which does not match the Accept header of the request.
Note:
A client should always also check the content type of the response body (if any) and decode it accordingly. In case of server errors, the content type may often be text/html instead in the content type specified in the Accept header, e.g. application/json.
So, if you log the complete error description for Cocoa error 3840 you will read that the given text is likely not JSON since it must start either with a '[' or '{'. This indicates, that you got an error message from the server which is not JSON. Decode the error message so that it is human readable and log it to the console to see what the server is telling you.

Gzip decompress JSON POST body in Rails/Passenger/Nginx

We have a function in our Rails code that accepts a JSON POST body:
contacts = ActiveSupport::JSON.decode(request.raw_post.gsub("+", ""))
(I'm aware that I can get this from params["_json"] as well, but we have extremely large (MBs) POST bodies that do not get put into params["_json"] for some reason (and + throws errors too).
Since the JSON is usually sent from a mobile client, it's important to us to optimize the upload size. We want to switch to having the POST body gzipped.
However, no matter what we do, we get the same error with no line number:
MultiJson::DecodeError (743: unexpected token at ''):
We have tried:
gzipped_contacts = Zlib::GzipReader.new(StringIO.new(request.raw_post)).read
contacts = ActiveSupport::JSON.decode(gzipped_contacts.gsub("+", ""))
This:
gzipped_contacts = ActiveSupport::Gzip.decompress(request.raw_post)
contacts = ActiveSupport::JSON.decode(gzipped_contacts.gsub("+", ""))
And the solution found here: Rails: how to unzip a compressed xml request body?
I'm pretty sure this is not occurring at the controller level because I can't log anything there, so it needs to be done in the middleware or at the server (but I can't find anything for Nginx that lets us deflate). Please assist!
Ok, turns out the iPhone client was sending the wrong headers. So the solution for anyone encountering this is to see the advice here:
Rails: how to unzip a compressed xml request body?
And verify that you are sending Content-Type: gzip/json.

IMAP fetch of a mime part without fetching the whole message

I need some help formulating an IMAP fetch command to only fetch a certain mime attachment without fetching the whole body or other attachment. Given the following bodystructure
(BODYSTRUCTURE (("text" "html" ("charset" "utf-8") NIL NIL "base64" 568 8 NIL NIL NIL) "mixed" ("boundary" "===============0621936444==") NIL NIL))
how would a fetch command for fetching the text/html part look like?
I have read Section 6.4.5 of RFC3501 several times but I still don't get it. Any pointers to further examples of fetch requests for mime attachments would also be appreciated.
Off the top of my head it would be something like:
FETCH 88 BODY.PEEK[1]
where 88 is the 88th message, and 1 is the body part.
Try this one:
FETCH uid BODY.PEEK[1.2]
UID FETCH uid_of_the_mail BODY[1.2] is the first inline attachment. Well, for gmail, 1 for TEXT, 2-N for attachment. 1.1 for TEXT, 1.2 should for the first inline attachment, 1.N-1 should for the Nth inline attachment.
First of all you need to parse bodystructure response and then find each part section string, Once you have acheived each part section string then You can fetch required part mime.
Assume that You need to fetch first part mime then command should be like this
. FETCH msgno (BODY[1.MIME])

Resources