Revision 11af2b2c
Von Tamino Steinert vor mehr als 1 Jahr hinzugefügt
SL/IMAPClient.pm | ||
---|---|---|
7 | 7 |
use IO::Socket::INET; |
8 | 8 |
use IO::Socket::SSL; |
9 | 9 |
use Mail::IMAPClient; |
10 |
use Email::MIME; |
|
10 | 11 |
use File::MimeInfo::Magic; |
11 | 12 |
use Encode qw(encode decode); |
12 | 13 |
use Encode::IMAPUTF7; |
13 | 14 |
|
14 | 15 |
use SL::SessionFile; |
15 | 16 |
use SL::Locale::String qw(t8); |
17 |
use SL::DB::EmailImport; |
|
18 |
use SL::DB::EmailJournal; |
|
16 | 19 |
|
17 | 20 |
my %TYPE_TO_FOLDER = ( |
18 | 21 |
sales_quotation => t8('Sales Quotations'), |
... | ... | |
44 | 47 |
} |
45 | 48 |
} |
46 | 49 |
|
50 |
sub update_emails_from_folder { |
|
51 |
my ($self, $folder_path) = @_; |
|
52 |
|
|
53 |
my $folder_string = $self->get_folder_string_from_path($folder_path); |
|
54 |
|
|
55 |
$self->{imap_client}->select($folder_string) |
|
56 |
or die "Could not select IMAP folder '$folder_string': $@\n"; |
|
57 |
|
|
58 |
my $msg_uids = $self->{imap_client}->messages |
|
59 |
or die "Could not messages via IMAP: $@\n"; |
|
60 |
|
|
61 |
my $query = <<SQL; |
|
62 |
SELECT uid |
|
63 |
FROM email_imports ei |
|
64 |
LEFT JOIN email_journal ej |
|
65 |
ON ej.email_import_id = ei.id |
|
66 |
WHERE ei.host_name = ? |
|
67 |
AND ei.user_name = ? |
|
68 |
AND ej.folder = ? |
|
69 |
SQL |
|
70 |
my $dbh = SL::DB->client->dbh; |
|
71 |
my $existing_uids = $dbh->selectall_hashref($query, 'uid', undef, |
|
72 |
$self->{hostname}, $self->{username}, $folder_path); |
|
73 |
|
|
74 |
my @new_msg_uids = grep { !$existing_uids->{$_} } @$msg_uids; |
|
75 |
|
|
76 |
return unless @new_msg_uids; |
|
77 |
|
|
78 |
SL::DB->client->with_transaction(sub { |
|
79 |
my $email_import = $self->_create_email_import($folder_path); |
|
80 |
$email_import->save(); # save to get id |
|
81 |
|
|
82 |
foreach my $new_uid (@new_msg_uids) { |
|
83 |
my $new_email_string = $self->{imap_client}->message_string($new_uid); |
|
84 |
my $email = Email::MIME->new($new_email_string); |
|
85 |
my $email_journal = $self->_create_email_journal( |
|
86 |
$email, $email_import, $new_uid, $folder_path |
|
87 |
); |
|
88 |
$email_journal->save(); |
|
89 |
} |
|
90 |
}); |
|
91 |
|
|
92 |
return; |
|
93 |
} |
|
94 |
|
|
95 |
sub _create_email_import { |
|
96 |
my ($self, $folder_path) = @_; |
|
97 |
my $email_import = SL::DB::EmailImport->new( |
|
98 |
host_name => $self->{hostname}, |
|
99 |
user_name => $self->{username}, |
|
100 |
folder => $folder_path, |
|
101 |
); |
|
102 |
return $email_import; |
|
103 |
} |
|
104 |
|
|
105 |
sub _create_email_journal { |
|
106 |
my ($self, $email, $email_import, $uid, $folder_path) = @_; |
|
107 |
|
|
108 |
my @email_parts = $email->parts; # get parts or self |
|
109 |
my $text_part = $email_parts[0]; # TODO: check if its allways the first part |
|
110 |
my $body = $text_part->body; |
|
111 |
|
|
112 |
my $header_string = join "\r\n", |
|
113 |
(map { $_ . ': ' . $email->header($_) } $email->header_names); |
|
114 |
|
|
115 |
my $date = $self->_parse_date($email->header('Date')); |
|
116 |
|
|
117 |
my $recipients = $email->header('To'); |
|
118 |
$recipients .= ', ' . $email->header('Cc') if ($email->header('Cc')); |
|
119 |
$recipients .= ', ' . $email->header('Bcc') if ($email->header('Bcc')); |
|
120 |
|
|
121 |
my @attachments = (); |
|
122 |
$email->walk_parts(sub { |
|
123 |
my ($part) = @_; |
|
124 |
my $filename = $part->filename; |
|
125 |
if ($filename) { |
|
126 |
my $content_type = $part->content_type; |
|
127 |
my $content = $part->body; |
|
128 |
my $attachment = SL::DB::EmailJournalAttachment->new( |
|
129 |
name => $filename, |
|
130 |
content => $content, |
|
131 |
mime_type => $content_type, |
|
132 |
); |
|
133 |
push @attachments, $attachment; |
|
134 |
} |
|
135 |
}); |
|
136 |
|
|
137 |
my $email_journal = SL::DB::EmailJournal->new( |
|
138 |
email_import_id => $email_import->id, |
|
139 |
folder => $folder_path, |
|
140 |
uid => $uid, |
|
141 |
status => 'imported', |
|
142 |
extended_status => '', |
|
143 |
from => $email->header('From') || '', |
|
144 |
recipients => $recipients, |
|
145 |
sent_on => $date, |
|
146 |
subject => $email->header('Subject') || '', |
|
147 |
body => $body, |
|
148 |
headers => $header_string, |
|
149 |
attachments => \@attachments, |
|
150 |
); |
|
151 |
|
|
152 |
return $email_journal; |
|
153 |
} |
|
154 |
|
|
155 |
sub _parse_date { |
|
156 |
my ($self, $date) = @_; |
|
157 |
return '' unless $date; |
|
158 |
my $strp = DateTime::Format::Strptime->new( |
|
159 |
pattern => '%a, %d %b %Y %H:%M:%S %z', |
|
160 |
time_zone => 'UTC', |
|
161 |
); |
|
162 |
my $dt = $strp->parse_datetime($date); |
|
163 |
return $dt->strftime('%Y-%m-%d %H:%M:%S'); |
|
164 |
} |
|
165 |
|
|
47 | 166 |
sub update_emails_for_record { |
48 | 167 |
my ($self, $record) = @_; |
49 | 168 |
|
Auch abrufbar als: Unified diff
IMAPClient: Funktion für Emails aus einem Ordner zu synchronisieren