[PATCH RFC3.5 06/12] send-email: Cleanup and streamline the SMTP code in send_message

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Some of the code was never used or not necessary; it should
be easier to read now.

The code could even be simplified further, because Net::SMTP{,::SSL}
both take the PORT variable in their new methods (which, as of this
commit, are actually the same method). Moreover, both take a server
URI of the form 'host:port' that trumps any value passed to PORT.

Unfortunately, none of this is documented publicly, so it isn't
exploited out of purity.

Signed-off-by: Michael Witten <mfwitten@xxxxxxxxx>
---
 git-send-email.perl |   93 +++++++++++++++++++++++++++-----------------------
 1 files changed, 50 insertions(+), 43 deletions(-)

diff --git a/git-send-email.perl b/git-send-email.perl
index 2727c77..6e2ea2c 100755
--- a/git-send-email.perl
+++ b/git-send-email.perl
@@ -985,67 +985,74 @@ X-Mailer: git-send-email $gitversion
 
 	} else {
 
-		if (!defined $smtp_server) {
-			die "The required SMTP server is not properly defined."
-		}
+		goto SEND_MAIL if $smtp;
+
+		if ($smtp_encryption =~ /ssl/i) {
+
+			use Net::SMTP::SSL;
+			$smtp = Net::SMTP::SSL->new($smtp_server, Port => $smtp_server_port // 465)
+				or die "Could not connect to SSL SMTP server '$smtp_server:$smtp_server_port'\n";
+
+		} else {
+
+			use Net::SMTP;
+
+			my $server_URI = (defined $smtp_server_port)
+						? "$smtp_server:$smtp_server_port"
+						:  $smtp_server;
+
+			$smtp = Net::SMTP->new($server_URI)
+				or die "Could not connect to SMTP server: '$server_URI'\n";
+
+			if ($smtp_encryption =~ /tls/i) {
 
-		if ($smtp_encryption eq 'ssl') {
-			$smtp_server_port ||= 465; # ssmtp
-			require Net::SMTP::SSL;
-			$smtp ||= Net::SMTP::SSL->new($smtp_server, Port => $smtp_server_port);
-		}
-		else {
-			require Net::SMTP;
-			$smtp ||= Net::SMTP->new((defined $smtp_server_port)
-						 ? "$smtp_server:$smtp_server_port"
-						 : $smtp_server);
-			if ($smtp_encryption eq 'tls') {
-				require Net::SMTP::SSL;
 				$smtp->command('STARTTLS');
-				$smtp->response();
-				if ($smtp->code == 220) {
-					$smtp = Net::SMTP::SSL->start_SSL($smtp)
-						or die "STARTTLS failed! ".$smtp->message;
-					$smtp_encryption = '';
-					# Send EHLO again to receive fresh
-					# supported commands
-					$smtp->hello();
-				} else {
-					die "Server does not support STARTTLS! ".$smtp->message;
-				}
-			}
-		}
+				$smtp->response(); # so $smtp->code works.
+
+				die "Server does not support STARTTLS: " . $smtp->message . "\n"
+					unless $smtp->code == 220;
+
+				use Net::SMTP::SSL;
+				Net::SMTP::SSL->start_SSL($smtp)
+					or die "STARTTLS failed! " . $smtp->message . "\n";
+
+				# Send EHLO again to receive fresh
+				# supported commands:
 
-		if (!$smtp) {
-			die "Unable to initialize SMTP properly.  Is there something wrong with your config?";
+				$smtp->hello();
+			}
 		}
 
 		if (defined $smtp_authuser) {
 
-			if (!defined $smtp_authpass) {
+			unless (defined $smtp_authpass) {
 
 				system "stty -echo";
 
-				do {
+				{
 					print "Password: ";
-					$_ = <STDIN>;
+					$smtp_authpass = <STDIN>;
 					print "\n";
-				} while (!defined $_);
-
-				chomp($smtp_authpass = $_);
+					redo unless defined $smtp_authpass;
+					chomp($smtp_authpass);
+				}
 
 				system "stty echo";
 			}
 
-			$auth ||= $smtp->auth( $smtp_authuser, $smtp_authpass ) or die $smtp->message;
+			$smtp->auth($smtp_authuser, $smtp_authpass)
+				or die "Could not authenticate '$smtp_authuser': " . $smtp->message . "\n";
 		}
 
-		$smtp->mail( $raw_from ) or die $smtp->message;
-		$smtp->to( @recipients ) or die $smtp->message;
-		$smtp->data or die $smtp->message;
-		$smtp->datasend("$header\n$message") or die $smtp->message;
-		$smtp->dataend() or die $smtp->message;
-		$smtp->code =~ /250|200/ or die "Failed to send $subject\n".$smtp->message;
+		SEND_MAIL:
+
+		$smtp->mail($raw_from)               and
+		$smtp->to(@recipients)               and
+		$smtp->data                          and
+		$smtp->datasend("$header\n$message") and
+		$smtp->dataend                       or
+
+		die "Failed to send '$subject': " . $smtp->message . "\n";
 	}
 	if ($quiet) {
 		printf (($dry_run ? "Dry-" : "")."Sent %s\n", $subject);
-- 
1.6.2.2.479.g2aec

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]