#!/usr/bin/perl -w
$SIG
{__DIE__} =
sub
{
die
"mime_postcard: $_[0]\n"
};
sub
usage {
my
$problem
=
join
''
,
@_
;
print
STDERR
"\n"
;
print
STDERR
"Error: $problem\n"
if
$problem
;
my
@usage
;
if
(
open
O,
'<'
.$0){
local
($/) =
"\n"
;
1
while
(
defined
(
$_
= <O>) and !/^=head1\s+SYNOPSIS/);
push
@usage
,
$_
while
(
defined
(
$_
= <O>) and !/^=/);
close
O;
}
my
$u
=
join
''
,
@usage
;
$u
=~ s/\A\n+//;
$u
=~ s{\n\n+(\s+)}{\n$1}g;
print
STDERR
$u
,
"\n"
;
exit
-1;
}
sub
main {
my
%opts
;
getopts(
""
, \
%opts
);
my
$graphic
=
shift
@ARGV
|| usage
"missing path to graphic\n"
;
(-r
$graphic
) or
die
"$graphic unreadable\n"
;
my
$graphic_type
;
if
(
$graphic
=~ /\.gif$/i) {
$graphic_type
=
"image/gif"
}
elsif
(
$graphic
=~ /\.jpe?g$/i) {
$graphic_type
=
"image/jpeg"
}
elsif
(
$graphic
=~ /\.png$/i) {
$graphic_type
=
"image/png"
}
else
{
die
"unknown type for: $graphic\n"
; }
my
$gid
=
"my-graphic"
;
my
$dest
=
shift
@ARGV
|| usage
"missing destination\n"
;
my
$msg
= MIME::Entity->build(
To
=>
$dest
,
Subject
=>
'A postcard for you'
,
Type
=>
'multipart/alternative'
);
my
$plain
=
$msg
->attach(
Type
=>
'text/plain'
,
Data
=> [
"Having a wonderful time... \n"
,
"wish you were looking at HTML \n"
,
"instead of this boring text!\n"
]);
my
$fancy
=
$msg
->attach(
Type
=>
'multipart/related'
);
$fancy
->attach(
Type
=>
'text/html'
,
Data
=> [
qq< <H1>
Hey there!</H1> \n>,
qq< Having a <I>
wonderful</I>
time
... \n>,
qq< take a look!\n >
,
qq< <BR>
<IMG SRC=
"cid:$gid"
ALT=
"Snapshot"
><HR>>
]);
$fancy
->attach(
Type
=>
$graphic_type
,
Path
=>
$graphic
,
Id
=>
$gid
);
if
(
$dest
eq
'-'
) {
$msg
->
print
;
}
else
{
$msg
->smtpsend();
}
}
eval
{ main() };
die
"$0: $@"
if
$@;