# Expects at least 1 argument matching the type of e-mail to send, and # optionally the additional arguments it might need. If this got much # more complicated, it might be worth passing the second argument as a # hash to simplify multiple values coming in. sub send_mail { # These could go at the beginning of your script, I just put them # here to make this a more complete example. use MIME::Lite; MIME::Lite->send( "smtp", "mail.server.com" ); my $type = shift; # what type of message should we send? my @args = @_; my $from = 'foo@example.com'; my $to = 'bar@example.com'; my $msg; if ( $type eq "ip_list_error" ) { $msg = MIME::Lite->new( From => $from, To => $to, Data => "Error:nas_array_ip_list.txt:$!.$^E\n", Subject => "IP List File - nas_array_ip_list.txt - " . "Not Found For $0 Script on $ENV{COMPUTERNAME}\n", ); } elsif ( $type eq "results" ) { my $filename = $args[0]; $msg = MIME::Lite->new( From => $from, To => $to, Subject => "Automated Check For NAS DataMover Health.", Type => 'Multipart/mixed', ); $msg->attach( Type => 'TEXT', Path => $filename, Filename => $filename, Disposition => 'inline', ); } else { die "send_mail - bad type: $type\n" } # This will send our e-mail, regardless of which one we built. $msg->send; }