{"thread":{"id":"45895","subject":"[PATCH v3] send-email: --batch-size to work around some SMTP server limit","startedAt":"2017-05-07T22:08:43Z","lastAt":"2017-05-08T11:25:12Z","messageCount":3,"participants":["xiaoqiang zhao","Junio C Hamano","赵小强"],"isPatch":true,"patchVersion":3,"patchTotal":null},"messages":[{"id":"319014","messageId":"20170507043648.24143-1-zxq_yx_007@163.com","threadId":"45895","inReplyTo":null,"subject":"[PATCH v3] send-email: --batch-size to work around some SMTP server limit","fromName":"xiaoqiang zhao","fromEmail":"zxq_yx_007@163.com","sentAt":"2017-05-07T04:36:48Z","receivedAt":"2017-05-07T22:08:43Z","isPatch":true,"sender":{"key":"zxq_yx_007@163.com","avatar":"https://avatars.githubusercontent.com/u/3981296?v=4"},"body":"Some email servers (e.g. smtp.163.com) limit the number emails to be\nsent per session(connection) and this will lead to a faliure when\nsending many messages.\n\nTeach send-email to disconnect after sending a number of messages\n(configurable via the --batch-size=<num> option), wait for a few\nseconds (configurable via the --relogin-delay=<seconds> option) and\nreconnect, to work around such a limit.\n\nAlso add this two configure option for git config command.\n\nSigned-off-by: xiaoqiang zhao <zxq_yx_007@163.com>\n---\n contrib/completion/git-completion.bash |  2 ++\n git-send-email.perl                    | 18 ++++++++++++++++++\n 2 files changed, 20 insertions(+)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex af658995d..29496353a 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -2608,6 +2608,8 @@ _git_config ()\n \t\tsendemail.thread\n \t\tsendemail.to\n \t\tsendemail.validate\n+\t\tsendemail.smtpbatchsize\n+\t\tsendemail.smtprelogindelay\n \t\tshowbranch.default\n \t\tstatus.relativePaths\n \t\tstatus.showUntrackedFiles\ndiff --git a/git-send-email.perl b/git-send-email.perl\nindex eea0a517f..5cbe97898 100755\n--- a/git-send-email.perl\n+++ b/git-send-email.perl\n@@ -81,6 +81,10 @@ git send-email --dump-aliases\n                                      This setting forces to use one of the listed mechanisms.\n     --smtp-debug            <0|1>  * Disable, enable Net::SMTP debug.\n \n+    --batch-size            <int>  * send max <int> message per connection.\n+    --relogin-delay         <int>  * delay <int> seconds between two successive login, default to 1,\n+                                     This option can only be used with --batch-size\n+\n   Automating:\n     --identity              <str>  * Use the sendemail.<id> options.\n     --to-cmd                <str>  * Email To: via `<str> \\$patch_path`\n@@ -153,6 +157,7 @@ my $have_email_valid = eval { require Email::Valid; 1 };\n my $have_mail_address = eval { require Mail::Address; 1 };\n my $smtp;\n my $auth;\n+my $num_sent = 0;\n \n # Regexes for RFC 2047 productions.\n my $re_token = qr/[^][()<>@,;:\\\\\"\\/?.= \\000-\\037\\177-\\377]+/;\n@@ -216,6 +221,7 @@ my ($cover_cc, $cover_to);\n my ($to_cmd, $cc_cmd);\n my ($smtp_server, $smtp_server_port, @smtp_server_options);\n my ($smtp_authuser, $smtp_encryption, $smtp_ssl_cert_path);\n+my ($batch_size, $relogin_delay) = (0, 0);\n my ($identity, $aliasfiletype, @alias_files, $smtp_domain, $smtp_auth);\n my ($validate, $confirm);\n my (@suppress_cc);\n@@ -247,6 +253,8 @@ my %config_settings = (\n     \"smtppass\" => \\$smtp_authpass,\n     \"smtpdomain\" => \\$smtp_domain,\n     \"smtpauth\" => \\$smtp_auth,\n+    \"smtpbatchsize\" => \\$batch_size,\n+    \"smtprelogindelay\" => \\$relogin_delay,\n     \"to\" => \\@initial_to,\n     \"tocmd\" => \\$to_cmd,\n     \"cc\" => \\@initial_cc,\n@@ -358,6 +366,8 @@ $rc = GetOptions(\n \t\t    \"force\" => \\$force,\n \t\t    \"xmailer!\" => \\$use_xmailer,\n \t\t    \"no-xmailer\" => sub {$use_xmailer = 0},\n+\t\t    \"batch-size=i\" => \\$batch_size,\n+\t\t    \"relogin-delay=i\" => \\$relogin_delay,\n \t );\n \n usage() if $help;\n@@ -1664,6 +1674,14 @@ foreach my $t (@files) {\n \t\t}\n \t}\n \t$message_id = undef;\n+\t$num_sent++;\n+\tif ($num_sent == $batch_size) {\n+\t\t$num_sent = 0;\n+\t\t$smtp->quit;\n+\t\t$smtp = undef;\n+\t\t$auth = 0;\n+\t\tsleep($relogin_delay);\n+\t}\n }\n \n # Execute a command (e.g. $to_cmd) to get a list of email addresses\n-- \n2.13.0.rc2.1.gd46ef338b.dirty\n\n\n"},{"id":"319044","messageId":"xmqqpofk9edz.fsf@gitster.mtv.corp.google.com","threadId":"45895","inReplyTo":"20170507043648.24143-1-zxq_yx_007@163.com","subject":"Re: [PATCH v3] send-email: --batch-size to work around some SMTP server limit","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2017-05-08T04:11:20Z","receivedAt":"2017-05-08T04:11:28Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"xiaoqiang zhao <zxq_yx_007@163.com> writes:\n\n> @@ -1664,6 +1674,14 @@ foreach my $t (@files) {\n>  \t\t}\n>  \t}\n>  \t$message_id = undef;\n> +\t$num_sent++;\n> +\tif ($num_sent == $batch_size) {\n> +\t\t$num_sent = 0;\n> +\t\t$smtp->quit;\n> +\t\t$smtp = undef;\n> +\t\t$auth = 0;\n\nTwo suggestions.\n\n (1) I do not think $smtp is always valid when we come here; it is\n     unsafe to unconditionally say $smtp->quit like this patch does.\n\n\t$smtp->quit if defined $smtp;\n\n     may help codepaths like $dry_run and also the case where\n     $smtp_server is the absolute path to a local program.\n\n (2) You are setting $auth to zero to force re-authentication to\n     happen.  It would be more consistent to the state of $auth that\n     is not-yet-used to \"undef $auth;\" here instead.  After all, the\n     variable starts its life in an undefined state.\n\n\nSo all in all\n\n\t$smtp->quit if defined $smtp;\n\tundef $smtp;\n\tundef $auth;\n\nperhaps?\n\nThis change of course forces re-authentication every N messages,\nwhich may not hurt those who use some form of credential helper, but\nthat may be something we want to mention in the log message.\n\n> +\t\tsleep($relogin_delay);\n> +\t}\n>  }\n\nThanks.\n"},{"id":"319079","messageId":"E359FC85-97FC-48BF-B93C-B01CA32F70A8@163.com","threadId":"45895","inReplyTo":"xmqqpofk9edz.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH v3] send-email: --batch-size to work around some SMTP server limit","fromName":"赵小强","fromEmail":"zxq_yx_007@163.com","sentAt":"2017-05-08T11:23:46Z","receivedAt":"2017-05-08T11:25:12Z","isPatch":true,"sender":{"key":"zxq_yx_007@163.com","avatar":"https://avatars.githubusercontent.com/u/3981296?v=4"},"body":"\n\n> 在 2017年5月8日，12:11，Junio C Hamano <gitster@pobox.com> 写道：\n> \n> Two suggestions.\n> \n> (1) I do not think $smtp is always valid when we come here; it is\n>     unsafe to unconditionally say $smtp->quit like this patch does.\n> \n>    $smtp->quit if defined $smtp;\n> \n>     may help codepaths like $dry_run and also the case where\n>     $smtp_server is the absolute path to a local program.\n> \n\nHmm，missed this code path.\n\n> (2) You are setting $auth to zero to force re-authentication to\n>     happen.  It would be more consistent to the state of $auth that\n>     is not-yet-used to \"undef $auth;\" here instead.  After all, the\n>     variable starts its life in an undefined state.\n> \n> \n> So all in all\n> \n>    $smtp->quit if defined $smtp;\n>    undef $smtp;\n>    undef $auth;\n> \n> perhaps?\n> \n> This change of course forces re-authentication every N messages,\n> which may not hurt those who use some form of credential helper, but\n> that may be something we want to mention in the log message.\n\nYes, it' s better to undef $auth here. I will update the commit message next version.\n\nThank you very much for your helpful suggestions ！\n\n> \n>> +        sleep($relogin_delay);\n>> +    }\n>> }\n> \n> Thanks.\n\n\n"}]}