Come posso allegare più file a un'e -mail usando Perl?
-
27-10-2019 - |
Domanda
Trovo difficile credere che questa domanda non esista, ma non sono riuscito a trovare un'istanza o una simile per Perl ....
Comunque, quale modulo Perl dovrei utilizzare per allegare più file a un'e -mail?
Attualmente, sto usando questo codice per inviare un'e -mail con un singolo allegato, ma non sono riuscito a capire come modificarlo per gestire più allegati:
my $mail_fh = \*MAIL;
open $mail_fh, "|uuencode $attachment $attachment |mailx -m -s \"$subject\" -r $sender $recipient";
print $mail_fh $message;
close($mail_fh);
Questo blocco di codice può essere modificato per gestire più allegati? O devo usare un modulo speciale per realizzarlo? In tal caso, qual è il modulo e come lo scriverei?
Grazie per qualsiasi aiuto!
Soluzione
Ho finito per fare un esempio usando MIME::Lite
fondare qui
use MIME::Lite;
use Getopt::Std;
my $SMTP_SERVER = 'smtp.server.com'; #change
my $DEFAULT_SENDER = 'default@sender.com'; #change
my $DEFAULT_RECIPIENT = 'default@recipient.com'; #change
MIME::Lite->send('smtp', $SMTP_SERVER, Timeout=>60);
my (%o, $msg);
# process options
getopts('hf:t:s:', \%o);
$o{f} ||= $DEFAULT_SENDER;
$o{t} ||= $DEFAULT_RECIPIENT;
$o{s} ||= 'Files';
if ($o{h} or !@ARGV) {
die "usage:\n\t$0 [-h] [-f from] [-t to] [-s subject] files ...\n";
}
# construct and send email
$msg = new MIME::Lite(
From => $o{f},
To => $o{t},
Subject => $o{s},
Data => "Data",
Type => "multipart/mixed",
);
while (@ARGV) {
$msg->attach('Type' => 'application/octet-stream',
'Encoding' => 'base64',
'Path' => shift @ARGV);
}
$msg->send( );
Esempio di utilizzo:
./notify_mime.pl -f cheese -t queso -s subject /home/id/cheeseconqueso/some_dir/example1.xls /home/id/cheeseconqueso/some_other_dir/*.xls
Altri suggerimenti
Vedere attach_file
in Email :: roba, o Email :: mime Se hai bisogno di più controllo.
Nonostante le valutazioni contrastanti, ho trovato Mail :: Sender (ed è amico Mail :: Sender :: Easy) Darn buono e diretto da usare e sembra che possa gestire più attacchi.
Ho trovato l'interfaccia estremamente fastidiosa Mail :: Internet.
Tutto dovrebbe essere migliore di quello che hai sopra, però. :-)