blib/lib/Email/Sender/Transport/Redirect.pm

filter: Statementstmt 50.0% 14 / 28 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 50.0% 2 / 4 Podpod n/a totaltotal 44.4% 16 / 36 SCAR 19.2
CC
4
Cov
44%
__ANON__
20.0
__ANON__
2.0
BEGIN
1.0
CRAP 6.8
↑ Summary
1 package Email::Sender::Transport::Redirect;
2 {
3 1 $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<To> and C<CC> header in the email and
28 adds a C<X-Intercepted-To> and C<X-Intercepted-CC> 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<X-Intercepted->.
55  
56 =cut
57  
58 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 1 ▶use MooX::Types::MooseLike::Base qw/ArrayRef Str/;
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 1 extends 'Email::Sender::Transport::Wrapper';
62  
63 1 has 'redirect_address' => (is => 'ro',
64 required => 1,
65 );
66  
67 has 'redirect_headers' => (
68 is => 'ro',
69 isa => ArrayRef,
70 0 ▶ default => sub { [qw/To Cc/] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
71 1 );
72  
73 1 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 0 ▶ my ($orig, $self, $email, $env, @rest) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
89 0 my ($email_copy, $env_copy, @values);
90  
91 # copy email object to prevent changes in the original object
92 0 $email_copy = ref($email)->new($email->as_string);
93  
94 # copy envelope hash reference
95 0 %$env_copy = %$env;
96  
97 0 for my $header (@{$self->redirect_headers}) {
98 0 ▶ next unless @values = $email_copy->get_header($header);
Branch: 1 branch
BranchTrueFalseTotal
unless @values = $email_copy->get_header($header) 0 0 0
99  
100 0 ▶ if ($self->intercept_prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->intercept_prefix) 0 0 0
101 0 $email_copy->set_header($self->intercept_prefix . $header,
102 @values);
103 }
104  
105 0 $email_copy->set_header($header);
106 }
107  
108 0 $email_copy->set_header('To', $self->redirect_address);
109 0 $env_copy->{to} = [$self->redirect_address];
110  
111 0 return $self->$orig($email_copy, $env_copy, @rest);
112 1 };
113  
114 =head1 AUTHOR
115  
116 Stefan Hornburg (Racke), C<racke@linuxia.de>
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 1; # End of Email::Sender::Transport::Redirect
↑ Summary