#! /usr/bin/perl
use strict;

use Mail::IMAPClient;
use Term::ReadPassword;

# Check that username, password and server have been provided

my $Server = "imap.pcl.ox.ac.uk";
my $User = `whoami`;
chomp $User;
my $Password = read_password('Mail password: ');
my $INBOX = "IT-Lilac";

unless ($Server) { die "You must provide a server!"; }
unless ($User) { die "You must provide a user!"; }
unless ($Password) { die "You must provide a password!"; }

print "Connecting to $User\@$Server\n\n";

my $IMAP = Mail::IMAPClient -> new (Server   => $Server,
				    User     => $User,
				    Password => $Password,
				    Debug    => 1)
    or die "Could not connect to your IMAP server, stopping";

if (!$IMAP->IsAuthenticated())
{
    die "Could not log in to your IMAP server, stopping";
}

$IMAP -> Uid(1); # Use UID's for messages.

$IMAP -> select($INBOX);

my @msgs = $IMAP -> search("HEADER","Subject","lilac gpg message");

my $MessageBody = <<'EOM';
>From chris@rodgers.org.uk  Tue Apr 18 21:49:09 2006
Return-Path: <chris@rodgers.org.uk>
Received: from lilac.home (localhost.localdomain [127.0.0.1])
        by lilac.home (8.13.4/8.13.4) with ESMTP id k3IKn8cK004092
    for <chris@lilac.home>; Tue, 18 Apr 2006 21:49:08 +0100
Received: (from chris@localhost)
        by lilac.home (8.13.4/8.13.4/Submit) id k3IKn8Cm004091
    for chris; Tue, 18 Apr 2006 21:49:08 +0100
Date: Tue, 18 Apr 2006 21:49:08 +0100
From: Chris Rodgers <chris@rodgers.org.uk>
Message-Id: <200604182049.k3IKn8Cm004091@lilac.home>
To: chris@lilac.home
Subject: test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

EOM

print "Length is: {" . length($MessageBody) . "}";

$IMAP -> append("IT-Lilac",$MessageBody);
# BUG: For some reason, we lose a bit of the message!

print "\n";

$IMAP = $IMAP -> disconnect();

exit;
