Skip to content

Commit 5453b83

Browse files
hitmoongitster
authored andcommitted
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>
1 parent b06d364 commit 5453b83

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

Documentation/config.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2914,6 +2914,16 @@ sendemail.xmailer::
29142914
sendemail.signedoffcc (deprecated)::
29152915
Deprecated alias for `sendemail.signedoffbycc`.
29162916

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+
29172927
showbranch.default::
29182928
The default set of branches for linkgit:git-show-branch[1].
29192929
See linkgit:git-show-branch[1].

Documentation/git-send-email.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,21 @@ must be used for each option.
248248
commands and replies will be printed. Useful to debug TLS
249249
connection and authentication problems.
250250

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+
251266
Automating
252267
~~~~~~~~~~
253268

contrib/completion/git-completion.bash

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2608,6 +2608,8 @@ _git_config ()
26082608
sendemail.thread
26092609
sendemail.to
26102610
sendemail.validate
2611+
sendemail.smtpbatchsize
2612+
sendemail.smtprelogindelay
26112613
showbranch.default
26122614
status.relativePaths
26132615
status.showUntrackedFiles

git-send-email.perl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ sub usage {
8181
This setting forces to use one of the listed mechanisms.
8282
--smtp-debug <0|1> * Disable, enable Net::SMTP debug.
8383
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+
8488
Automating:
8589
--identity <str> * Use the sendemail.<id> options.
8690
--to-cmd <str> * Email To: via `<str> \$patch_path`
@@ -153,6 +157,7 @@ sub format_2822_time {
153157
my $have_mail_address = eval { require Mail::Address; 1 };
154158
my $smtp;
155159
my $auth;
160+
my $num_sent = 0;
156161

157162
# Regexes for RFC 2047 productions.
158163
my $re_token = qr/[^][()<>@,;:\\"\/?.= \000-\037\177-\377]+/;
@@ -216,6 +221,7 @@ sub do_edit {
216221
my ($to_cmd, $cc_cmd);
217222
my ($smtp_server, $smtp_server_port, @smtp_server_options);
218223
my ($smtp_authuser, $smtp_encryption, $smtp_ssl_cert_path);
224+
my ($batch_size, $relogin_delay);
219225
my ($identity, $aliasfiletype, @alias_files, $smtp_domain, $smtp_auth);
220226
my ($validate, $confirm);
221227
my (@suppress_cc);
@@ -247,6 +253,8 @@ sub do_edit {
247253
"smtppass" => \$smtp_authpass,
248254
"smtpdomain" => \$smtp_domain,
249255
"smtpauth" => \$smtp_auth,
256+
"smtpbatchsize" => \$batch_size,
257+
"smtprelogindelay" => \$relogin_delay,
250258
"to" => \@initial_to,
251259
"tocmd" => \$to_cmd,
252260
"cc" => \@initial_cc,
@@ -358,6 +366,8 @@ sub signal_handler {
358366
"force" => \$force,
359367
"xmailer!" => \$use_xmailer,
360368
"no-xmailer" => sub {$use_xmailer = 0},
369+
"batch-size=i" => \$batch_size,
370+
"relogin-delay=i" => \$relogin_delay,
361371
);
362372

363373
usage() if $help;
@@ -1664,6 +1674,14 @@ sub send_message {
16641674
}
16651675
}
16661676
$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+
}
16671685
}
16681686

16691687
# Execute a command (e.g. $to_cmd) to get a list of email addresses

0 commit comments

Comments
 (0)