View | Details | Raw Unified | Return to bug 29330
Collapse All | Expand All

(-)a/Koha/Email.pm (-1 / +43 lines)
Lines 22-28 use Modern::Perl; Link Here
22
22
23
use Email::Address;
23
use Email::Address;
24
use Email::MessageID;
24
use Email::MessageID;
25
use Email::MIME;
25
use List::Util qw( pairs );
26
use List::Util qw( pairs );
27
use Scalar::Util qw( blessed );
26
28
27
use Koha::Exceptions;
29
use Koha::Exceptions;
28
30
Lines 38-43 Koha::Email - A wrapper around Email::Stuffer Link Here
38
40
39
=head2 Class methods
41
=head2 Class methods
40
42
43
=head3 new_from_string
44
45
    my $email = Koha::Email->new_from_string( $email_string );
46
47
Constructor for the Koha::Email class. The I<$email_string> (mandatory)
48
parameter will be parsed with I<Email::MIME>.
49
50
Note: I<$email_string> can be the produced by the I<as_string> method from
51
B<Koha::Email> or B<Email::MIME>.
52
53
=cut
54
55
sub new_from_string {
56
    my ( $class, $email_string ) = @_;
57
58
    Koha::Exceptions::MissingParameter->throw("Mandatory string parameter missing.")
59
        unless $email_string;
60
61
    my $self = $class->SUPER::new();
62
    my $mime = Email::MIME->new( $email_string );
63
    $self->{email} = $mime;
64
65
    return $self;
66
}
67
41
=head3 create
68
=head3 create
42
69
43
    my $email = Koha::Email->create(
70
    my $email = Koha::Email->create(
Lines 126-132 sub create { Link Here
126
    $args->{bcc} = $addresses->{bcc}
153
    $args->{bcc} = $addresses->{bcc}
127
        if $addresses->{bcc};
154
        if $addresses->{bcc};
128
155
129
    my $email = $self->SUPER::new( $args );
156
    my $email;
157
    # FIXME: This is ugly, but aids backportability
158
    # TODO: Remove this and move address and default headers handling
159
    #       to separate subs to be (re)used
160
    if ( blessed($self) ) {
161
        $email = $self;
162
        $email->to( $args->{to} )             if $args->{to};
163
        $email->from( $args->{from} )         if $args->{from};
164
        $email->cc( $args->{cc} )             if $args->{cc};
165
        $email->bcc( $args->{bcc} )           if $args->{bcc};
166
        $email->reply_to( $args->{reply_to} ) if $args->{reply_to};
167
        $email->subject( $args->{subject} )   if $args->{subject};
168
    }
169
    else {
170
        $email = $self->SUPER::new( $args );
171
    }
130
172
131
    $email->header( 'Reply-To', $addresses->{reply_to} )
173
    $email->header( 'Reply-To', $addresses->{reply_to} )
132
        if $addresses->{reply_to};
174
        if $addresses->{reply_to};
(-)a/t/Koha/Email.t (-2 / +26 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 4;
20
use Test::More tests => 5;
21
21
22
use Test::MockModule;
22
use Test::MockModule;
23
use Test::Exception;
23
use Test::Exception;
Lines 266-268 subtest 'is_valid' => sub { Link Here
266
    isnt(Koha::Email->is_valid('example.com'), 1);
266
    isnt(Koha::Email->is_valid('example.com'), 1);
267
    isnt(Koha::Email->is_valid('from'), 1);
267
    isnt(Koha::Email->is_valid('from'), 1);
268
};
268
};
269
- 
269
270
subtest 'new_from_string() tests' => sub {
271
272
    plan tests => 1;
273
274
    my $html_body = '<h1>Title</h1><p>Message</p>';
275
    my $email_1 = Koha::Email->create(
276
        {
277
            from        => 'Fróm <from@example.com>',
278
            to          => 'Tö <to@example.com>',
279
            cc          => 'cc@example.com',
280
            bcc         => 'bcc@example.com',
281
            reply_to    => 'reply_to@example.com',
282
            sender      => 'sender@example.com',
283
            subject     => 'Some subject',
284
            html_body   => $html_body,
285
            body_params => { charset => 'iso-8859-1' },
286
        }
287
    );
288
289
    my $string = $email_1->as_string;
290
    my $email_2 = Koha::Email->new_from_string( $string );
291
292
    is( $email_1->as_string, $email_2->as_string, 'Emails match' );
293
};

Return to bug 29330