#!/usr/bin/perl use warnings; use strict; use Net::SMTP::SSL; use Net::SMTP::Multipart; # # mailobj is just a hash (aka associative array) # It is just a glob of data that contains all info related to the mail server # my %mailobj=('server','mail.mydomain.edu', 'sender',"\"Sender Name\" ", 'recipient',"\"Recipient Name\" ", 'subject','C is for Cookie', 'body',"This is the body text of the message.", # Unix would be: 'file','/path/to/file', 'file',"c:\\path\\to\\file", 'username','ausername', 'password','apassword', # 465 is de facto standard SMTP-SSL port 'port',465, # Set debug=1 for debugging, 0 for production 'debug',1 ); # # call the 'send_plain_text' subroutine, giving it a reference pointer to the mailobj hash # &send_plain_text(\%mailobj); # # call the 'send_attached_file' subroutine, giving it a reference pointer to the mailobj hash # &send_attached_file(\%mailobj); sub send_plain_text { my ($mailref) = @_; my $smtp; if (not $smtp = Net::SMTP::SSL->new(${$mailref}{'server'}, Port => ${$mailref}{'port'}, Debug => ${$mailref}{'debug'}, Timeout => ${$mailref}{'timeout'})) { die "Could not connect to ${$mailref}{'server'}\n"; } $smtp->auth(${$mailref}{'username'},${$mailref}{'password'}) || die "Authentication failed!\n"; $smtp->mail(${$mailref}{'sender'} . "\n"); $smtp->to(${$mailref}{'recipient'} . "\n"); $smtp->data(); $smtp->datasend("From: " . ${$mailref}{'sender'} . "\n"); $smtp->datasend("To: " . ${$mailref}{'recipient'} . "\n"); $smtp->datasend("Subject: " . ${$mailref}{'subject'} . "\n"); $smtp->datasend("\n"); $smtp->datasend(${$mailref}{'body'} . "\n"); $smtp->dataend(); $smtp->quit; } sub send_attached_file { my ($mailref) = @_; my $smtp; if (not $smtp = Net::SMTP::Multipart->new(${$mailref}{'server'}, Port => ${$mailref}{'port'}, Debug => ${$mailref}{'debug'}, Timeout => ${$mailref}{'timeout'})) { die "Could not connect to ${$mailref}{'server'}\n"; } $smtp->auth(${$mailref}{'username'},${$mailref}{'password'}) || die "Authentication failed!\n"; $smtp->Header(To => ${$mailref}{'recipient'}, Subj => ${$mailref}{'subject'}, From => ${$mailref}{'sender'}); $smtp->Text(${$mailref}{'body'}); $smtp->FileAttach(${$mailref}{'file'}); $smtp->End(); }