# [PATCH] Add ability to specify SMTP server port when using git-send-email.

2 messages from 2007-09-24 to 2007-09-25. Participants: Glenn Rempe, Junio C Hamano.
Thread: https://gitlist.dev/t/10000

## Glenn Rempe, 2007-09-24 20:34

Subject: [PATCH] Add ability to specify SMTP server port when using git-send-email.
Message-ID: <1190666058-10969-1-git-send-email-glenn@rempe.us>
URL: https://gitlist.dev/e/1190666058-10969-1-git-send-email-glenn%40rempe.us

```
Add ability to specify custom SMTP server port using
smtpserverport config value or --smtp-server-port command
line option.

Will default to port 25 if smtpssl config is set
to false or --smtp-ssl command line is unset.

Will default to port 465 if smtpssl config is set
to true or --smtp-ssl is set.

User can specify any arbitrary port number for standard or
SSL connections.

Users should be aware that sending auth info over non-ssl
connections may be unsafe.

Signed-off-by: Glenn Rempe <glenn@rempe.us>
---
 git-send-email.perl |   48 +++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 37 insertions(+), 11 deletions(-)

diff --git a/git-send-email.perl b/git-send-email.perl
index 4031e86..7c9c302 100755
--- a/git-send-email.perl
+++ b/git-send-email.perl
@@ -79,6 +79,10 @@ Options:
    --smtp-server  If set, specifies the outgoing SMTP server to use.
                   Defaults to localhost.
 
+   --smtp-server-port If set, specifies the port on the outgoing SMTP
+   server to use. Defaults to port 25 unless --smtp-ssl is set in
+   which case it will default to port 465.
+
    --smtp-user    The username for SMTP-AUTH.
 
    --smtp-pass    The password for SMTP-AUTH.
@@ -172,7 +176,7 @@ my ($quiet, $dry_run) = (0, 0);
 
 # Variables with corresponding config settings
 my ($thread, $chain_reply_to, $suppress_from, $signed_off_cc, $cc_cmd);
-my ($smtp_server, $smtp_authuser, $smtp_authpass, $smtp_ssl);
+my ($smtp_server, $smtp_server_port, $smtp_authuser, $smtp_authpass, $smtp_ssl);
 my ($identity, $aliasfiletype, @alias_files);
 
 my %config_bool_settings = (
@@ -185,6 +189,7 @@ my %config_bool_settings = (
 
 my %config_settings = (
     "smtpserver" => \$smtp_server,
+    "smtpserverport" => \$smtp_server_port,
     "smtpuser" => \$smtp_authuser,
     "smtppass" => \$smtp_authpass,
     "cccmd" => \$cc_cmd,
@@ -204,6 +209,7 @@ my $rc = GetOptions("sender|from=s" => \$sender,
 		    "bcc=s" => \@bcclist,
 		    "chain-reply-to!" => \$chain_reply_to,
 		    "smtp-server=s" => \$smtp_server,
+		    "smtp-server-port=s" => \$smtp_server_port,
 		    "smtp-user=s" => \$smtp_authuser,
 		    "smtp-pass=s" => \$smtp_authpass,
 		    "smtp-ssl!" => \$smtp_ssl,
@@ -375,6 +381,14 @@ if (!defined $smtp_server) {
 	$smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug*
 }
 
+if (!defined $smtp_server_port) {
+  if ($smtp_ssl) {
+    $smtp_server_port = 465  # SSL port
+  } else {
+    $smtp_server_port = 25  # Non-SSL port
+  }
+}
+
 if ($compose) {
 	# Note that this does not need to be secure, but we will make a small
 	# effort to have it be unique
@@ -604,20 +618,32 @@ X-Mailer: git-send-email $gitversion
 	} else {
 		if ($smtp_ssl) {
 			require Net::SMTP::SSL;
-			$smtp ||= Net::SMTP::SSL->new( $smtp_server, Port => 465 );
+			$smtp ||= Net::SMTP::SSL->new( $smtp_server, Port => $smtp_server_port );
 		}
 		else {
 			require Net::SMTP;
-			$smtp ||= Net::SMTP->new( $smtp_server );
+			$smtp ||= Net::SMTP->new( $smtp_server . ":" . $smtp_server_port );
 		}
-		$smtp->auth( $smtp_authuser, $smtp_authpass )
-			or die $smtp->message if (defined $smtp_authuser);
-		$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->ok or die "Failed to send $subject\n".$smtp->message;
+    
+    # we'll get an ugly error if $smtp was undefined above.
+    # If so we'll catch it and present something friendlier.
+    if ($smtp) {
+
+      if ((defined $smtp_authuser) && (defined $smtp_authpass)) {
+        $smtp->auth( $smtp_authuser, $smtp_authpass ) or die $smtp->message;
+      }
+
+      $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->ok or die "Failed to send $subject\n".$smtp->message;
+      
+    } else {
+      die "Unable to initialize SMTP properly.  Is there something wrong with your config?";
+    }
+
 	}
 	if ($quiet) {
 		printf (($dry_run ? "Dry-" : "")."Sent %s\n", $subject);
-- 
1.5.3.2.83.gc6869

```

