Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

flexible command line mail sender (SMTP)

by holli (Monsignor)
on Jun 23, 2005 at 07:44 UTC ( #469311=sourcecode: print w/ replies, xml ) Need Help??

Category: E-Mail Programs
Author/Contact Info holli
Description: A Simple tool to send mails from the command line via a smptp server.
# 
# EDVMAIL 0.2
#
use warnings;
use strict;

use Mail::Sender;
use File::Slurp;

use Getopt::Attribute;
use Getopt::ArgvFile;

our $to          : Getopt(an=s);
our $from        : Getopt(absender=s EDV);
our $subject     : Getopt(betreff=s);
our $password    : Getopt(passwort=s xxxx);
our $mode        : Getopt(modus=s html);

our @cc          : Getopt(cc=s);
our @mailfiles   : Getopt(dateien=s);
our @attachments : Getopt(attachments=s);
our @lines       : Getopt(zeilen=s);

die "Kein Adressat angegeben!\n"   . usage()
    unless defined $to;

die "Kein Betreff angegeben!\n"    . usage()
    unless defined $subject;

die "Keine Nachricht angegeben!\n" . usage()
    unless (@mailfiles or @lines);

my ($br, $ctype);

if ( $mode =~ /html/i )
{
    $br    = "<br>";
    $ctype = "text/html";
}
else
{
    $br    = "";
    $ctype = "text/plain";
}    

my $sender = new Mail::Sender 
{

    smtp    => '192.168.1.23', 
    from    => "$from\@kvsaarland.de",
    authid  => $from,
    authpwd => $password,
}
or die "Mail::Sender Objekt kann nicht erstellt werden: $Mail::Sender:
+:Error\n";

$sender->OpenMultipart
({
    to => $to, 
    subject => $subject, 
    cc => [@cc],
    boundary => ""
});

$sender->Body({ encoding => 'ISO-8859-1', ctype=>$ctype });

$sender->Send('<font face="Arial" size="2">')
    if $br;

if ( @lines )
{
    $sender->Send ( map { chomp; "$_$br\n" } @lines )
        or die "$Mail::Sender::Error\n";
}

if ( @mailfiles )
{
    $sender->Send ( map { map { chomp; "$_$br\n" } read_file ($_) } @m
+ailfiles )
        or die "$Mail::Sender::Error\n";
}

$sender->Send('</font>')
    if $br;

if ( @attachments )
{
    $sender->Send   ( "$br\n" x 2 ),
    $sender->Attach ( { file => $_ } ) 
        for @attachments;
}

$sender->Close()
    or die "Nachricht kann nicht gesendet werden: $sender->{'error_msg
+'}\n";

print "Nachricht versendet.\n";
    
sub usage
{
    return <<'USAGE';
edvmail.pl - 0.2

Aufruf:
 edvmail -parameter [wert]

Parameter:
 -ab(sender)      opt. Absender (ohne "@kvsaarland.de", default: EDV)
 -an              erf. Empf„nger
 -cc            * opt. Blindkopie
 -b(betreff)      erf. Betreff
 -p(asswort)      opt. Passwort, default "xxxx"
 -d(atei)       * opt. Textdateien fr den Text der Mail
 -at(tachments) * opt. Anh„nge
 -z(eilen)      * opt. Textzeilen
 -m(odus)         opt. "text" or "html", default "html"

Mit * gekennzeichnete Parameter können mehrfach angegeben werden.

Beispiel:
 edvmail -ab m.holzer -an p.balfie@kvsaarland.de -cc m.taller@kvsaarla
+nd.de 
         -b "Na ihr Luschen?" -z "Was Lustiges" -z "im Anhang" -at c:\
+witze.txt
USAGE
}

Comment on flexible command line mail sender (SMTP)
Download Code
Re: flexible command line mail sender (SMTP)
by davidrw (Prior) on Jun 23, 2005 at 11:33 UTC
    Looks handy (and a good sample for including in other programs the next time i need to kick off emails, especially with attachments).

    One quick request though -- can you post (or make a usage_en() sub or in __END__ or in =pod) an english translation of the USAGE heredoc? thanks!
      Here's your english version:
      # # EDVMAIL 0.2 # use warnings; use strict; use Mail::Sender; use File::Slurp; use Getopt::Attribute; use Getopt::ArgvFile; our $to : Getopt(to=s); our $from : Getopt(from=s EDV); our $subject : Getopt(subject=s); our $password : Getopt(password=s xxxx); our $mode : Getopt(mode=s html); our @cc : Getopt(cc=s); our @mailfiles : Getopt(files=s); our @attachments : Getopt(attachments=s); our @lines : Getopt(lines=s); die "No recipient specified!\n" . usage() unless defined $to; die "No subject specified!\n" . usage() unless defined $subject; die "No message specified!\n" . usage() unless (@mailfiles or @lines); my ($br, $ctype); if ( $mode =~ /html/i ) { $br = "<br>"; $ctype = "text/html"; } else { $br = ""; $ctype = "text/plain"; } my $sender = new Mail::Sender { smtp => '192.168.1.23', from => "$from\@kvsaarland.de", authid => $from, authpwd => $password, } or die "Cannot create Mail::Sender Object: $Mail::Sender::Error\n"; $sender->OpenMultipart ({ to => $to, subject => $subject, cc => [@cc], boundary => "" }); $sender->Body({ encoding => 'ISO-8859-1', ctype=>$ctype }); $sender->Send('<font face="Arial" size="2pt">') if $br; if ( @lines ) { $sender->Send ( map { chomp; "$_$br\n" } @lines ) or die "$Mail::Sender::Error\n"; } if ( @mailfiles ) { $sender->Send ( map { map { chomp; "$_$br\n" } read_file ($_) } @m +ailfiles ) or die "$Mail::Sender::Error\n"; } $sender->Send('</font>') if $br; if ( @attachments ) { $sender->Send ( "$br\n" x 2 ), $sender->Attach ( { file => $_ } ) for @attachments; } $sender->Close() or die "Message not sent: $sender->{'error_msg'}\n"; print "Message sent.\n"; sub usage { return <<'USAGE'; edvmail.pl - 0.2 Usage: edvmail -parameter [wert] Parameter: -fr(om) opt. Author (without "@domain", default: EDV) -to req. Recipient -cc * opt. CC-Adresses -s(ubject) req. subject -p(assword) opt. Password, default "xxxx" -fi(les) * opt. Textfiles that build the text for the Mail -a(ttachments) * opt. Well, attachments. -l(ines) * opt. Text of the mail -m(ode) opt. "text" or "html", default "html" Parameters with * can me specified multiple times. Example: edvmail -fr m.holzer -to p.balfie@kvsaarland.de -cc m.taller@kvsaarla +nd.de -s "How ya doin'?" -l "Something funny" -l "in the attachment +" -a c:\jokes.txt USAGE }


      holli, /regexed monk/

Back to Code Catacombs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://469311]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (17)
As of 2014-08-22 14:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (158 votes), past polls