Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- use Net::DNS;
- use Net::SMTP;
- #argv to from subject message
- if($#ARGV+1 != 4){
- print "Usage: script.pl to from subject message\n";
- exit 1;
- }
- my $to = $ARGV[0];
- my $from = $ARGV[1];
- my $subject = $ARGV[2];
- my $msg = $ARGV[3];
- my $domain = "";
- if($to =~ m/\@(.+)$/){
- $domain = $1;
- }else{
- die "Could not get domain from '$to'\n";
- }
- my %mx = ();
- my $res = Net::DNS::Resolver->new;
- my $query = $res->query($domain, "MX");
- my @result = $query->answer;
- print "found ".(@result)." mx entries for '$domain'\n";
- if(@result){
- foreach my $rr (@result){
- $mx{$rr->exchange} = $rr->preference;
- print $rr->preference." - ".$rr->exchange."\n";
- }
- }else{
- die "Can't find MX records for '$domain': ", $res->errorstring, "\n";
- }
- my $sent = 0;
- my @mailhosts = sort { $mx{$a} cmp $mx{$b} } keys %mx;
- foreach my $host(@mailhosts){
- print "Connecting to $host...\n";
- my $smtp = Net::SMTP->new($host) or next;
- $smtp->hello('mailhost');
- $smtp->mail($from);
- $smtp->to($to);
- $smtp->data();
- $smtp->datasend("Subject: $subject\r\n");
- $smtp->datasend("From: $from\r\n");
- $smtp->datasend("To: $to\r\n");
- $smtp->datasend("$msg");
- $smtp->dataend();
- $sent = 1;
- last;
- }
- if(!$sent){
- die("Could not send email...");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement