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 >> object with its envelope. Objects of this class are usually generated by L<< C|Sietima/munge_mail >>, and consumed by L<< C|Sietima/send_message >>. =head1 ATTRIBUTES All attributes are read-only and required. =attr C An L<< C >> object, representing the message. =cut has mail => ( is => 'ro', isa => EmailMIME, required => 1, ); =attr C An L<< C >> object, coercible from a string, representing the sender. =cut has from => ( is => 'ro', isa => Address, coerce => AddressFromStr, required => 1, ); =attr C An arrayref of L<< C >> objects, each coercible from a string or an L<< C >> 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 my %envelope = $message->envelope->%*; Returns a hashref with envelope data, suitable for use with L<< C|Email::Sender::Transport/send >>. =cut sub envelope ($self) { return { from => $self->from, to => [ map { $_->address } $self->to->@* ], } } 1;