Skip to content

Instantly share code, notes, and snippets.

@mshock
Last active October 4, 2015 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mshock/2703050 to your computer and use it in GitHub Desktop.
Save mshock/2703050 to your computer and use it in GitHub Desktop.
module for sending e-mail with built-in example
#! perl -w
# framework for e-mail module for importing
package SendEMail;
use strict;
use Net::SMTP;
use Exporter 'import';
our @EXPORT = qw(send_email);
# call module directly to execute code below, otherwise see import example
unless (caller) {
# load all your unfortunate targets into an array
my @recips = qw(list of email recipients);
# pretty sure this is a valid SMTP server
# Porf will know which you should use depending on where you are running from
my $smtp_server = 'some.server.com';
# here's another one that may or may not work depending on where you are
# 'qai-chi-mon01.qai.qaisoftware.com';
# basic message body (short msgs)
my $msg_body = 'read me';
# also supported, reference to message body (long msgs)
#my $msg_body = \$body_text;
# call syntax for sub
# passes anonymous hash reference for options
send_email(
{ smtp_server => $smtp_server,
# array ref to array of recipients
send_to => \@recips,
msg_subject => 'foobie bletch',
msg_body => $msg_body,
# optional, you can masquerade as whomever you like
sender => 'overlord@evil.org',
# optional, auth to SMTP server with specified user (rather than default - executing user)
#user => 'TEN\yourmom'
}
) or die "failed to send mail\n";
}
# THE TRUTH for importers, oughta be 42
1;
# this is the part that gets imported
# returns truth upon success
sub send_email {
my $opts_href = shift;
my $smtp = Net::SMTP->new( $opts_href->{smtp_server} )
or warn
"failed to connect to SMTP server: ${\$opts_href->{smtp_server}}\n"
and return;
# change authentication as appropriate for local machine / SMTP server settings
# this should work in most cases where the current user has privs
$smtp->mail( $opts_href->{user} || $ENV{USER} )
or warn "server auth failed\n" and return;
my @add_list = @{ $opts_href->{send_to} };
foreach my $add (@add_list) {
$smtp->to($add) or warn "failed to add recip: $add\n";
}
$smtp->data();
foreach my $add (@add_list) {
$smtp->datasend("To: $add\n") or warn "failed to add recip: $add\n";
}
$smtp->datasend("From: ${\$opts_href->{sender}}\n")
or warn "could not use sender: ${\$opts_href->{sender}}\n" and return;
$smtp->datasend("Subject: ${\$opts_href->{msg_subject}}\n")
or warn "could not write subject: ${\$opts_href->{msg_subject}}\n"
and return;
$smtp->datasend("\n");
$smtp->datasend( ref( $opts_href->{msg_body} ) eq 'SCALAR'
? ${ \$opts_href->{msg_body} }
: $opts_href->{msg_body}
)
or warn "could not write body: ${\$opts_href->{msg_body}}\n"
and return;
$smtp->dataend();
$smtp->quit;
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment