]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blob - scripts/smtpsend
Fixes for 4.19
[thirdparty/kernel/stable-queue.git] / scripts / smtpsend
1 #!/usr/bin/perl
2 #-----------------------------------------------------------------------------
3 # This program gives you low level control over an SMTP conversation.
4 #
5 # This program delivers a mail message to an SMTP server. Command line
6 # options tell what server to use and determine the commands this program
7 # sends to it. The program reads the mail message itself from
8 # Standard Input.
9 #
10 # This program does not generate headers inside the mail message (e.g.
11 # RFC 822 headers). You can use the program 'makemail' to do that.
12 #
13 # This program does not extract any envelope information from the mail
14 # message or rewrite the message in any way.
15 #-----------------------------------------------------------------------------
16
17 use strict;
18 use warnings;
19
20 use Net::SMTP;
21 use Getopt::Long;
22 use Sys::Hostname;
23 use Data::Dumper;
24
25 my $TRUE = 1;
26 my $FALSE = 0;
27
28
29 sub getSender($) {
30 my ($fromOpt) = @_;
31
32 my $retval;
33
34 if (defined($fromOpt)) {
35 $retval = $fromOpt;
36 } else {
37 my $user = $ENV{"USER"} || $ENV{"LOGNAME"};
38 if (!defined("$user")) {
39 die("You didn't supply a sender address with " .
40 "--from and I cannot " .
41 "default one because neither USER nor LOGNAME environment " .
42 "variables is set.");
43 } else {
44 my $hostname = hostname;
45 if (defined($hostname)) {
46 $retval = "$user\@$hostname";
47 } else {
48 $retval = $user;
49 }
50 }
51 }
52 return $retval;
53 }
54
55
56 ##############################################################################
57 # MAINLINE
58 ##############################################################################
59
60 my %options;
61
62 $options{"to"} = []; # Establish as an array reference
63
64 GetOptions(\%options,
65 "to=s",
66 "from=s",
67 "server=s",
68 "hello=s",
69 "timeout=i",
70 "quiet",
71 "debug"
72 );
73
74 if ($options{"debug"}) {
75 Net::SMTP->debug(1);
76 }
77
78 if (@{$options{"to"}} == 0) {
79 die("Must specify the recipient email address with --to");
80 }
81
82 my @recipients = @{$options{"to"}};
83 #print Data::Dumper->Dump([ \@recipients ], [ "recipients" ]);
84
85 my $sender = getSender($options{"from"});
86
87 my $server = $options{"server"} || "localhost";
88
89 my @smtpOptions = (); # initial value
90 if (defined($options{"hello"})) {
91 push(@smtpOptions, Hello => $options{"hello"});
92 }
93 if (defined($options{"timeout"})) {
94 push(@smtpOptions, Timeout => $options{"timeout"});
95 }
96 if ($options{"debug"}) {
97 push(@smtpOptions, Debug => 1);
98 }
99
100 my $smtp = Net::SMTP->new($server, @smtpOptions);
101
102 if (!defined($smtp)) {
103 die("Failed to connect to SMTP server at '$server'");
104 }
105
106 if (!$options{"quiet"}) {
107 print("Server at $server identifies as '" . $smtp->domain . "' " .
108 "and says:\n");
109 print $smtp->banner;
110 print ("\n");
111 print ("Reading mail message from Standard Input...\n");
112 }
113
114 my $result = $smtp->mail($sender);
115 if (!$result) {
116 warn("Failed sending MAIL command. " .
117 "Server says '" . $smtp->message . "'");
118 } else {
119 my $rcptError;
120 foreach my $recipient (@recipients) {
121 my $result = $smtp->recipient($recipient);
122 if (!$result) {
123 warn("Failed sending RCPT command for '$recipient'. " .
124 "Server says '" . $smtp->message . "'");
125 $rcptError = $TRUE;
126 }
127 }
128 if ($rcptError) {
129 $smtp->quit;
130 die("send error");
131 } else {
132 my @message = <STDIN>;
133
134 my $result = $smtp->data(@message);
135
136 if (!$result) {
137 warn("Server rejected message. " .
138 "Server says '" . $smtp->message . "'");
139 $smtp->quit;
140 die("rejected");
141 } else {
142 $smtp->quit;
143 if (!$options{"quiet"}) {
144 my $recipientDesc;
145 if (@recipients == 1) {
146 $recipientDesc = $recipients[0];
147 } else {
148 $recipientDesc = scalar(@recipients) . " recipients";
149 }
150 print("Message sent to $recipientDesc from $sender.\n");
151 }
152 }
153 }
154 }