I'm getting an error when using the Socket SMTP part of this code (not the sendmail part) below on Windows 10 Perl 5.22 with taint mode on.
I would like to get the code working on windows and linux, but the first test on windows gave this error.
Sending Email: Sender address 'valid@email.was.used' not valid. inappropriate i/o control operation
What can be done to get this portable?
sub send_email {
my ($self, %email_set) = @_;
my ($x, $here, $there, $null) = ('', '', '', '');
# Format input.
$email_set{to} =~ s/[ \t]+/, /g;
$email_set{from} =~ s/.*<([^\s]*?)>/$1/;
$email_set{message} =~ s/^\./\.\./gm;
#$email_set{message} =~ s/\r\n/\n/g;
#$email_set{message} =~ s/\n/\r\n/g;
$cfg{smtp_server} =~ s/\A\s+|\s+\z//g;
# Send email via SMTP.
if ($cfg{mail_type} == 1) {
($x, $x, $x, $x, $here) = gethostbyname($null);
($x, $x, $x, $x, $there) = gethostbyname($cfg{smtp_server});
my $thisserver = pack('S n a4 x8', 2, 0, $here);
my $remoteserver = pack('S n a4 x8', 2, 25, $there);
use Carp;
croak "Socket failure. $!" if (!(socket(S, 2, 1, 6)));
croak "Bind failure. $!" if (!(bind(S, $thisserver)));
croak "Connection to $cfg{smtp_server} has failed. $!"
if (!(connect(S, $remoteserver)));
my $oldfh = select(S);
$| = 1;
select($oldfh);
$_ = <S>;
croak "Sending Email: data in Connect error - 220. $!"
if ($_ !~ /^220/);
print S "HELO $cfg{smtp_server}\r\n";
$_ = <S>;
croak "Sending Email: data in Connect error - 250. $!"
if ($_ !~ /^250/);
print S "MAIL FROM:<$email_set{from}>\n";
$_ = <S>;
croak "Sending Email: Sender address '$email_set{from}' not valid. $
+!"
if ($_ !~ /^250/);
print S "RCPT TO:<$email_set{to}>\n";
$_ = <S>;
croak "Sending Email: Recipient address '$email_set{to}' not valid.
+$!"
if ($_ !~ /^250/);
print S "DATA\n";
$_ = <S>;
croak "Sending Email: Message send failed - 354. $!"
if ($_ !~ /^354/);
}
# Send email via sendmail.
$ENV{PATH} = '';
if ($cfg{mail_type} == 0) {
open S, "| $cfg{mail_program} -t" or croak "Mailprogram error. at $!
+";
print S <<THE_EMAIL;
From: $email_set{from}
Subject: $email_set{subject}
To: $email_set{to}
X-Mailer: Flex-WPS Mail_Module v1.0
Content-type: text/plain
$email_set{message}
THE_EMAIL
}
# Send email via SMTP.
if ($cfg{mail_type} == 1) {
$_ = <S>;
croak "Sending Email: Message send failed - try again - 250. $!"
if ($_ !~ /^250/);
print S "QUIT\n";
}
close(S);
return 1;
}