@matheusbsbs / matheus-bagliano / commits / 4bc87a28be
Fork 0

send-email: Change from Mail::Sendmail to Net::SMTP

Net::SMTP is in the base Perl distribution, so users are more likely to have it. Net::SMTP also allows reusing the SMTP connection, so sending multiple emails is faster. [jc: tweaked X-Mailer further while we are at it.] Signed-off-by: Eric Wong <normalperson@yhbt.net> Signed-off-by: Junio C Hamano <junkio@cox.net>

Eric Wong committed Mar 25, 2006 at 17:20 UTC 4bc87a28be020a6bf7387161c65ea3d8e4a0228b
1 file changed +40 -24
git-send-email.perl
+40 -24
@@ -19,11 +19,17 @@
19 use strict;
20 use warnings;
21 use Term::ReadLine;
22 -use Mail::Sendmail qw(sendmail %mailcfg);
22 use Getopt::Long;
23 use Data::Dumper;
24 +use Net::SMTP;
25 use Email::Valid;
26
27 +# most mail servers generate the Date: header, but not all...
28 +$ENV{LC_ALL} = 'C';
29 +use POSIX qw/strftime/;
30 +
31 +my $smtp;
32 +
33 sub unique_email_list(@);
34 sub cleanup_compose_files();
35
@@ -270,35 +276,45 @@ $cc = "";
276
277 sub send_message
278 {
273 - my $to = join (", ", unique_email_list(@to));
274 -
275 - %mail = ( To => $to,
276 - From => $from,
277 - CC => $cc,
278 - Subject => $subject,
279 - Message => $message,
280 - 'Reply-to' => $from,
281 - 'In-Reply-To' => $reply_to,
282 - 'Message-ID' => $message_id,
283 - 'X-Mailer' => "git-send-email",
284 - );
285 -
286 - $mail{smtp} = $smtp_server;
287 - $mailcfg{mime} = 0;
288 -
289 - #print Data::Dumper->Dump([\%mail],[qw(*mail)]);
290 -
291 - sendmail(%mail) or die $Mail::Sendmail::error;
279 + my @recipients = unique_email_list(@to);
280 + my $to = join (",\n\t", @recipients);
281 + @recipients = unique_email_list(@recipients,@cc);
282 + my $date = strftime('%a, %d %b %Y %H:%M:%S %z', localtime(time));
283 +
284 + my $header = "From: $from
285 +To: $to
286 +Cc: $cc
287 +Subject: $subject
288 +Reply-To: $from
289 +Date: $date
290 +Message-Id: $message_id
291 +X-Mailer: git-send-email @@GIT_VERSION@@
292 +";
293 + $header .= "In-Reply-To: $reply_to\n" if $reply_to;
294 +
295 + $smtp ||= Net::SMTP->new( $smtp_server );
296 + $smtp->mail( $from ) or die $smtp->message;
297 + $smtp->to( @recipients ) or die $smtp->message;
298 + $smtp->data or die $smtp->message;
299 + $smtp->datasend("$header\n$message") or die $smtp->message;
300 + $smtp->dataend() or die $smtp->message;
301 + $smtp->ok or die "Failed to send $subject\n".$smtp->message;
302
303 if ($quiet) {
304 printf "Sent %s\n", $subject;
305 } else {
296 - print "OK. Log says:\n", $Mail::Sendmail::log;
297 - print "\n\n"
306 + print "OK. Log says:
307 +Date: $date
308 +Server: $smtp_server Port: 25
309 +From: $from
310 +Subject: $subject
311 +Cc: $cc
312 +To: $to
313 +
314 +Result: ", $smtp->code, ' ', ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
315 }
316 }
317
301 -
318 $reply_to = $initial_reply_to;
319 make_message_id();
320 $subject = $initial_subject;
@@ -389,7 +405,7 @@ sub cleanup_compose_files() {
405
406 }
407
392 -
408 +$smtp->quit if $smtp;
409
410 sub unique_email_list(@) {
411 my %seen;