PostfixAdmin and Fetchmail

August 25, 2010 Postfixadmin

Getting Fetchmail to Work with Postfix Admin
The default set up with Postfixadmin looks like it should work but it will not work correctly until you modify a few things.  This tutorial will help you get it working correctly.

WARNING: Use this at your own risk.  It has been found to work however, it may pull down all email from an account and remove it on the remote server.  Test to verify that it is working like you want.

First go to PostfixAdmin and set up the account you want to pull mail to.

Mailbox            this is the local mailbox
Server               this is the remote server for the account you want to pull from
Auth Type      usually you will supply a password
User                 user_name or remote account
Password        password for account
Folder             the remote folder you want to retrieve
Poll                 Does not work, you must use cron jobs for script
Fetch All       click for all messages
Keep              Be sure to keep on both servers until you are satisfied
Protocol         IMAP usually
SSL                 if it is required

Ubuntu 10.04
Install several programs in preparation.

apt-get install fetchmail liblockfile-simple-perl

Create a directory and lockfile for fetchmail.

mkdir /var/run/fetchmail
touch /var/run/fetchmail/fetchmail-all.lock

You will need to install several perl modules.  Here is a list:

DBI
MIME::Base64
File::Temp
Sys::Syslog

perl -MCPAN -e shell

install>File::Temp

When you install them they should return an “OK” at the end or you will need to fix dependencies.

Edit the fetchmail.pl script.  The key here is to put your database name, user name and password in the script.  Note several lines have been commented out as the file in /etc/mail/postfixadmin/ did not work as well as actually changing the script.

#!/usr/bin/perl
use DBI;
use MIME::Base64;
# use Data::Dumper;
use File::Temp qw/ mkstemp /;
use Sys::Syslog;
#require “liblockfile-simple-perl”;
use LockFile::Simple qw(lock trylock unlock);

######################################################################
########## Change the following variables to fit your needs ##########

# database settings

# database backend – uncomment one of these
#our $db_type = ‘Pg’;
my $db_type = ‘mysql’;

# host name
our $db_host=”127.0.0.1″;
# database name
our $db_name=”postfix”;
# database username
our $db_username=”postfix_user”;
# database password
our $db_password=”database_password”;

# instead of changing this script, you can put your settings to /etc/mail/postfixadmin/fetchmail.conf
# just use perl syntax there to fill the variables listed above (without the “our” keyword). Example:
# $db_username = ‘mail’;
#if (-f “/etc/mail/postfixadmin/fetchmail.conf”) {
#       require “/etc/mail/postfixadmin/fetchmail.conf”;
#}

Make the script executable.
chmod 755 /var/www/postfixadmin/ADDITIONS/fetchmail.pl

It may choke if you do not run it directly from perl.
sh /var/www/postfixadmin/ADDITIONS/fetchmail.pl
/var/www/postfixadmin/ADDITIONS/fetchmail.pl: 3: use: not found
/var/www/postfixadmin/ADDITIONS/fetchmail.pl: 4: use: not found
/var/www/postfixadmin/ADDITIONS/fetchmail.pl: 6: use: not found
/var/www/postfixadmin/ADDITIONS/fetchmail.pl: 7: use: not found
/var/www/postfixadmin/ADDITIONS/fetchmail.pl: 9: Syntax error: “(” unexpected

Call it from perl and it works…it will complain running as root.
/usr/bin/perl /var/www/postfixadmin/ADDITIONS/fetchmail.pl

Put it in a cron job if you want to run it continually.

It finally works correctly and is a great way to move mail to a new account.

Tags: ,

Comments are closed.