I recently got a gmail account, and I wanted to copy a bunch of my old mail to it from dbmail to try it out. So I wrote this script to copy it over for me. I figured someone else may be able to use it for one thing or another, so have at.

Blake
#!/usr/bin/perl
#
# Send mail from a dbmail mailbox to a remote mail account
# and then move it to a different mailbox. Based on a dbmail
# 2.0 schema and MySQL.
#
# June 22, 2004   Blake Mitchell <[EMAIL PROTECTED]>
# This code is released into the public domain.
#

use warnings;
use strict;

use DBI qw(:sql_types);
use Net::SMTP;

# database connection information
my($dsn, $user, $pass) =
        ("DBI:mysql:database=dbmail", "dbmail", "passwd");
# smtp server and envelope sender, recipient
my($smtpserver, $fromaddr, $toaddr) =
        ("mail.example.com", '[EMAIL PROTECTED]', '[EMAIL PROTECTED]');
# the maximum number of messages to process
my $maxmsgs = 50;
# the mailbox to read from, and then move to on success
my($frombox, $tobox) = (1, 2);

$| = 1;

$dbh = DBI->connect($dsn, $user, $pass);
$dbh->{RaiseError} = 1;

my $sth = $dbh->prepare("select message_idnr, physmessage_id from messages 
where mailbox_idnr = ? limit ?");
$sth->bind_param(1, $frombox, SQL_INTEGER);
$sth->bind_param(2, $maxmsgs, SQL_INTEGER);
$sth->execute;
my $msgs = $sth->fetchall_arrayref;

$sth = $dbh->prepare("select messageblk from messageblks where physmessage_id = 
? order by messageblk_idnr");
my $mvsth = $dbh->prepare("update messages set mailbox_idnr = ? where 
message_idnr = ?");

my $maildata;

my $smtp = Net::SMTP->new($smtpserver);

foreach my $msg (@$msgs) {
        $sth->bind_param(1, $msg->[1], SQL_INTEGER);
        $sth->execute;
        $sth->bind_col(1, \$maildata);

        $smtp->mail($fromaddr);
        $smtp->to($toaddr);
        $smtp->data;
        $smtp->datasend($maildata) while $sth->fetch;
        $smtp->dataend;

        $mvsth->bind_param(1, $tobox, SQL_INTEGER);     
        $mvsth->bind_param(2, $msg->[0], SQL_INTEGER);
        $mvsth->execute;

        print ".";
        sleep 1; # to prevent overwhelming the smtp server
}
print "\n";

Reply via email to