Sign inSign up

v4tech/cya-email

By v4tech

Updated over 8 years ago

A working full-stack OAuth2 + offline + queueing email container!

Image
1

1.3K

v4tech/cya-email repository overview

CYA - Email

Part of the CYA Project : https://github.com/v4tech/cya-project

Offline email tools

Goals

GoalDonetl;dr
I want to be able to backup my cloud based email to a local machine - using OAuth2:white_check_mark:OfflineIMAP below
I want to be able to send email from my local machine - using OAuth2:white_check_mark:send.py below
I will not use passwords or turn on "Allow insecure apps" - I will use OAuth2 (or better!):white_check_mark:
I want to be able to search and read messages whilst offline:white_check_mark:see notmuch
I want to be be able to reply to my email when I'm offline (i.e. queue):white_check_mark:msmtpq below

tl;dr - For the extra lazy...

  • Pull the docker image:
docker pull v4tech/cya-email
  • Source the wrapper functions:
wget https://raw.githubusercontent.com/v4tech/cya-project/master/cya-dockerfunc.sh
source cya-dockerfunc.sh
  • Profit!     :dollar: :moneybag:
    • offlineimap
    • send.py pretending to be msmtp
    • msmtpq

OfflineIMAP

OAuth2 aware email sync - www.offlineimap.org

Mapped as command offlineimap

Docker command
docker run --rm \
    --name email 
    -v ${HOME}/offlineimap.conf:/home/mymail/.offlineimaprc:ro \
    -v ${HOME}/Mail:/home/mymail/Mail \
    v4tech/cya-email \
    fetch-messages.sh
  • You can set this up as a cron job to run regularly...
  • Email is downloaded to your (host) ~/Mail
  • Link to a sample offlineimaprc file

send.py

OAuth2 aware email sending - https://github.com/cscorley/send.py

Mapped as commands send.py and (wrapper) msmtp

This section is only really useful if you want to send a message now. For queueing and sending later see the msmtpq section below.

Docker command

NOTE: I've written a wrapper called msmtp to make this interact with msmtpq transparently.

docker run --rm -i \
    -v ${HOME}/sendpyrc:/home/mymail/.sendpyrc:ro
    v4tech/cya-email \
    msmtp
Example: sending a message

Look at the example given by Christopher Corley, but remember to change his send.py --readfrommsg to msmtp.

Test your email by doing this (change the email addresses first, silly):

echo "From: Christopher S. Corley <[email protected]>
To: Test <[email protected]>
Subject: Test message
`
Body would go here
" | msmtp

msmtpq

Message queue for msmtp - https://github.com/project-mir/mir.msmtpq

Mapped as command msmtpq

I'm cheating and using msmtpq to queue messages to send with send.py. In JVR's post he mentions patching mir.msmtpq, but I've just added a wrapper script.

Docker command
docker run --rm -i \
    -v ${HOME}/sendpyrc:/home/mymail/.sendpyrc:ro \
    -v ${HOME}/msmtpq-queue:/home/mymail/.config/msmtpq/queue \
    v4tech/cya-email \
    msmtpq
  • Messages are queued in (container) ~/.config/msmtpq/queue. Remember to mount this externally.
Example: queueing then sending a message

Queue a message:

echo "From: Christopher S. Corley <[email protected]>
To: Test <[email protected]>
Subject: Test message

Body would go here
" | msmtpq

Check the queue:

msmtpq --manage list

See...it's there...

Key: 920cce313eb9095a463f00a7b711d1a224756879
Args: []
From: Christopher S. Corley <[email protected]>
To: Test <[email protected]>
Subject: Test message

Body would go here

So then send it:

msmtpq --manage send

Creating OAuth2 tokens

You really should read Google's OAuth2DotPyRunThrough.

There are effectively two steps:

  1. Registering An Application
  2. Creating and Authorizing an OAuth Token
Step 1: Registering An Application

To use OAuth2, you must have registered your application through the Google APIs Console. Registration is explained in Using OAuth 2.0 to Access Google APIs.

After registering your application, go to the "API Access" tab and create a Client ID. oauth2.py is designed to work with the "Installed application" application type. After creating a Client ID, the API Access tab should show a Client ID and Client secret. You will need to supply these values to oauth2.py in the next step.

Step 2: Creating and Authorizing an OAuth Token

Run the following command - using your client_id and client_secret:

 docker run --rm -it v4tech/cya-email \
     python /home/mymail/gmail-oauth2-tools/python/oauth2.py \
    --generate_oauth2_token --client_id=364545978226.apps.googleusercontent.com \
    --client_secret=zNrNsBzOOnQy8_O-8LkofeTR

The output will be something like:

To authorize token, visit this url and follow the directions:
  https://accounts.google.com/o/oauth2/auth?client_id=364545978226.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&scope=https%3A%2F%2Fmail.google.com%2F
Enter verification code:

Visit the URL in your browser. You may need to login to Google. Then you should see a request to grant access to your application. Done!

TO DO

Things I still need to do - or creat links to solutions elsewhere:

  • Add in some sample config files
  • CI testing


Aside for non-Un*x systems

You'll have to work out what ${HOME}/sendpyrc, etc., means in your system. I don't do Windows.



OAuth2 authentication

Tag summary

Content type

Image

Digest

Size

53 MB

Last updated

over 8 years ago

docker pull v4tech/cya-email