send-email: --batch-size to work around some SMTP server limit

Some email servers (e.g. smtp.163.com) limit the number emails to be sent per session (connection) and this will lead to a faliure when sending many messages. Teach send-email to disconnect after sending a number of messages (configurable via the --batch-size=<num> option), wait for a few seconds (configurable via the --relogin-delay=<seconds> option) and reconnect, to work around such a limit. Also add two configuration variables to give these options the default. Note: We will use this as a band-aid for now, but in the longer term, we should look at and react to the SMTP error code from the server; Xianqiang reports that 450 and 451 are returned by problematic servers. cf. https://public-inbox.org/git/7993e188.d18d.15c3560bcaf.Coremail.zxq_yx_007@163.com/ Signed-off-by: xiaoqiang zhao <zxq_yx_007@163.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

xiaoqiang zhao committed May 21, 2017 at 20:59 UTC 5453b83bdf92efbfe9e9aaa54d4c62ce48ced2f5
4 files changed +45
Documentation/config.txt
+10
@@ -2914,6 +2914,16 @@ sendemail.xmailer::
2914 sendemail.signedoffcc (deprecated)::
2915 Deprecated alias for `sendemail.signedoffbycc`.
2916
2917 +sendemail.smtpBatchSize::
2918 + Number of messages to be sent per connection, after that a relogin
2919 + will happen. If the value is 0 or undefined, send all messages in
2920 + one connection.
2921 + See also the `--batch-size` option of linkgit:git-send-email[1].
2922 +
2923 +sendemail.smtpReloginDelay::
2924 + Seconds wait before reconnecting to smtp server.
2925 + See also the `--relogin-delay` option of linkgit:git-send-email[1].
2926 +
2927 showbranch.default::
2928 The default set of branches for linkgit:git-show-branch[1].
2929 See linkgit:git-show-branch[1].
Documentation/git-send-email.txt
+15
@@ -248,6 +248,21 @@ must be used for each option.
248 commands and replies will be printed. Useful to debug TLS
249 connection and authentication problems.
250
251 +--batch-size=<num>::
252 + Some email servers (e.g. smtp.163.com) limit the number emails to be
253 + sent per session (connection) and this will lead to a faliure when
254 + sending many messages. With this option, send-email will disconnect after
255 + sending $<num> messages and wait for a few seconds (see --relogin-delay)
256 + and reconnect, to work around such a limit. You may want to
257 + use some form of credential helper to avoid having to retype
258 + your password every time this happens. Defaults to the
259 + `sendemail.smtpBatchSize` configuration variable.
260 +
261 +--relogin-delay=<int>::
262 + Waiting $<int> seconds before reconnecting to SMTP server. Used together
263 + with --batch-size option. Defaults to the `sendemail.smtpReloginDelay`
264 + configuration variable.
265 +
266 Automating
267 ~~~~~~~~~~
268
contrib/completion/git-completion.bash
+2
@@ -2608,6 +2608,8 @@ _git_config ()
2608 sendemail.thread
2609 sendemail.to
2610 sendemail.validate
2611 + sendemail.smtpbatchsize
2612 + sendemail.smtprelogindelay
2613 showbranch.default
2614 status.relativePaths
2615 status.showUntrackedFiles
git-send-email.perl
+18
@@ -81,6 +81,10 @@ git send-email --dump-aliases
81 This setting forces to use one of the listed mechanisms.
82 --smtp-debug <0|1> * Disable, enable Net::SMTP debug.
83
84 + --batch-size <int> * send max <int> message per connection.
85 + --relogin-delay <int> * delay <int> seconds between two successive login.
86 + This option can only be used with --batch-size
87 +
88 Automating:
89 --identity <str> * Use the sendemail.<id> options.
90 --to-cmd <str> * Email To: via `<str> \$patch_path`
@@ -153,6 +157,7 @@ my $have_email_valid = eval { require Email::Valid; 1 };
157 my $have_mail_address = eval { require Mail::Address; 1 };
158 my $smtp;
159 my $auth;
160 +my $num_sent = 0;
161
162 # Regexes for RFC 2047 productions.
163 my $re_token = qr/[^][()<>@,;:\\"\/?.= \000-\037\177-\377]+/;
@@ -216,6 +221,7 @@ my ($cover_cc, $cover_to);
221 my ($to_cmd, $cc_cmd);
222 my ($smtp_server, $smtp_server_port, @smtp_server_options);
223 my ($smtp_authuser, $smtp_encryption, $smtp_ssl_cert_path);
224 +my ($batch_size, $relogin_delay);
225 my ($identity, $aliasfiletype, @alias_files, $smtp_domain, $smtp_auth);
226 my ($validate, $confirm);
227 my (@suppress_cc);
@@ -247,6 +253,8 @@ my %config_settings = (
253 "smtppass" => \$smtp_authpass,
254 "smtpdomain" => \$smtp_domain,
255 "smtpauth" => \$smtp_auth,
256 + "smtpbatchsize" => \$batch_size,
257 + "smtprelogindelay" => \$relogin_delay,
258 "to" => \@initial_to,
259 "tocmd" => \$to_cmd,
260 "cc" => \@initial_cc,
@@ -358,6 +366,8 @@ $rc = GetOptions(
366 "force" => \$force,
367 "xmailer!" => \$use_xmailer,
368 "no-xmailer" => sub {$use_xmailer = 0},
369 + "batch-size=i" => \$batch_size,
370 + "relogin-delay=i" => \$relogin_delay,
371 );
372
373 usage() if $help;
@@ -1664,6 +1674,14 @@ foreach my $t (@files) {
1674 }
1675 }
1676 $message_id = undef;
1677 + $num_sent++;
1678 + if (defined $batch_size && $num_sent == $batch_size) {
1679 + $num_sent = 0;
1680 + $smtp->quit if defined $smtp;
1681 + undef $smtp;
1682 + undef $auth;
1683 + sleep($relogin_delay) if defined $relogin_delay;
1684 + }
1685 }
1686
1687 # Execute a command (e.g. $to_cmd) to get a list of email addresses