#!/usr/bin/perl

use Getopt::Long;
use URI::Escape;

my @arguments;

$result = GetOptions (
	"to=s"		=> \$recipient,
	"subject=s"	=> \$subject,
	"cc=s"		=> \$recipient_cc,
	"bcc=s"		=> \$recipient_bcc,
	"body=s"	=> \$body,
	"attach=s"	=> \$attachment,
	"sylpheed:s"    => \$sylpheed
	);

if (!$sylpheed){
	$sylpheed = `which sylpheed`;
	chomp($sylpheed);
}

if (! -x $sylpheed){
	die("Sylpheed not executable (checked file was '" . $sylpheed . "')\n");
}

$url  = "mailto:"  . $recipient           . "?";
$url .= "cc="      . $recipient_cc        . "&";
$url .= "bcc="     . $recipient_bcc       . "&";
$url .= "subject=" . uri_escape($subject) . "&";
$url .= "body="    . uri_escape($body);

push (@arguments, "--compose");
push (@arguments, $url);
push (@arguments, "--attach");
push (@arguments, $attachment);

exec($sylpheed, @arguments) || die ("Error launching sylpheed\n");

