Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 9ecd342a

Von Tamino Steinert vor mehr als 1 Jahr hinzugefügt

  • ID 9ecd342a63f64cabcc843cb96dac23c20f97bd0d
  • Vorgänger c2342266
  • Nachfolger ed6261fd

Mail: Beim senden einer Mail diese in einem Ordner über IMAP ablegen

Unterschiede anzeigen:

SL/Mailer.pm
23 23

  
24 24
package Mailer;
25 25

  
26
use IO::Socket::INET;
27
use IO::Socket::SSL;
28
use Mail::IMAPClient;
26 29
use Email::Address;
27 30
use Email::MIME::Creator;
28 31
use Encode;
......
303 306

  
304 307
  $error = $@ if !$ok;
305 308

  
309
  $self->_store_in_imap_sent_folder() or do {
310
    $ok = 0;
311
    push @$error, $@;
312
  };
313

  
306 314
  # create journal and link to record
307 315
  $self->{journalentry} = $self->_store_in_journal;
308 316
  $self->_create_record_link if $self->{journalentry};
......
316 324
  return map { @{ $self->{addresses}->{$_} || [] } } qw(to cc bcc);
317 325
}
318 326

  
327
sub _get_header_string {
328
  my ($self) = @_;
329
  my $header_string =
330
    join "\r\n",
331
      (bundle_by { join(': ', @_) } 2, @{ $self->{headers} || [] });
332
  return $header_string;
333
}
334

  
335
sub _store_in_imap_sent_folder {
336
  my ($self) = @_;
337
  my $config = $::lx_office_conf{sent_emails_in_imap} || {};
338
  return unless ($config->{enabled} && $config->{hostname});
339

  
340
  my $socket;
341
  if ($config->{ssl}) {
342
    $socket = IO::Socket::SSL->new(
343
      Proto    => 'tcp',
344
      PeerAddr => $config->{hostname},
345
      PeerPort => $config->{port} || 993,
346
    );
347
  } else {
348
    $socket = IO::Socket::INET->new(
349
      Proto    => 'tcp',
350
      PeerAddr => $config->{hostname},
351
      PeerPort => $config->{port} || 143,
352
    );
353
  }
354
  if (!$socket) {
355
    die "Failed to create socket for IMAP client: $@\n";
356
  }
357

  
358
  # TODO: get email and password for each user
359
  my $imap = Mail::IMAPClient->new(
360
    Socket   => $socket,
361
    User     => $config->{username},
362
    Password => $config->{password},
363
  ) or do {
364
    die "Failed to create IMAP Client: $@\n"
365
  };
366

  
367
  $imap->IsAuthenticated() or do {
368
    die "IMAP Client login failed: " . $imap->LastError() . "\n";
369
  };
370

  
371
  my $separator =  $imap->separator();
372
  my $folder    =  $config->{folder} || 'INBOX/Sent';
373
  $folder       =~ s|/|${separator}|g;
374

  
375
  my $header_string = $self->_get_header_string;
376
  my $email_string = $header_string. "\n" . $self->{message};
377

  
378
  # TODO: doesn't stop with non ASCII-Chars
379
  # In test file it works like a charm !?
380
  $imap->append_string($folder, $email_string) or do {
381
    $imap->logout();
382
    die "IMAP Client append failed: " . $imap->LastError() . "\n";
383
  };
384

  
385
  $imap->logout();
386
  return 1;
387
}
388

  
319 389
sub _store_in_journal {
320 390
  my ($self, $status, $extended_status) = @_;
321 391

  
......
328 398
  $extended_status //= $self->{driver}->extended_status if $self->{driver};
329 399
  $extended_status //= 'unknown error';
330 400

  
331
  my $headers = join "\r\n", (bundle_by { join(': ', @_) } 2, @{ $self->{headers} || [] });
401
  my $headers = $self->_get_header_string;
332 402

  
333 403
  my $jentry = SL::DB::EmailJournal->new(
334 404
    sender          => SL::DB::Manager::Employee->current,
config/kivitendo.conf.default
162 162
login =
163 163
password =
164 164

  
165
[sent_emails_in_imap]
166
enabled = 0
167
hostname = localhost
168
username =
169
password =
170
# Use / for subfolders
171
folder = INBOX/Sent
172
# Port only needs to be changed if it is not the default port.
173
# port = 143
174
# If SSL is used, default port is 993
175
ssl = 1
176

  
165 177
[applications]
166 178
# Location of OpenOffice.org/LibreOffice writer
167 179
openofficeorg_writer = lowriter

Auch abrufbar als: Unified diff