aboutsummaryrefslogtreecommitdiff
path: root/lib/Sietima/Message.pm
blob: b0d82e6130471abd134063e184635e789a8c6381 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package Sietima::Message; 
use Moo;
use Sietima::Policy;
use Types::Standard qw(ArrayRef Object);
use Sietima::Types qw(Address AddressFromStr
                      Subscriber SubscriberFromAddress SubscriberFromStr
                      EmailMIME);
use Email::Address;
use Sietima::Subscriber;
use Email::MIME;
use namespace::clean;
 
# VERSION 
# ABSTRACT: an email message with an envelope 
 
=head1 SYNOPSIS
 
  use Sietima::Message;
 
  my $msg = Sietima::Message->new({
    mail => $email_mime_object,
    from => 'sender@example.com',
    to => [ 'recipient@example.com', 'also@example.com' ],
  });
 
=head1 DESCRIPTION
 
This class pairs a L<< C<Email::MIME> >> object with its
envelope. Objects of this class are usually generated by L<<
C<Sietima::munge_mail>|Sietima/munge_mail >>, and consumed by L<<
C<Sietima::send_message>|Sietima/send_message >>.
 
=head1 ATTRIBUTES
 
All attributes are read-only and required.
 
=attr C<mail>
 
An L<< C<Email::MIME> >> object, representing the message.
 
=cut
 
has mail => (
    is => 'ro',
    isa => EmailMIME,
    required => 1,
);
 
=attr C<from>
 
An L<< C<Email::Address> >> object, coercible from a string,
representing the sender.
 
=cut
 
has from => (
    is => 'ro',
    isa => Address,
    coerce => AddressFromStr,
    required => 1,
);
 
=attr C<to>
 
An arrayref of L<< C<Sietima::Subscriber> >> objects, each coercible
from a string or an L<< C<Email::Address> >> object, representing the
recipients.
 
=cut
 
my $subscriber_array = ArrayRef[
    Subscriber->plus_coercions(
        SubscriberFromStr,
        SubscriberFromAddress,
    )
];
has to => (
    isa => $subscriber_array,
    is => 'ro',
    coerce => $subscriber_array->coercion,
    required => 1,
);
 
=method C<envelope>
 
  my %envelope = $message->envelope->%*;
 
Returns a hashref with envelope data, suitable for use with L<<
C<Email::Sender::Transport::send>|Email::Sender::Transport/send >>.
 
=cut
 
sub envelope ($self) {
    return {
        from => $self->from,
        to => [ map { $_->address } $self->to->@* ],
    }
}
 
1;