Created
August 3, 2015 08:58
-
-
Save jddurand/49b23c393792d557a6ad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!env perl | |
use strict; | |
use diagnostics; | |
use Getopt::Long; | |
use POSIX qw/EXIT_SUCCESS EXIT_FAILURE/; | |
use MIME::Lite; | |
use File::Basename; | |
# | |
## Options | |
# ------- | |
my $hello = ''; | |
my $host = $ENV{SMTP_HOSTNAME} || ''; | |
my $timeout = 0; | |
my $debug = 0; | |
my $login = ''; | |
eval 'use Win32; $login = Win32::LoginName() || $ENV{USERNAME} || $ENV{USER} || "";' if ($^O =~ /win32/smi); | |
$login = getlogin || getpwuid($<) || 'Unknown.Sender' if (! $login); | |
my $domain = ''; | |
eval 'use Win32; $domain = Win32::NodeName() || $ENV{COMPUTERNAME} || "";' if ($^O =~ /win32/smi); | |
eval 'use Sys::Hostname::Long; $domain = hostname_long() || "";' if (! $domain); | |
$domain = 'localhost.localdomain' if (! $domain); | |
$login = getlogin || getpwuid($<) || 'Unknown.Sender' if (! $login); | |
my $from = $ENV{MAIL_FROM} || "$login\@$domain"; | |
my $help = 0; | |
my @to = (); | |
my @cc = (); | |
my @bcc = (); | |
my $body = ''; | |
my $subject = ''; | |
my @file = (); | |
GetOptions("debug" => \$debug, | |
"timeout=i" => \$timeout, | |
"subject=s" => \$subject, | |
"hello=s" => \$hello, | |
"from=s" => \$from, | |
"to=s" => \@to, | |
"cc=s" => \@cc, | |
"bcc=s" => \@bcc, | |
"host=s" => \$host, | |
"body=s" => \$body, | |
"file=s" => \@file, | |
"help" => \$help); | |
# | |
## Usage | |
# ----- | |
if ($help || ! $from || ! $host || ! @to) { | |
print STDERR "Usage: $^X $0 [options]\n"; | |
print STDERR "\n"; | |
print STDERR "where mandatory options are:\n"; | |
print STDERR "\n"; | |
print STDERR "\t--from=<from> Sender address. Default is: $from\n"; | |
print STDERR "\t--host=<host> SMTP Host. Default is: $host\n"; | |
print STDERR "\t--to=<to> Destination addresses. Use --to more than once if needed.\n"; | |
print STDERR "\n"; | |
print STDERR "where other options are:\n"; | |
print STDERR "\n"; | |
print STDERR "\t--file=<file> Attachement. Use --file more than once if needed.\n"; | |
print STDERR "\t--subject=<subject> Subject.\n"; | |
print STDERR "\t--debug Debug mode\n"; | |
print STDERR "\t--timeout=<timeout> Timeout in seconds waiting for a response from SMTP host.\n"; | |
print STDERR "\t--hello=<hello> String to pass as mail domain. Default is Net::SMTP default, e.g. localhost.localdomain.\n"; | |
print STDERR "\t--body=<body> Body of the mail. Default is to read STDIN.\n"; | |
print STDERR "\t--help This help\n"; | |
print STDERR "\n"; | |
print STDERR "If there is no output, this script will wait for input on STDIN. Thus please always provide something in the STDIN, and say echo \"\" | ... if you want nothing\n"; | |
print STDERR "\n"; | |
print STDERR "Example:\n"; | |
print STDERR "echo My_Body | $^X $0 --subject \"Test\" --to $from --file /etc/group\n"; | |
print STDERR " $^X $0 --subject \"Test\" --to $from --file /etc/group --body My_Body\n"; | |
print STDERR "\n"; | |
print STDERR "Environment variables:\n"; | |
print STDERR "SMTP_HOSTNAME for smtp host\n"; | |
print STDERR "USERNAME, USER for current login (Win32)\n"; | |
print STDERR "COMPUTERNAME for current login (Win32)\n"; | |
print STDERR "MAIL_FROM for the From: part in mail header\n"; | |
print STDERR "\n"; | |
exit(EXIT_SUCCESS); | |
} | |
# | |
## Create a new multipart message | |
# ------------------------------ | |
my $msg = MIME::Lite->new(From => $from, To => [ @to ], Cc => [ @cc ], Bcc => [ @bcc ], | |
Subject => $subject, | |
Type => 'multipart/mixed' | |
); | |
# | |
## Add the text message part | |
# ------------------------- | |
if (! defined($body) || ! $body) { | |
$body = do { local $/; <> }; | |
} | |
$msg->attach(Type => 'TEXT', Data => $body) if (defined($body) && $body); | |
# | |
## Add the attachements | |
# -------------------- | |
map {$msg->attach(Type => 'AUTO', Path => $_, Filename => basename($_), Disposition => 'attachment')} @file; | |
# | |
## Send the Message | |
# ---------------- | |
my %smtpopts = (); | |
$smtpopts{Debug} = $debug if ($debug); | |
$smtpopts{Timeout} = $timeout if ($timeout); | |
$smtpopts{Hello} = $hello if ($hello); | |
$smtpopts{To} = [ @to, @cc, @bcc ]; | |
$smtpopts{From} = $from; | |
$msg->send_by_smtp($host, %smtpopts); | |
exit(EXIT_SUCCESS); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment