1 |
760 |
ahitrov |
#!/usr/bin/env perl |
2 |
743 |
ahitrov |
|
3 |
|
|
use strict; |
4 |
|
|
use warnings "all"; |
5 |
|
|
use locale; |
6 |
|
|
|
7 |
851 |
ahitrov |
BEGIN { require './inc.pl' }; |
8 |
743 |
ahitrov |
|
9 |
|
|
use Contenido::Globals; |
10 |
|
|
use Contenido::Init; |
11 |
|
|
use ErrorTee; |
12 |
|
|
use PidFile; |
13 |
|
|
use Data::Dumper; |
14 |
|
|
|
15 |
|
|
# begin |
16 |
|
|
Contenido::Init->init(); |
17 |
|
|
|
18 |
|
|
my $keeper_module = $state->project.'::Keeper'; |
19 |
|
|
$keeper = $keeper_module->new($state); |
20 |
768 |
ahitrov |
$DEBUG = 0; |
21 |
743 |
ahitrov |
|
22 |
|
|
#PidFile->new($keeper, compat=>1); # db-based locking (run only on one host) |
23 |
|
|
#PidFile->new($keeper, compat=>1, per_host=>1); # db-based locking (run on whole cluster) |
24 |
|
|
|
25 |
|
|
############################################ |
26 |
|
|
# please use: |
27 |
|
|
# $state->{log_dir} for logging |
28 |
|
|
# $state->{tmp_dir} for temporary files |
29 |
|
|
########################################### |
30 |
|
|
$keeper->t_connect() || die $keeper->error(); |
31 |
|
|
my $ids = $keeper->TSQL->selectall_arrayref("SELECT id FROM mailqueue WHERE status = 0 and dtime <= CURRENT_TIMESTAMP ORDER BY ctime LIMIT 5"); |
32 |
|
|
if ( ref $ids eq 'ARRAY' && @$ids ) { |
33 |
750 |
ahitrov |
$keeper->TSQL->do("UPDATE mailqueue SET status = 2 WHERE ID IN (".join(',', ('?') x scalar @$ids).")", {}, map { $_->[0] } @$ids); |
34 |
743 |
ahitrov |
} |
35 |
|
|
$keeper->t_finish(); |
36 |
|
|
|
37 |
753 |
ahitrov |
my $now = Contenido::DateTime->new; |
38 |
743 |
ahitrov |
if ( @$ids ) { |
39 |
749 |
ahitrov |
my $sendmail = Contenido::Mail->new(); |
40 |
743 |
ahitrov |
foreach my $row ( @$ids ) { |
41 |
|
|
my $mail = $keeper->get_document_by_id($row->[0], class => 'Contenido::Email'); |
42 |
|
|
if ( ref $mail ) { |
43 |
|
|
my $data = { |
44 |
759 |
ahitrov |
to => [split /,\s*/, $mail->name], |
45 |
743 |
ahitrov |
subject => $mail->subject, |
46 |
|
|
body => $mail->body_html, |
47 |
|
|
}; |
48 |
|
|
if ( $mail->cc ) { |
49 |
|
|
$data->{cc} = [split /\n/, $mail->cc]; |
50 |
|
|
} |
51 |
766 |
ahitrov |
if ( $mail->from ) { |
52 |
|
|
$data->{from} = $mail->from; |
53 |
|
|
} |
54 |
743 |
ahitrov |
if ( $mail->body_text ) { |
55 |
|
|
$data->{text} = $mail->body_text; |
56 |
|
|
} |
57 |
|
|
if ( my $err = $sendmail->send({ email => $data }) ) { |
58 |
753 |
ahitrov |
$mail->status( 3 ); |
59 |
743 |
ahitrov |
$mail->error( $err ); |
60 |
|
|
} else { |
61 |
753 |
ahitrov |
$mail->status( 1 ); |
62 |
|
|
$mail->etime( $now->ymd('-').' '.$now->hms ); |
63 |
|
|
$mail->error( undef ); |
64 |
743 |
ahitrov |
} |
65 |
|
|
$mail->store; |
66 |
|
|
} |
67 |
|
|
} |
68 |
|
|
} |