Skip to content

Instantly share code, notes, and snippets.

@juneym
Forked from cristianp6/Fetch email in a file.md
Last active August 29, 2015 14:26
Show Gist options
  • Select an option

  • Save juneym/2e57f7444c5f32d4c064 to your computer and use it in GitHub Desktop.

Select an option

Save juneym/2e57f7444c5f32d4c064 to your computer and use it in GitHub Desktop.
Write retrieved emails on a log file (Ubuntu/Debian)

Write retrieved emails in a file

As root, install fetchmail

$ apt-get install fetchmail

Check if fetchmail has SSL support: if you see something like "libssl.so.0" then yours has it

$ ldd /usr/bin/fetchmail

Test Gmail SSL certificates

$ openssl s_client -connect pop.gmail.com:995

You should see something like "+OK Gpop ready for requests from ..."

Setup fetchmail

$ wget -O ~/.fetchmailrc https://gist.github.com/cristianp6/c063e36826a6b1623f3d/raw/f429c4633992c9f8d95e2263c2876b7d38972912/fetchmailrc
$ chmod 0700 ~/.fetchmailrc

Edit the file with your settings

$ nano ~/.fetchmailrc

As root, create the "fetchmail handler"

$ wget -O /sbin/fetchmailhandler https://gist.githubusercontent.com/cristianp6/c063e36826a6b1623f3d/raw/06537116cfb81041932149257d68d2ffcc6f363a/fetchmailhandler
$ chmod +x /sbin/fetchmailhandler
  • Change the ownername as the username who have to read the file
  • Change the filepath where you want to store the emails
$ nano /sbin/fetchmailhandler

Test fetchmail

$ fetchmail -d0 -vk -f .fetchmailrc

Kill eventual fetchmail daemon process with:

$ fetchmail -q

Set a cronjob for fetchmail

Find fetchmail path

$ whereis fetchmail

The path should be something like "fetchmail: /usr/bin/fetchmail ..."

$ crontab -e

# Fetch emails every 5 minutes max
*/5 * * * * /usr/bin/fetchmail > /dev/null 2>&1

Or start fetchmail daemon

$ fetchmail

Now you can parse the file "/path/to/the/file/[unixtimeInMicroseconds].eml"

Source

#!/bin/bash
owner="YourOSuserHere"
filepath="/path/to/the/file/"
filename="$(date +%s%N).eml"
mkdir -p $filepath && cd $filepath
touch $filename && chown $owner $filename
while read line
do
echo "$line" >> $filename
done < "${1:-/proc/${$}/fd/0}"
#set daemon 600
set postmaster "YourOSuserHere"
poll pop.gmail.com with proto POP3 and options no dns
user 'account@domain.com' there with password 'YourPasswordHere' is YourOSuserHere here options ssl
mda /sbin/fetchmailhandler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment