aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/smtpsend
blob: b314b2f547f39d51c69098650570d310326e9966 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/perl
#-----------------------------------------------------------------------------
#  This program gives you low level control over an SMTP conversation.
#
#  This program delivers a mail message to an SMTP server.  Command line
#  options tell what server to use and determine the commands this program
#  sends to it.  The program reads the mail message itself from 
#  Standard Input.
#
#  This program does not generate headers inside the mail message (e.g.
#  RFC 822 headers).  You can use the program 'makemail' to do that.
#
#  This program does not extract any envelope information from the mail
#  message or rewrite the message in any way.
#-----------------------------------------------------------------------------

use strict;
use warnings;

use Net::SMTP;
use Getopt::Long;
use Sys::Hostname;
use Data::Dumper;

my $TRUE = 1;
my $FALSE = 0;


sub getSender($) {
    my ($fromOpt) = @_;

    my $retval;
    
    if (defined($fromOpt)) {
        $retval = $fromOpt;
    } else {
        my $user = $ENV{"USER"} || $ENV{"LOGNAME"};
        if (!defined("$user")) {
            die("You didn't supply a sender address with " .
                "--from and I cannot " .
                "default one because neither USER nor LOGNAME environment " .
                "variables is set.");
        } else {
            my $hostname = hostname;
            if (defined($hostname)) {
                $retval = "$user\@$hostname";
            } else {
                $retval = $user;
            }
        }
    }
    return $retval;
}


##############################################################################
#       MAINLINE
##############################################################################

my %options;

$options{"to"} = [];   # Establish as an array reference

GetOptions(\%options, 
           "to=s",
           "from=s",
           "server=s",
           "hello=s",
           "timeout=i",
           "quiet",
           "debug"
           );

if ($options{"debug"}) {
  Net::SMTP->debug(1);
}

if (@{$options{"to"}} == 0) {
    die("Must specify the recipient email address with --to");
} 

my @recipients = @{$options{"to"}};
#print Data::Dumper->Dump([ \@recipients ], [ "recipients" ]);

my $sender = getSender($options{"from"});

my $server = $options{"server"} || "localhost";

my @smtpOptions = ();  # initial value
if (defined($options{"hello"})) {
    push(@smtpOptions, Hello => $options{"hello"});
}
if (defined($options{"timeout"})) {
    push(@smtpOptions, Timeout => $options{"timeout"});
}
if ($options{"debug"}) {
    push(@smtpOptions, Debug => 1);
}

my $smtp = Net::SMTP->new($server, @smtpOptions);

if (!defined($smtp)) {
    die("Failed to connect to SMTP server at '$server'");
}

if (!$options{"quiet"}) {
    print("Server at $server identifies as '" . $smtp->domain . "' " .
          "and says:\n");
    print $smtp->banner;
    print ("\n");
    print ("Reading mail message from Standard Input...\n");
}

my $result = $smtp->mail($sender);
if (!$result) {
    warn("Failed sending MAIL command.  " .
         "Server says '" . $smtp->message . "'");
} else {
    my $rcptError;
    foreach my $recipient (@recipients) {
        my $result = $smtp->recipient($recipient);
        if (!$result) {
            warn("Failed sending RCPT command for '$recipient'.  " .
                 "Server says '" . $smtp->message . "'");
            $rcptError = $TRUE;
        }
    }
    if ($rcptError) {
        $smtp->quit;
        die("send error");
    } else {
        my @message = <STDIN>;

        my $result = $smtp->data(@message);

        if (!$result) {
            warn("Server rejected message.  " .
                 "Server says '" . $smtp->message . "'");
            $smtp->quit;
            die("rejected");
        } else {
            $smtp->quit;
            if (!$options{"quiet"}) {
                my $recipientDesc;
                if (@recipients == 1) {
                    $recipientDesc = $recipients[0];
                } else {
                    $recipientDesc = scalar(@recipients) . " recipients";
                }
                print("Message sent to $recipientDesc from $sender.\n");
            }
        }
    }
}