ubuntu:curl:perform_imap_queries_using_curl
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
ubuntu:curl:perform_imap_queries_using_curl [2020/07/15 09:30] – external edit 127.0.0.1 | ubuntu:curl:perform_imap_queries_using_curl [2020/09/25 12:58] (current) – removed 192.168.1.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Ubuntu - Curl - Perform imap queries using Curl ====== | ||
- | |||
- | If you have an IMAP or IMAPS server you can "read your email" using Curl. To get started you'll first of all need to know: | ||
- | |||
- | * Your mail-server address. | ||
- | * Your mail login. | ||
- | * Your mail password. | ||
- | |||
- | ---- | ||
- | |||
- | ===== Listing Folders ===== | ||
- | |||
- | The initial example will connect us to the IMAP server at imap.example.com, | ||
- | |||
- | <code bash> | ||
- | curl --url " | ||
- | </ | ||
- | |||
- | <WRAP info> | ||
- | If you leave out the password from the --user argument, curl will prompt you for the password before it preforms the request. This way you're not leaking your password to your shell history or the process list. | ||
- | |||
- | Curl also supports .netrc lookup of user: | ||
- | </ | ||
- | |||
- | returns | ||
- | |||
- | <code bash> | ||
- | .. | ||
- | .. | ||
- | * LIST (\HasNoChildren) "/" | ||
- | * LIST (\HasNoChildren) "/" | ||
- | * LIST (\HasNoChildren) "/" | ||
- | * LIST (\HasNoChildren) "/" | ||
- | </ | ||
- | |||
- | Here we see that we've connected, and received a list of folders, including " | ||
- | |||
- | If your mail-server is running over SSL then instead of using **imap://** you should set the schema to **imaps:// | ||
- | |||
- | This would look like so: | ||
- | |||
- | <code bash> | ||
- | curl --insecure | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ===== Discovering Messages ===== | ||
- | |||
- | With the previous example we looked at listing mailboxes. | ||
- | |||
- | To see how many messages exist in the folder " | ||
- | |||
- | <code bash> | ||
- | curl --insecure \ | ||
- | --url " | ||
- | --user " | ||
- | --request " | ||
- | </ | ||
- | |||
- | returns: | ||
- | |||
- | <code bash> | ||
- | * OK [PERMANENTFLAGS ()] Read-only mailbox. | ||
- | * 9465 EXISTS | ||
- | * 3266 RECENT | ||
- | * OK [UIDVALIDITY 1373146046] UIDs valid | ||
- | * OK [UIDNEXT 9468] Predicted next UID | ||
- | </ | ||
- | |||
- | This tells us that there are 9465 messages. | ||
- | |||
- | ---- | ||
- | |||
- | ===== Fetching A Single Message ===== | ||
- | |||
- | Fetching a message is simple if you know both the folder from which it comes and the ID of the message you wish to retrieve. | ||
- | |||
- | We've already shown that the folder " | ||
- | |||
- | <code bash> | ||
- | curl --insecure \ | ||
- | --url " | ||
- | --user " | ||
- | </ | ||
- | |||
- | returns: | ||
- | |||
- | <code bash> | ||
- | .. | ||
- | X-HELO: mail.cs.helsinki.fi | ||
- | X-REMOTE-IP: | ||
- | X-REMOTE-HOST: | ||
- | .. | ||
- | .. | ||
- | I just accidentally someone! (see facebook) | ||
- | .. | ||
- | </ | ||
- | |||
- | Fetching the message includes both the headers and the body. If you want to fetch just the headers, or a subset, things are a little fiddlier due to URL-encoding. | ||
- | |||
- | <code bash> | ||
- | curl --insecure \ | ||
- | --url " | ||
- | --user " | ||
- | </ | ||
- | |||
- | returns: | ||
- | |||
- | <code bash> | ||
- | Date: Sat, 14 Apr 2012 14:13:41 +0300 (EEST) | ||
- | From: Steve Kemp < | ||
- | To: Steve Kemp < | ||
- | Subject: Re: Fancy a cake? | ||
- | </ | ||
- | |||
- | We could have avoided the use of URL-encoding and instead sent a custom-request, | ||
- | |||
- | <code bash> | ||
- | curl --insecure --verbose \ | ||
- | --url " | ||
- | --user " | ||
- | --request "fetch 512 BODY.PEEK[HEADER.FIELDS (Subject)]" | ||
- | </ | ||
- | |||
- | returns: | ||
- | |||
- | <code bash> | ||
- | .. | ||
- | Subject: Re: Fancy a cake? | ||
- | .. | ||
- | </ | ||
- | |||
- | The reason for avoiding custom-requests where possible is that when you're using the curl API programatically you'll discover that responses are not decoded - which is a known issue. | ||
- | |||
- | In the example above we'd have received zero output unless/ | ||
- | |||
- | ---- | ||
- | |||
- | ===== Simple Shell Scripts ===== | ||
- | |||
- | As a quick hack the following shell-script will dump the subject of the first ten messages in the given mailbox: | ||
- | |||
- | <code bash> | ||
- | #!/bin/sh | ||
- | # Dump the subject of the first ten messages in the folder. | ||
- | |||
- | for id in `seq 1 10` ; do | ||
- | echo " | ||
- | curl --insecure \ | ||
- | --url " | ||
- | --user " | ||
- | done | ||
- | </ | ||
- | |||
- | This takes no account of the maximum message-ID. | ||
- | |||
- | <code bash> | ||
- | #!/bin/sh | ||
- | # Dump the subject of all messages in the folder. | ||
- | |||
- | id=1 | ||
- | |||
- | while true ; do | ||
- | echo " | ||
- | curl --insecure \ | ||
- | --url " | ||
- | --user " | ||
- | id=`expr $id + 1` | ||
- | done | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ===== CurlMail Script ===== | ||
- | |||
- | <code bash> | ||
- | #!/bin/bash | ||
- | # / | ||
- | # http:// | ||
- | # celeste crystalfaery 2016-02-24 22: | ||
- | # https:// | ||
- | # read(only) IMAPS INBOX (using ~/.netrc for password) | ||
- | # (machine example.org login user password my_password) | ||
- | # 1st argument is machine name (else talk to self if not specified) | ||
- | # 2..n argument(s) (optional) are message number(s) to view | ||
- | # e.g.: curlmail example.org 3 1 4 | ||
- | help=10 # this line number of file -1 | ||
- | # NOTE: remove the " | ||
- | # NOTE: we do not sanitize the server argument, so as-is don't make this into a cgi | ||
- | |||
- | function cleanup { | ||
- | if [ -e /tmp/$$ ]; then | ||
- | rm / | ||
- | fi | ||
- | } | ||
- | |||
- | trap cleanup INT TERM # cleanup our temporary file | ||
- | |||
- | case $# in | ||
- | |||
- | 0) | ||
- | exec $0 `hostname` # | ||
- | exit -1 # exec doesn' | ||
- | ;; | ||
- | 1) | ||
- | case " | ||
- | -v) | ||
- | tail -n +4 $0 | head -n 1 | ||
- | ;; | ||
- | --version) | ||
- | tail -n +4 $0 | head -n 1 | ||
- | ;; | ||
- | -h) | ||
- | head -n $help $0 | tail -n +6 | ||
- | ;; | ||
- | --help) | ||
- | head -n $help $0 | tail -n +6 | ||
- | ;; | ||
- | *) | ||
- | let uid=0 | ||
- | while let uid=$uid+1 | ||
- | do # iterate available messages | ||
- | echo -n " | ||
- | chmod 600 | ||
- | curl -# --insecure -n --url \ | ||
- | " | ||
- | done | ||
- | sed ' | ||
- | rm | ||
- | ;; | ||
- | esac | ||
- | exit | ||
- | ;; | ||
- | *) | ||
- | server=" | ||
- | shift | ||
- | while [ $# != 0 ] | ||
- | do # iterate requested messages | ||
- | message=`echo " | ||
- | shift | ||
- | rm | ||
- | echo -n " | ||
- | chmod 600 | ||
- | curl -# --insecure -n --url " | ||
- | sed ' | ||
- | rm | ||
- | done | ||
- | exit | ||
- | ;; | ||
- | esac | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ===== References ===== | ||
- | |||
- | http:// | ||
ubuntu/curl/perform_imap_queries_using_curl.1594805433.txt.gz · Last modified: 2020/07/15 09:30 by 127.0.0.1