| 1 | # $Id: TheSchwartz.pm 3 2007-05-04 01:54:52Z btrott $ | ||
| 2 | |||
| 3 | package Email::Send::TheSchwartz; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | use Email::Address; | ||
| 7 | use Return::Value; | ||
| 8 | |||
| 9 | our $SCHWARTZ; | ||
| 10 | our $VERSION = '0.01'; | ||
| 11 | |||
| 12 | sub is_available { | ||
| 13 | return eval { require TheSchwartz }; | ||
| 14 | } | ||
| 15 | |||
| 16 | sub send { | ||
| 17 | my $class = shift; | ||
| 18 | my($msg, %args) = @_; | ||
| 19 | |||
| 20 | require TheSchwartz; | ||
| 21 | if (keys %args) { | ||
| 22 | $SCHWARTZ = eval { $args{_client} || TheSchwartz->new(%args) }; | ||
| 23 | return failure "$@" if $@; | ||
| 24 | } | ||
| 25 | return failure "Can't send to TheSchwartz unless client is initialized" | ||
| 26 | unless $SCHWARTZ; | ||
| 27 | |||
| 28 | my $env_sender = (Email::Address->parse($msg->header('From')))[0]->address; | ||
| 29 | |||
| 30 | my %to = map { $_->address => 1 } | ||
| 31 | map { Email::Address->parse($msg->header($_)) } | ||
| 32 | qw(To Cc Bcc); | ||
| 33 | my @rcpts = keys %to; | ||
| 34 | |||
| 35 | my $host; | ||
| 36 | if (@rcpts == 1) { | ||
| 37 | $rcpts[0] =~ /(.+)@(.+)$/; | ||
| 38 | $host = lc($2) . '@' . lc($1); | ||
| 39 | } | ||
| 40 | |||
| 41 | my $handle; | ||
| 42 | eval { | ||
| 43 | my $job = TheSchwartz::Job->new( | ||
| 44 | funcname => 'TheSchwartz::Worker::SendEmail', | ||
| 45 | arg => { | ||
| 46 | env_from => $env_sender, | ||
| 47 | rcpts => \@rcpts, | ||
| 48 | data => $msg->as_string, | ||
| 49 | }, | ||
| 50 | coalesce => $host, | ||
| 51 | ); | ||
| 52 | $handle = $SCHWARTZ->insert($job); | ||
| 53 | }; | ||
| 54 | |||
| 55 | return failure "Can't create job: $@" unless !$@ && $handle; | ||
| 56 | |||
| 57 | return success "Message sent", prop => { handle => $handle }; | ||
| 58 | } | ||
| 59 | |||
| 60 | 1; | ||
| 61 | __END__ |