## Junio C Hamano, 2007-09-25 06:24

Subject: Re: [PATCH] Add ability to specify SMTP server port when using git-send-email.
Message-ID: <7v8x6v6z4u.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7v8x6v6z4u.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <1190666058-10969-1-git-send-email-glenn@rempe.us>

```
Glenn Rempe <glenn@rempe.us> writes:

>  git-send-email.perl |   48 +++++++++++++++++++++++++++++++++++++-----------
>  1 files changed, 37 insertions(+), 11 deletions(-)
>
> diff --git a/git-send-email.perl b/git-send-email.perl
> index 4031e86..7c9c302 100755
> --- a/git-send-email.perl
> +++ b/git-send-email.perl
> @@ -79,6 +79,10 @@ Options:
>     --smtp-server  If set, specifies the outgoing SMTP server to use.
>                    Defaults to localhost.
>  
> +   --smtp-server-port If set, specifies the port on the outgoing SMTP
> +   server to use. Defaults to port 25 unless --smtp-ssl is set in
> +   which case it will default to port 465.
> +

This paragraph look inconsistent with different indentation
for second and subsequent lines.

> @@ -375,6 +381,14 @@ if (!defined $smtp_server) {
>  	$smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug*
>  }
>  
> +if (!defined $smtp_server_port) {
> +  if ($smtp_ssl) {
> +    $smtp_server_port = 465  # SSL port
> +  } else {
> +    $smtp_server_port = 25  # Non-SSL port
> +  }
> +}
> +
>  if ($compose) {
>  	# Note that this does not need to be secure, but we will make a small
>  	# effort to have it be unique
> @@ -604,20 +618,32 @@ X-Mailer: git-send-email $gitversion
>  	} else {
>  		if ($smtp_ssl) {
>  			require Net::SMTP::SSL;
> -			$smtp ||= Net::SMTP::SSL->new( $smtp_server, Port => 465 );
> +			$smtp ||= Net::SMTP::SSL->new( $smtp_server, Port => $smtp_server_port );
>  		}
>  		else {
>  			require Net::SMTP;
> -			$smtp ||= Net::SMTP->new( $smtp_server );
> +			$smtp ||= Net::SMTP->new( $smtp_server . ":" . $smtp_server_port );
>  		}

This change suggests that, although undocumented, existing users
could have already been using

	--smtp-server=smtp.myisp.com:26

to specify a nonstandard port, and this patch, while bringing in
the support for a nonstandard port as an official feature, would
break such a setup.  I wonder how real the issue is, and if we
can work it around easily.  For example,

 (1) drop the "default to 25 for smtp if undefined" part we saw
     earlier;

 (2) redo this part as

	if ($smtp_ssl) {
		... as you have it ...
	} else {
		require Net::SMTP;
                $smtp ||= Net::SMTP->new((defined $smtp_server_port) ?
					 "$smtp_server:$smtp_server_port" :
                                         $smtp_server);
	}

> -		$smtp->auth( $smtp_authuser, $smtp_authpass )
> -			or die $smtp->message if (defined $smtp_authuser);
> ...
> +    
> +    # we'll get an ugly error if $smtp was undefined above.
> +    # If so we'll catch it and present something friendlier.
> +    if ($smtp) {
> +
> +      if ((defined $smtp_authuser) && (defined $smtp_authpass)) {
> +        $smtp->auth( $smtp_authuser, $smtp_authpass ) or die $smtp->message;
> +      }
> +    ...
> +    } else {
> +      die "Unable to initialize SMTP properly.  Is there something wrong with your config?";
> +    }


I'd prefer the check done the opposite order, like

	if (!$smtp) {
        	die ...
	}

without an else clause.

```
