From: Glenn Rempe <glenn@rempe.us>
To: git@vger.kernel.org
Cc: Glenn Rempe <glenn@rempe.us>
Subject: [PATCH] Add ability to specify SMTP server port when using git-send-email.
Date: Tue, 25 Sep 2007 15:38:47 -0700 [thread overview]
Message-ID: <1190759927-19493-1-git-send-email-glenn@rempe.us> (raw)
Add ability to specify custom SMTP server port using
smtpserverport config value or --smtp-server-port command
line option.
Ability to specify custom SMTP server port using
--smtp-server host:port syntax.
Will default to port 25 if smtpssl config is set
to false or --smtp-ssl command line is unset and port
is not explicitly defined.
Will default to port 465 if smtpssl config is set
to true or --smtp-ssl is set and port is not explicity
defined.
Users should be aware that sending auth info over non-ssl
connections may be unsafe or just may not work at all
depending on SMTP server config.
Added some negative test cases.
Signed-off-by: Glenn Rempe <glenn@rempe.us>
---
git-send-email.perl | 73 ++++++++++++++++++++++++++++++++++++++++---------
t/t9001-send-email.sh | 12 ++++++++
2 files changed, 72 insertions(+), 13 deletions(-)
diff --git a/git-send-email.perl b/git-send-email.perl
index 4031e86..969cb39 100755
--- a/git-send-email.perl
+++ b/git-send-email.perl
@@ -77,7 +77,10 @@ Options:
the default section.
--smtp-server If set, specifies the outgoing SMTP server to use.
- Defaults to localhost.
+ Defaults to localhost. Port number can be specified here with
+ hostname:port format or by using --smtp-server-port option.
+
+ --smtp-server-port Specify a port on the outgoing SMTP server to connect to.
--smtp-user The username for SMTP-AUTH.
@@ -172,8 +175,8 @@ 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 ($identity, $aliasfiletype, @alias_files);
+my ($smtp_server, $smtp_server_port, $smtp_authuser, $smtp_authpass, $smtp_ssl);
+my ($identity, $aliasfiletype, @alias_files, @smtp_host_parts);
my %config_bool_settings = (
"thread" => [\$thread, 1],
@@ -185,6 +188,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 +208,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 +380,29 @@ if (!defined $smtp_server) {
$smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug*
}
+# don't allow BOTH forms of port definition to work since we can't guess which one is right.
+if (($smtp_server =~ /:\d+/) && (defined $smtp_server_port)) {
+ die "You must specify the port using either hostname:port OR --smtp-server-port but not both!"
+}
+
+# setup smtp_server var if it was passed in as host:port format
+if ( $smtp_server =~ /:\d+/) {
+ # if they do pass a host:port form then split it and use the parts
+ @smtp_host_parts = split(/:/, $smtp_server);
+ $smtp_server = $smtp_host_parts[0];
+ $smtp_server_port = $smtp_host_parts[1];
+}
+
+# setup reasonable defaults if neither host:port or --smtp-server-port were passed
+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
@@ -602,22 +630,41 @@ X-Mailer: git-send-email $gitversion
print $sm "$header\n$message";
close $sm or die $?;
} else {
+
+ if (!defined $smtp_server) {
+ die "The required SMTP server is not properly defined."
+ }
+
+ if (!defined $smtp_server_port || !$smtp_server_port =~ /^\d+$/ ) {
+ die "The required SMTP server port is not properly defined."
+ }
+
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) {
+ die "Unable to initialize SMTP properly. Is there something wrong with your config?";
+ }
+
+ 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;
+
}
if ($quiet) {
printf (($dry_run ? "Dry-" : "")."Sent %s\n", $subject);
diff --git a/t/t9001-send-email.sh b/t/t9001-send-email.sh
index 83f9470..d32907d 100755
--- a/t/t9001-send-email.sh
+++ b/t/t9001-send-email.sh
@@ -41,4 +41,16 @@ test_expect_success \
'Verify commandline' \
'diff commandline expected'
+test_expect_failure 'Passing in both host:port form AND --smtp-server-port' '
+ git send-email --from="Example <nobody@example.com>" --to=nobody@example.com --smtp-server smtp.foo.com:66 --smtp-server-port 77" $patches 2>errors
+'
+
+test_expect_failure 'Passing in non-numeric server port with host:port form' '
+ git send-email --from="Example <nobody@example.com>" --to=nobody@example.com --smtp-server smtp.foo.com:bar" $patches 2>errors
+'
+
+test_expect_failure 'Passing in non-numeric server port with --smtp-server-port form' '
+ git send-email --from="Example <nobody@example.com>" --to=nobody@example.com --smtp-server smtp.foo.com --smtp-server-port bar" $patches 2>errors
+'
+
test_done
--
1.5.3.2.105.g23fe
next reply other threads:[~2007-09-25 22:39 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-09-25 22:38 Glenn Rempe [this message]
2007-09-25 23:05 ` [PATCH] Add ability to specify SMTP server port when using git-send-email Johannes Schindelin
2007-09-25 23:12 ` Junio C Hamano
2007-09-25 23:25 ` Junio C Hamano
2007-09-26 0:23 ` Glenn Rempe
2007-09-26 0:27 ` Junio C Hamano
2007-09-26 1:15 ` Glenn Rempe
2007-09-26 1:42 ` Johannes Schindelin
2007-09-26 7:18 ` Andreas Ericsson
2007-09-26 10:31 ` Johannes Schindelin
-- strict thread matches above, loose matches on Subject: below --
2007-09-24 20:34 Glenn Rempe
2007-09-25 6:24 ` Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1190759927-19493-1-git-send-email-glenn@rempe.us \
--to=glenn@rempe.us \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).