|
package SL::IMAPClient;
|
|
|
|
use strict;
|
|
use warnings;
|
|
use utf8;
|
|
|
|
use IO::Socket::INET;
|
|
use IO::Socket::SSL;
|
|
use Mail::IMAPClient;
|
|
use Email::MIME;
|
|
use File::MimeInfo::Magic;
|
|
use Encode qw(encode decode);
|
|
use Encode::IMAPUTF7;
|
|
use SL::Locale;
|
|
|
|
use SL::SessionFile;
|
|
use SL::Locale::String qw(t8);
|
|
use SL::DB::EmailImport;
|
|
use SL::DB::EmailJournal;
|
|
use SL::DB::EmailJournalAttachment;
|
|
|
|
use SL::DB::Order;
|
|
|
|
sub new {
|
|
my ($class, %params) = @_;
|
|
my $config = $::lx_office_conf{imap_client} || {};
|
|
my $server_locale = Locale->new($::lx_office_conf{server}->{language});
|
|
my %record_type_to_folder = (
|
|
sales_quotation => $server_locale->text('Sales Quotations'),
|
|
sales_order => $server_locale->text('Sales Orders'),
|
|
);
|
|
my %record_folder_to_type = reverse %record_type_to_folder;
|
|
my $self = bless {
|
|
enabled => $config->{enabled},
|
|
hostname => $config->{hostname},
|
|
port => $config->{port},
|
|
ssl => $config->{ssl},
|
|
username => $config->{username},
|
|
password => $config->{password},
|
|
base_folder => $config->{base_folder} || 'INBOX',
|
|
record_type_to_folder => \%record_type_to_folder,
|
|
record_folder_to_type => \%record_folder_to_type,
|
|
%params,
|
|
}, $class;
|
|
return unless $self->{enabled};
|
|
$self->_create_imap_client();
|
|
return $self;
|
|
}
|
|
|
|
sub DESTROY {
|
|
my ($self) = @_;
|
|
if ($self->{imap_client}) {
|
|
$self->{imap_client}->logout();
|
|
}
|
|
}
|
|
|
|
sub store_email_in_email_folder {
|
|
my ($self, $email_string, $folder_path) = @_;
|
|
$folder_path ||= $self->{base_folder};
|
|
|
|
my $folder_string = $self->get_folder_string_from_path($folder_path);
|
|
$self->{imap_client}->append_string($folder_string, $email_string)
|
|
or die "Could not store email in folder '$folder_string': "
|
|
. $self->{imap_client}->LastError() . "\n";
|
|
}
|
|
|
|
sub set_flag_for_email {
|
|
my ($self, $email_journal, $imap_flag) = @_;
|
|
return unless $imap_flag;
|
|
|
|
my $folder_string = $email_journal->folder;
|
|
|
|
$self->{imap_client}->select($folder_string)
|
|
or die "Could not select IMAP folder '$folder_string': $@\n";
|
|
|
|
my $folder_uidvalidity = $self->{imap_client}->uidvalidity($folder_string)
|
|
or die "Could not get UIDVALIDITY for folder '$folder_string': $@\n";
|
|
|
|
|
|
=item C<update_emails_from_folder>
|
|
|
|
Updates the emails for a folder. Checks which emails are missing and
|
|
fetches these from the IMAP server. Returns the created email import object.
|
|
|
|
=item C<update_emails_from_subfolders>
|
|
|
|
Updates the emails for all subfolders of a folder. Checks which emails are
|
|
missing and fetches these from the IMAP server. Returns the created email
|
|
import object.
|
|
|
|
=item C<_update_emails_from_folder_strings>
|
|
|
|
Updates the emails for a list of folder strings. Checks which emails are
|
|
missing and fetches these from the IMAP server. Returns the created
|
|
email import object.
|
|
|
|
=item C<update_email_files_for_record>
|
|
|