--- origtwiki/lib/TWiki/Net.pm Sun Jan 5 02:36:11 2003 +++ twiki/lib/TWiki/Net.pm Fri May 16 15:35:30 2003 @@ -29,12 +29,13 @@ use strict; use vars qw( - $useNetSmtp + $useNetSmtp $useMailTools $mailInitialized $mailHost $helloHost ); BEGIN { $useNetSmtp = 0; + $useMailTools = 0; $mailInitialized = 0; } @@ -118,7 +119,12 @@ if( $mailHost ) { eval { # May fail if Net::SMTP not installed $useNetSmtp = require Net::SMTP; - } + }; + eval { # Mail fail if MailTools is not installed + require Mail::Internet; + require Mail::Address; + }; + $useMailTools = ! $@; } } @@ -126,6 +132,23 @@ # Send the email. Use Net::SMTP if it's installed, otherwise use a # sendmail type program. if( $useNetSmtp ) { + my $from = ""; + my @to = (); + + if ($useMailTools) { + my @lines = map { $_ . "\n" } split(/\n/, $theText); + my $mail = new Mail::Internet(\@lines); + my $header = $mail->head(); + $from = $header->get('From'); + @to; + foreach my $field ('To', 'Cc', 'BCC') { + my @addr = Mail::Address->parse($header->get($field)); + push @to, map { $_->format() } @addr; + } + $header->delete('BCC'); + $theText = $mail->as_string(); + } else { + my ( $header, $body ) = split( "\n\n", $theText, 2 ); my @headerlines = split( /\n/, $header ); $header =~ s/\nBCC\:[^\n]*//os; #remove BCC line from header @@ -133,7 +156,6 @@ $theText = "$header\n\n$body"; # rebuild message # extract 'From:' - my $from = ""; my @arr = grep( /^From: /i, @headerlines ); if( scalar( @arr ) ) { $from = $arr[0]; @@ -144,34 +166,33 @@ } # extract @to from 'To:', 'CC:', 'BCC:' - my @to = (); @arr = grep( /^To: /i, @headerlines ); my $tmp = ""; if( scalar( @arr ) ) { $tmp = $arr[0]; $tmp =~ s/^To:\s*//io; - @arr = split( /[,\s]+/, $tmp ); + @arr = split( /\s*,\s*/, $tmp ); push( @to, @arr ); } @arr = grep( /^CC: /i, @headerlines ); if( scalar( @arr ) ) { $tmp = $arr[0]; $tmp =~ s/^CC:\s*//io; - @arr = split( /[,\s]+/, $tmp ); + @arr = split( /\s*,\s*/, $tmp ); push( @to, @arr ); } @arr = grep( /^BCC: /i, @headerlines ); if( scalar( @arr ) ) { $tmp = $arr[0]; $tmp =~ s/^BCC:\s*//io; - @arr = split( /[,\s]+/, $tmp ); + @arr = split( /\s*,\s*/, $tmp ); push( @to, @arr ); } if( ! ( scalar( @to ) ) ) { return "ERROR: Can't send mail, missing receipient"; } - + } $error = _sendEmailByNetSMTP( $from, \@to, $theText ); } else {