From 89a1d3bc9682b947dbd20a93588a111d4fcd02cd Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Sat, 14 Mar 2020 08:00:29 +0100 Subject: Update localmail with advice from tejr --- .local/bin/localmail | 50 +++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 25 deletions(-) (limited to '.local') diff --git a/.local/bin/localmail b/.local/bin/localmail index eea4511..428e0fc 100755 --- a/.local/bin/localmail +++ b/.local/bin/localmail @@ -7,15 +7,14 @@ use utf8; use warnings; use Data::UUID; +use Email::Address; use Email::Simple; +use File::Basename; use File::Copy; +use File::Path; use File::Slurp; use Net::IMAP::Simple; use Net::SMTP; -use File::Path; -use File::Basename; - -use Data::Dumper; use App::Localmail; @@ -56,6 +55,7 @@ sub fetch ($config, @args) { my $connection = Net::IMAP::Simple->new( $current->{server}, use_ssl => $current->{ssl} // 1, + Timeout => $current->{timeout} // 5, ); if (!$connection) { @@ -68,20 +68,20 @@ sub fetch ($config, @args) { $connection->starttls; } - say " Authenticating"; + say ' Authenticating'; if (!$connection->login($current->{username}, $current->{password}{plain})) { say ' Failed: '.$connection->errstr."\n"; next; } - say " Fetching mailboxes"; + say ' Fetching mailboxes'; for my $mbox ($connection->mailboxes) { $connection->select($mbox); my @messages = keys(%{$connection->list}); my $count = 0; - say " $mbox (".($#messages + 1).")"; + say " $mbox (".(scalar @messages).")"; for my $message (@messages) { $count++; @@ -89,9 +89,9 @@ sub fetch ($config, @args) { say " $current_uuid ($count)"; - open(FH, '>', "$maildir/tmp/$current_uuid.eml") or die $!; - print FH $connection->fetch($message); - close(FH); + open my $fh, '>', "$maildir/tmp/$current_uuid.eml" or die $!; + print $fh $connection->fetch($message); + close $fh; move("$maildir/tmp/$current_uuid.eml", "$maildir/new/$current_uuid.eml"); $connection->delete($message); @@ -100,7 +100,7 @@ sub fetch ($config, @args) { $connection->close; } - say " Closing connection"; + say ' Closing connection'; $connection->quit; } @@ -134,15 +134,15 @@ sub send ($config, @args) { my $body = File::Slurp::read_file("$item/message.eml"); my $email = Email::Simple->new($body); - my $from = $email->header("From"); + my ($from) = Email::Address->parse($email->header('From')); my @recipients = File::Slurp::read_file("$item/recipients"); - if (!exists $senders{$from}) { + if (!$from || !exists $senders{$from->address}) { say " No account handles mail from '$from'"; next; } - my $current = $config->{accounts}{$senders{$from}}{outgoing}; + my $current = $config->{accounts}{$senders{$from->address}}{outgoing}; my $port = $current->{port} // ($current->{ssl} ? 465 : 25); say " Connecting to ".$current->{server}.':'.$port; @@ -162,29 +162,29 @@ sub send ($config, @args) { } if ($current->{starttls}) { - say " StartTLS"; + say ' StartTLS'; $connection->starttls(); } - say " Authenticating"; + say ' Authenticating'; if (!$connection->auth($current->{username}, $current->{password}{plain})) { say " Failed: ".$connection->message; next; } - say " MAIL FROM"; - if (!$connection->mail($from)) { + say ' MAIL FROM'; + if (!$connection->mail($email->header('From'))) { say " Failed: ".$connection->message; next; } - say " RCPT TO"; + say ' RCPT TO'; if (!$connection->recipient(@recipients)) { say " Failed: ".$connection->message; next; } - say " DATA"; + say ' DATA'; if (!$connection->data($body)) { say " Failed: ".$connection->message; next; @@ -192,13 +192,13 @@ sub send ($config, @args) { $connection->quit; - say " Sent!"; + say ' Sent!'; - if (open(FH, '>', "$maildir/sent/$id.eml")) { - say " Moving to sentdir"; + if (open my $fh, '>', "$maildir/sent/$id.eml") { + say ' Moving to sentdir'; - print FH $email->as_string; - close(FH); + print $fh $email->as_string; + close $fh; File::Path::rmtree($item); } -- cgit v1.1