File Coverage

blib/lib/Email/Sender/Transport/Redirect.pm
Criterion Covered Total %
statement 6 6 100.0
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 8 8 100.0


line stmt bran cond sub pod time code
1             package Email::Sender::Transport::Redirect;
2             {
3             $Email::Sender::Transport::Redirect::VERSION = '0.0004';
4             }
5              
6             =head1 NAME
7              
8             Email::Sender::Transport::Redirect - Intercept all emails and redirect them to a specific address
9              
10             =head1 VERSION
11              
12             Version 0.0004
13              
14             =head1 SYNOPSIS
15              
16             $transport_orig = Email::Sender::Transport::Sendmail->new;
17              
18             $transport = Email::Sender::Transport::Redirect->new({transport => $transport_orig,
19             redirect_address => 'shop@nitesi.com',
20             });
21              
22             =head1 DESCRIPTION
23              
24             Transport wrapper for Email::Sender which intercepts all emails and redirects
25             them to a specific address.
26              
27             This transport changes the C and C header in the email and
28             adds a C and C header with
29             the original recipients.
30              
31             =head1 ATTRIBUTES
32              
33             =head2 redirect_address
34              
35             Recipient email address for redirected emails.
36              
37             =head2 redirect_headers
38              
39             Email headers to be changed, defaults to an
40             array reference containing:
41              
42             =over 4
43              
44             =item To
45              
46             =item CC
47              
48             =back
49              
50             =head2 intercept_prefix
51              
52             Prefix for headers which show the original recipients.
53              
54             Defaults to C.
55              
56             =cut
57              
58 1     1   22403 use Moo;
  1         22473  
  1         6  
59 1     1   3396 use MooX::Types::MooseLike::Base qw/ArrayRef Str/;
  1         6688  
  1         413  
60              
61             extends 'Email::Sender::Transport::Wrapper';
62              
63             has 'redirect_address' => (is => 'ro',
64             required => 1,
65             );
66              
67             has 'redirect_headers' => (
68             is => 'ro',
69             isa => ArrayRef,
70             default => sub { [qw/To Cc/] },
71             );
72              
73             has 'intercept_prefix' => (
74             is => 'ro',
75             isa => Str,
76             default => 'X-Intercepted-',
77             );
78              
79             =head1 METHOD MODIFIERS
80              
81             =head2 send_email
82              
83             Wraps around original method and changes email headers.
84              
85             =cut
86              
87             around send_email => sub {
88             my ($orig, $self, $email, $env, @rest) = @_;
89             my ($email_copy, $env_copy, @values);
90              
91             # copy email object to prevent changes in the original object
92             $email_copy = ref($email)->new($email->as_string);
93              
94             # copy envelope hash reference
95             %$env_copy = %$env;
96              
97             for my $header (@{$self->redirect_headers}) {
98             next unless @values = $email_copy->get_header($header);
99              
100             if ($self->intercept_prefix) {
101             $email_copy->set_header($self->intercept_prefix . $header,
102             @values);
103             }
104              
105             $email_copy->set_header($header);
106             }
107              
108             $email_copy->set_header('To', $self->redirect_address);
109             $env_copy->{to} = [$self->redirect_address];
110              
111             return $self->$orig($email_copy, $env_copy, @rest);
112             };
113              
114             =head1 AUTHOR
115              
116             Stefan Hornburg (Racke), C
117              
118             =head1 ACKNOWLEDGEMENTS
119              
120             Thanks to Peter Mottram for the port to Moo (GH #1).
121              
122             Thanks to Matt Trout for his help regarding the initial write of this
123             module on #dancer IRC.
124              
125             =head1 LICENSE AND COPYRIGHT
126              
127             Copyright 2012-2015 Stefan Hornburg (Racke).
128              
129             This program is free software; you can redistribute it and/or modify it
130             under the terms of either: the GNU General Public License as published
131             by the Free Software Foundation; or the Artistic License.
132              
133             See http://dev.perl.org/licenses/ for more information.
134              
135              
136             =cut
137              
138             1; # End of Email::Sender::Transport::Redirect