# this is a example

use Mail::Cclient;

my @attachs = (
	{
		content_type => "image/gif",
		filename     => "image.gif",
		encoding     => "binary",
		path         => "files/image.gif",
	},
	{
		content_type => "image/jpeg",
		filename     => "image.jpg",
		encoding     => "binary",
		path         => "files/image.jpg",
	},
);

my $envelope = {
	from    => "hdias@aeiou.pt",
	to      => "xyz@perl.com",
	cc      => "abc@perl.com",
	subject => "another test",
};
        
my $data = "bla bla bla";
my $signature = "\n\n--\n--Henrique Dias--\n";

my $body = {
	content_type => "text/plain",
	encoding     => "quoted-printable",
	data         => $data . $signature,
}

my $i = 0;
my $part = [];
for my $attach (@attachs) {
	$part->[$i++] = {
		content_type => $attach->{'content_type'},
		disposition  => {
			type      => "attachment",
			parameter => [{
				attribute => "filename",
				value     => $attach->{'filename'},
			}],
		},
		encoding     => $attach->{'encoding'},
		path         => $attach->{'path'},
		parameter    => [{
			attribute => "name",
			value     => $attach->{'filename'},
		}],
	};
}
if(scalar(@{$part})) {
	unshift(@{$part}, $body);
	$body = {
		content_type => "multipart/mixed",
		part         => $part,
	};
}

my $smtp = Mail::Cclient::SMTP->new(["smtp.aeiou.pt"]);
open(FILE, ">tmp.rfc822") or die "$!\n";
my $fh = *FILE;
$smtp->mail(
	filehandle  => $fh,
	envelope    => $envelope,
	body        => $body,
);
seek($fh, 0, 0);
local $/ = undef;
my $text = <$fh>;
close(FILE);
$smtp->close();

# Append the message to mailbox
my $msg2mb = "sent-mail";
my $mailbox = "{mail.aeiou.pt/imap}mail/$msg2mb";
$cclient->append($mailbox, $text);

exit(0);
