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