use Mail::Sender; #this calls the function &send_email( from => $from, to => $to, subject => $subject, message => $message, ); #this is the function sub send_email{ my %data = @_; #Hash #Check required fields foreach(qw( to from subject message )){ if(!exists $data{$_}){ return(1, "Missing $_"); } } #Defaults $data{contenttype} = ($data{contenttype}) ? $data{contenttype} : 'text/plain'; #Build CC string my ($CCs); if(ref($data{cc})){ $CCs = join(',', @{$data{cc}}); }elsif(length($data{cc}) > 0){ $CCs = $data{cc}; } #Build BCC string my ($BCCs); if(ref($data{bcc})){ $BCCs = join(',', @{$data{bcc}}); }elsif(length($data{bcc}) > 0){ $BCCs = $data{bcc}; } # Send the email my $mail_errors; my %mailInfo; $mailInfo{from} = $data{from}; $mailInfo{to} = $data{to}; $mailInfo{subject} = $data{subject}; $mailInfo{b_ctype} = $data{contenttype}; $mailInfo{ctype} = $data{contenttype}; $mailInfo{cc} = $CCs if($CCs); $mailInfo{bcc} = $BCCs if($BCCs); foreach my $smtp ('SMTP SERVER 1', 'SMTP SERVER N') { # Set up email information $mailInfo{'smtp'} = $smtp; $mailInfo{'msg'} = $data{message}; # Send the email if (ref((new Mail::Sender)->MailMsg(\%mailInfo))) { return (0, ''); # Success } # Problem with sending the email $mail_errors .= "SMTP address: $smtp\nError: " . $Mail::Sender::Error . "\n"; } if($mail_errors){ chop $mail_errors; return (1, $mail_errors); } }