File Coverage

blib/lib/Mail/Builder/Simple.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package Mail::Builder::Simple;
2              
3 2     2   32272 use strict;
  2         4  
  2         124  
4 2     2   13 use warnings;
  2         3  
  2         76  
5 2     2   966 use Mail::Builder;
  0            
  0            
6             use Email::Sender::Simple;
7             use Email::Valid;
8             use Encode;
9             use Carp qw/cluck/;
10             use Config::Any;
11             use Module::Load;
12             use base 'Mail::Builder';
13              
14             use 5.008_008;
15              
16             our $VERSION = '0.16';
17              
18             sub new {
19             my @params = @_;
20             my $class = shift @params;
21             my $args = ref( $params[0] ) eq 'HASH' ? $params[0] : {@params}
22             or cluck 'Can\'t create mail object. Invalid parameters hash';
23              
24             my $self = $class->SUPER::new();
25             bless $self, $class;
26              
27             my @fields = qw/from reply organization returnpath sender priority
28             subject plaintext htmltext language to cc bcc attachment image mailer/;
29              
30             $self->{mail_fields} = join q{|}, @fields;
31              
32             $self->_add_args($args);
33              
34             return bless $self, $class;
35             }
36              
37             sub _add_args {
38             my @params = @_;
39             my $self = shift @params;
40             my $args = ref( $params[0] ) eq 'HASH' ? $params[0] : {@params}
41             or return;
42              
43             if ( $args->{mail_client} ) {
44             $self->{mail_client} = $args->{mail_client};
45             }
46              
47             if ( $args->{template_args} ) {
48             $self->{template_args} = $args->{template_args};
49             }
50              
51             if ( $args->{template_vars} ) {
52             $self->{template_vars} = $args->{template_vars};
53             }
54              
55             if ( my $config_file = $args->{config_file} ) {
56             my $conf = Config::Any->load_files(
57             {
58             files => [$config_file],
59             use_ext => 1,
60             driver_args => { General => { -UTF8 => 1 } },
61             }
62             );
63              
64             delete $args->{config_file};
65              
66             $self->_add_args( $conf->[0]->{$config_file} );
67             }
68              
69             foreach my $field ( sort keys %{$args} ) {
70             $self->_add_arg( $args, $field );
71             }
72              
73             return 1;
74             }
75              
76             sub _add_arg {
77             my ( $self, $args, $field ) = @_;
78              
79             return if $field !~ /^(?:$self->{mail_fields})$/smox;
80              
81             my $value = $args->{$field};
82              
83             #If the value is an arrayref
84             if ( $value and ref($value) eq 'ARRAY' ) {
85              
86             #There are more items
87             if ( $value->[0] and $value->[0] eq 'MORE' ) {
88             shift @{$value};
89             $self->_process_array( $args, $field, $value );
90             }
91              
92             #There are more items
93             elsif ( $value->[0] and ref( $value->[0] ) eq 'ARRAY' ) {
94             $self->_process_array( $args, $field, $value );
95             }
96              
97             #There is an item with one or more fields
98             elsif ( $value->[0] and not ref( $value->[0] ) ) {
99             $self->_process_item( $args, $field, $value );
100             }
101             else {
102             cluck 'Items inside arrayref should be scalar/arrayref.';
103             }
104             }
105              
106             #The value is a scalar
107             elsif ( $value and not ref $value ) {
108             $self->_set_or_add( $field, $value );
109             }
110             else {
111             cluck "Value for $field should be scalar/an arrayref";
112             }
113              
114             return 1;
115             }
116              
117             sub _process_array {
118             my ( $self, $args, $field, $value ) = @_;
119              
120             foreach my $item ( @{$value} ) {
121             $self->_process_item( $args, $field, $item );
122             }
123              
124             return 1;
125             }
126              
127             sub _process_item {
128             my ( $self, $args, $field, $item ) = @_;
129              
130             #This item is an arrayref
131             if ( ref($item) eq 'ARRAY' ) {
132              
133             #This item is a template
134             if ( $item->[-1] =~ /^:(.+)/smox ) {
135             my $result = $self->_process_template( $args, $item, $field );
136             $self->_set_or_add( $field, $result );
137             }
138              
139             #It is not a template
140             else {
141             $self->_set_or_add( $field, @{$item} );
142             }
143             }
144              
145             #It is a scalar
146             elsif ( !ref $item ) {
147             $self->_set_or_add( $field, $item );
148             }
149             else {
150             cluck 'The elements of the array can be just scalars.';
151             }
152              
153             return 1;
154             }
155              
156             sub _process_template {
157             my ( $self, $args, $item, $field ) = @_;
158              
159             my $type = substr $item->[-1], 1;
160             ( $type, my $source ) = split /-/smx, $type;
161             $source ||= 'file';
162             delete $item->[-1];
163              
164             #Get and overwrite template_args
165             my $template_args = $self->{template_args};
166             if ( $args->{template_args} ) {
167             foreach ( keys %{ $args->{template_args} } ) {
168             $template_args->{$_} = $args->{template_args}->{$_};
169             }
170             }
171              
172             #If this template has its own settings:
173             if ( ref( $item->[-1] ) eq 'HASH' ) {
174             my $template_settings = pop @{$item};
175             foreach ( keys %{$template_settings} ) {
176              
177             #Insert and overwrite the new template settings:
178             $template_args->{$_} = $template_settings->{$_};
179             }
180             }
181              
182             #Get and overwrite template_vars
183             my $template_vars = $self->{template_vars};
184             if ( $args->{template_vars} ) {
185             foreach ( keys %{ $args->{template_vars} } ) {
186             $template_vars->{$_} = $args->{template_vars}->{$_};
187             }
188             }
189              
190             #If this template has its own variables:
191             if ( ref( $item->[-1] ) eq 'HASH' ) {
192             my $template_variables = pop @{$item};
193             foreach ( keys %{$template_variables} ) {
194              
195             #Insert and overwrite the template vars:
196             $template_vars->{$_} = $template_variables->{$_};
197             }
198             }
199              
200             load "Mail::Builder::Simple::$type";
201              
202             my $t =
203             "Mail::Builder::Simple::$type"->new( $template_args, $template_vars );
204              
205             $item->[0] = $t->process( $item->[0], $source );
206              
207             if ( $field eq 'attachment' ) {
208             return Mail::Builder::Attachment->new( \$item->[0], $item->[1], $item->[2] );
209             }
210             else {
211             return $item;
212             }
213             }
214              
215             sub _set_or_add {
216             my ( $self, $field, @value ) = @_;
217              
218             return if not $self->_check_email_valid( $field, @value );
219              
220             if ( $field eq 'from'
221             or $field eq 'reply'
222             or $field eq 'organization'
223             or $field eq 'returnpath'
224             or $field eq 'sender'
225             or $field eq 'priority'
226             or $field eq 'subject'
227             or $field eq 'language'
228             or $field eq 'mailer' )
229             {
230             $self->$field(@value);
231             }
232             elsif ( $field eq 'plaintext' or $field eq 'htmltext' ) {
233             if ( ref( $value[0] ) eq 'ARRAY' ) {
234             $self->$field( $value[0][0] );
235             }
236             else {
237             $self->$field( $value[0] );
238             }
239             }
240             else {
241             if ( $self->$field ) {
242             $self->$field->add(@value);
243             }
244             else {
245             $self->$field(@value);
246             }
247             }
248              
249             return 1;
250             }
251              
252             sub _check_email_valid {
253             my ( $self, $field, @value ) = @_;
254              
255             if ( $field eq 'from'
256             or $field eq 'to'
257             or $field eq 'cc'
258             or $field eq 'bcc'
259             or $field eq 'reply'
260             or $field eq 'returnpath' )
261             {
262             if ( !Email::Valid->address( $value[0] ) ) {
263             warn "Bad email address: $value[0]";
264             return;
265             }
266             }
267              
268             return 1;
269             }
270              
271             sub sendmail {
272             my @params = @_;
273             my $self = shift @params;
274             my $args = ref( $params[0] ) eq 'HASH' ? $params[0] : {@params}
275             or cluck 'Can\'t send mail. Invalid parameters hash';
276              
277             #Add message fields:
278             $self->_add_args($args);
279              
280             #Create the email message
281             my $entity = $self->build_message;
282              
283             #Add custom headers if there are:
284             $self->_add_custom_headers( $entity, $args );
285              
286             my $mail_client = $self->{mail_client};
287              
288             my $mailer = $self->_load_mailer($mail_client);
289              
290             my $mailer_args = $self->_mailer_args($mail_client);
291              
292             #Accept the old keys for compatibility with older versions:
293             $mailer_args = $self->_asure_compatibility($mailer_args);
294              
295             #If the mailer_args contains other addresses to send the email to
296             #than the ones from the email:
297             my $different_addresses = $self->_different_email_addresses($mailer_args);
298              
299             #For sending with send() or try_to_send()
300             my $transport = $mailer->new( %{$mailer_args} );
301              
302             if ( $mail_client->{live_on_error} ) {
303             Email::Sender::Simple->try_to_send( $entity->stringify,
304             { transport => $transport, %{$different_addresses} } );
305             }
306             else {
307             Email::Sender::Simple->send( $entity->stringify,
308             { transport => $transport, %{$different_addresses} } );
309             }
310              
311             #Reset To, Cc and BcC:
312             $self->to->reset;
313             $self->cc->reset;
314             $self->bcc->reset;
315              
316             return 1;
317             }
318              
319             *send = \&sendmail;
320              
321             sub _add_custom_headers {
322             my ( $self, $entity, $args ) = @_;
323              
324             foreach my $field ( keys %{$args} ) {
325             next if $field =~ /^(?:$self->{mail_fields})$/smox;
326              
327             $entity->head->replace( $field,
328             Encode::encode( 'MIME-Header', $args->{$field} ) );
329             }
330              
331             return 1;
332             }
333              
334             sub _load_mailer {
335             my ( $self, $mail_client ) = @_;
336              
337             my $mailer;
338              
339             if ($mail_client) {
340             $mailer = $mail_client->{mailer};
341             }
342              
343             if ( !$mailer ) {
344             $mailer = 'Sendmail';
345             }
346              
347             if ( $mailer !~ /^Email::Sender::Transport/smox ) {
348             $mailer = 'Email::Sender::Transport::' . $mailer;
349             }
350              
351             load $mailer;
352              
353             return $mailer;
354             }
355              
356             sub _mailer_args {
357             my ( $self, $mail_client ) = @_;
358              
359             my %mailer_args = ();
360             if ( ref( $mail_client->{mailer_args} ) eq 'HASH' ) {
361             %mailer_args = %{ $mail_client->{mailer_args} };
362             }
363             elsif ( ref( $mail_client->{mailer_args} ) eq 'ARRAY' ) {
364             %mailer_args = @{ $mail_client->{mailer_args} };
365             }
366              
367             return \%mailer_args;
368             }
369              
370             sub _asure_compatibility {
371             my ( $self, $mailer_args ) = @_;
372              
373             if ( $mailer_args->{Host} ) {
374             $mailer_args->{host} = delete $mailer_args->{Host};
375             }
376              
377             if ( $mailer_args->{username} ) {
378             $mailer_args->{sasl_username} = $mailer_args->{username};
379             }
380              
381             if ( $mailer_args->{password} ) {
382             $mailer_args->{sasl_password} = $mailer_args->{password};
383             }
384              
385             #Add the port if it was provided as host:port
386             if ( $mailer_args->{host} and $mailer_args->{host} =~ /:(\d+)$/smx ) {
387             my $port = $1;
388             $mailer_args->{host} =~ s/:\d+$//smx;
389             $mailer_args->{port} = $port;
390             }
391              
392             return $mailer_args;
393             }
394              
395             sub _different_email_addresses {
396             my ( $self, $mailer_args ) = @_;
397              
398             my %different_addresses;
399              
400             if ( $mailer_args->{to} ) {
401             $different_addresses{to} = delete $mailer_args->{to};
402             }
403              
404             if ( $mailer_args->{cc} ) {
405             $different_addresses{cc} = delete $mailer_args->{cc};
406             }
407              
408             if ( $mailer_args->{from} ) {
409             $different_addresses{from} = delete $mailer_args->{from};
410             }
411              
412             return \%different_addresses;
413             }
414              
415             1;
416              
417             __END__
418              
419             =head1 NAME
420              
421             Mail::Builder::Simple - Send UTF-8 HTML and text email with attachments and inline images, eventually using templates
422              
423             =head1 VERSION
424              
425             Version 0.15
426              
427             =head1 SYNOPSIS
428              
429             # Send a plain text email with Sendmail:
430              
431             use Mail::Builder::Simple;
432              
433             my $mail = Mail::Builder::Simple->new;
434              
435             $mail->send(
436             from => 'me@host.com',
437             to => 'you@yourhost.com',
438             subject => 'The subject with UTF-8 chars',
439             plaintext => "Hello,\n\nHow are you?\n",
440             );
441              
442             # Send the email with an SMTP server:
443              
444             $mail->send(
445             mail_client => {
446             mailer => 'SMTP',
447             mailer_args => {host => 'smtp.host.com'},
448             },
449             from => 'me@host.com',
450             to => 'you@yourhost.com',
451             subject => 'The subject with UTF-8 chars',
452             plaintext => "Hello,\n\nHow are you?\n",
453             );
454              
455             # Send a text and HTML email with an attachment and an inline image
456             # Specify the displayed name for To: and From: fields and add other headers
457              
458             $mail->send(
459             from => ['me@host.com', 'My Name'],
460             to => ['you@yourhost.com', 'Your Name'],
461             reply => 'foo@anotherhost.com',
462             subject => 'The subject with UTF-8 chars',
463             plaintext => "Hello,\n\nHow are you?\n\n",
464             htmltext => "<h1>Hello,</h1> <p>How are you?</p>",
465             attachment => 'file.pdf',
466             image => 'logo.png',
467             priority => 1,
468             mailer => 'My Mailer 0.01',
469             );
470              
471             Warning! The previous version of this module was using C<Email::Send> for sending mail but because of the issues of Email::Send, this version uses C<Email::Sender::Simple> and because of this change there may appear incompatibilities (although at least for the programs which are using Sendmail and SMTP mailers there shouldn't be any issues). Look for "Compatibility" below.
472              
473             =head1 DESCRIPTION
474              
475             C<Mail::Builder::Simple> can create email messages with L<Mail::Builder|Mail::Builder> and send them with L<Email::Sender::Simple|Email::Sender::Simple>. It has the following features:
476              
477             =over
478              
479             =item UTF-8 encoding
480              
481             C<Mail::Builder::Simple> automaticly encodes the body and headers of the email messages to UTF-8, so they can display the special chars in other languages than English correctly.
482              
483             =item attachments
484              
485             C<Mail::Builder::Simple> allow adding one or more attachments to the message, without needing to specify their Content-Type if you don't want to.
486              
487             The attachments can be files saved on the disk or can be created on the fly, eventually using templates.
488              
489             =item images
490              
491             C<Mail::Builder::Simple> can add inline images that will be displayed in the HTML part of the message.
492              
493             =item templates
494              
495             The body and the attachments can be created using a template, either using external template files or templates from scalar variables.
496              
497             C<Mail::Builder::Simple> uses other modules like L<Mail::Builder::Simple::TT|Mail::Builder::Simple::TT> and L<Mail::Builder::Simple::HTML::Template|Mail::Builder::Simple::HTML::Template> in order to allow using L<Template-Toolkit|Template> or L<HTML::Template|HTML::Template> templates.
498              
499             For using another templating system, you can create a module like these 2 modules.
500              
501             =item mail sender
502              
503             C<Mail::Builder::Simple> can send the email messages using any of the mailers allowed by L<Email::Sender::Simple|Email::Sender::Simple> and some of them are: Sendmail, SMTP, SMTP::Persistent, Maildir, Mbox.
504              
505             =item configuration file
506              
507             All the parameters that can be sent to the C<new()> function can be also stored in a configuration file, and this file can be used in more applications.
508              
509             For example, you could save the mailer type, the mailer host, username and password and maybe the C<From:> field in a configuration file, so you won't need to specify them each time when you want to send an email.
510              
511             =back
512              
513             =head1 CONFIGURATION AND ENVIRONMENT
514              
515             The configuration file is specified using the C<config_file> key when the C<new()> constructor is called.
516              
517             It is explained below.
518              
519             =head1 SUBROUTINES/METHODS
520              
521             Mail::Builder::Simple offers the following methods:
522              
523             =over
524              
525             =item new()
526              
527             This is the constructor of the C<Mail::Builder::Simple> object. This object is a L<Mail::Builder|Mail::Builder> object also, so you can use the methods from Mail::Builder on it if you want.
528              
529             =item send()
530              
531             This function sends the email. After sending the email, it cleans the C<To:>, C<CC:> and C<BCC:> fields, so you can send the already built message to somebody else if you want, needing to specify only the recipient's email address.
532              
533             =back
534              
535             These 2 functions can receive a hash with parameters that have the keys explained below.
536              
537             =head1 Parameters
538              
539             =head2 mail_client
540              
541             This parameter is optional.
542              
543             It is a hashref with all the options needed to configure L<Email::Sender::Simple|Email::Sender::Simple> for sending the email messages.
544              
545             It looks like:
546              
547             mail_client => {
548             mailer => 'SMTP',
549             mailer_args => {host => 'smtp.host.com'},
550             },
551              
552             where the mailer is the C<Email::Sender::Transport::> type of transporter you want to use, like 'SMTP', 'SMTP::Persistent', 'Sendmail', 'Maildir', 'Mbox', or other types supported by Email::Sender::Simple.
553              
554             C<mailer_args> receives all the configuration options that might be required by the specified mailer.
555              
556             For example, for sending email with an SMTP host that require authentication, and listens to a non-standard port, you should use:
557              
558             mail_client => {
559             mailer => 'SMTP',
560             mailer_args => {
561             host => 'smtp.host.com',
562             port => 28,
563             username => 'the_user',
564             password => 'the_password',
565             },
566             },
567              
568             If you want to send email using an SMTP server that uses SSL, for example send an email with Gmail, use:
569              
570             mail_client => {
571             mailer => 'SMTP',
572             mailer_args => {
573             host => 'smtp.gmail.com',
574             #port => 465, #The port 465 is the default when using SSL, so it is not necessary.
575             username => 'the_user',
576             password => 'the_password',
577             ssl => 1,
578             ],
579             },
580              
581             If the parameter C<mail_client> is not specified, the default mailer that is used is sendmail.
582              
583             Starting with the version 0.10, the key C<mail_client> supports a new sub-key named C<live_on_error>. By default, if the email message can't be sent for different reasons, the module dies. If you set the key C<live_on_error> to true, the module doesn't die, but continues to run. This might be helpful if you try to send more email messages and if you are not interested if certain messages can't be sent.
584              
585             You can use:
586              
587             mail_client => {
588             mailer => 'SMTP',
589             mailer_args => {host => 'smtp.host.com'},
590             live_on_error => 1,
591             },
592              
593             The mailer_args key could have any other sub-keys, depending on the type of transport used. For example, the SMTP type of transport could have host, username, password, ssl and others. For more information look in the L<Email::Sender::Transport::SMTP|Email::Sender::Transport::SMTP> or other module which is used.
594              
595             The mailer_args key could also contain the C<to>, C<cc> and C<from> keys, which are used if you want to send the email message to addresses specified by them, and not to the addresses specified when creating the email message.
596              
597             In the following example, the email message is sent to good-email@host.com and not to fake-email@host.com:
598              
599             my $mail = Mail::Builder::Simple->new(
600             mail_client => {
601             mailer => 'SMTP',
602             mailer_args => {
603             host => 'smtp.host.com',
604             to => 'good-email@host.com',
605             },
606             },
607             );
608              
609             $mail->send(
610             to => 'fake-email@host.com',
611             from => 'me@host.com',
612             subject => 'The subject',
613             htmltext => '<h1>Hello</h1><p>The body of the message...</p>',
614             );
615              
616             =head2 template_args
617              
618             This parameter is optional.
619              
620             It is a hashref with all the arguments neede by the templating system you are using for creating the email body or the attachments.
621              
622             C<template_args> can receive any kind of parameters, depending on the parameters which are accepted by the templating system used. C<Mail::Builder::Simple> allows using more templating systems even for creating a single email message. If more templating systems are used to create an email message, all the templates will use the arguments from the hashref C<template_args> unless overwritten, as you will see.
623              
624             It could look like:
625              
626             template_args => {
627             INCLUDE_PATH => '/path/to/templates', #default "."
628             ENCODING => 'UTF-8', #The default is UTF-8 anyway
629             },
630              
631             =head2 template_vars
632              
633             This parameter is optional.
634              
635             It is a hashref with the pairs of variables from the templates and their values.
636              
637             An example:
638              
639             template_vars => {
640             name => 'Gil Bates',
641             preferences => ['pizza', 'yogurt', 'blondes'],
642             },
643              
644             The variables from C<template_vars> will be used by all the templates which are used for creating the email message, unless some of them are overwritten as you will see.
645              
646             The variables from template_args and template_vars should be defined before using them in a template. So for example if you want to send a message to more addressees and want to send template_vars to the send() method, you also need to send the template's parameters to send() method, because if you define the template earlier, in the new() method, the template won't see the template_vars.
647              
648             Examples:
649              
650             Don't do something like this:
651              
652             my $mail = Mail::Builder::Simple->new(
653             from => 'my@host.com',
654             subject => 'The subject',
655             htmltext => ['template.tt', ':TT'],
656             );
657            
658             $mail->send(
659             to => 'one@host.com',
660             template_vars => {name => 'Foo', age => 33},
661             );
662              
663             $mail->send(
664             to => 'two@host2.com',
665             template_vars => {name => 'Bar', age => 28},
666             );
667              
668             But do it like this:
669              
670             my $mail = Mail::Builder::Simple->new(
671             from => 'my@host.com',
672             subject => 'The subject',
673             );
674            
675             $mail->send(
676             to => 'one@host.com',
677             htmltext => ['template.tt', ':TT'],
678             template_vars => {name => 'Foo', age => 33},
679             );
680              
681             $mail->send(
682             to => 'two@host2.com',
683             htmltext => ['template.tt', ':TT'],
684             template_vars => {name => 'Bar', age => 28},
685             );
686              
687             =head2 email message fields
688              
689             These are the fields that create the email message. They are: C<from, to, cc, bcc, subject, plaintext, htmltext, attachment, image, priority, reply, organization, returnpath, sender, language, mailer>.
690              
691             There are many ways of using these fields, and I will explain them below.
692              
693             =head2 config_file
694              
695             This parameter is optional.
696              
697             It shows the path to a configuration file that holds some parameters you don't want to specify in each program.
698              
699             The configuration file can be any type of file supported by L<Config::Any|Config::Any>: Apache config style (Config::General), JSON, INI files, XML, YAML or perl code.
700              
701             Here is an example of a configuration file that uses L<Config::General|Config::General>:
702             (/home/user/email.conf)
703              
704             <mail_client>
705             mailer SMTP
706             <mailer_args>
707             host smtp.host.com
708             username user
709             password passwd
710             </mailer_args>
711             </mail_client>
712             from user@host.com
713              
714             This configuration file contains options not only for the mailer, but it also contains the message field C<From:> which wouldn't need to be specified when sending an email.
715              
716             Here is a program that sends an email using this configuration file:
717              
718             use Mail::Builder::Simple;
719              
720             my $mail = Mail::Builder::Simple->new(config_file => '/home/user/email.conf');
721              
722             $mail->send(
723             to => 'you@yourhost.com',
724             subject => 'The subject',
725             htmltext => '<h1>Hello</h1> How are you?',
726             );
727              
728             As all other parameters shown until now, C<config_file> can be sent to both C<new()> and C<send()> functions.
729              
730             =head2 other email message headers
731              
732             You might need to include in your message some headers which are not in the list shown above. You can also add them as separate parameters, but they need to be capitalised exactly how they should appear in the email message.
733              
734             These headers overwrite the previous set headers that have the same name and they can be sent as parameters only to the L</send> method, not to the L</new>.
735              
736             Here is an example of including the header C<X-My-Special-Header>:
737              
738             $mail->send(
739             to => 'you@yourhost.com',
740             subject => 'The subject',
741             plaintext => 'The body',
742             'X-My-Special-Header' => 'This is my header',
743             );
744              
745             =head1 Using the email message fields
746              
747             =head2 to, cc, bcc
748              
749             Here are a few ways of using the C<To:> field:
750              
751             As parameters to the C<new()> or C<send()> functions:
752              
753             Set a single email address for the C<To:> field:
754              
755             to => 'you@host.com',
756              
757             Set a single address and set the name that should be displayed in the C<To:> field:
758              
759             to => ['you@host.com', 'Your Name - with UTF-8 chars'],
760              
761             Set more email addresses for the C<To:> field in 2 ways:
762              
763             to => ['MORE', 'you@host.com', 'he@host2.com', 'she@host3.com'],
764             or
765             to => [['you@host.com'], ['he@host2.com'], ['she@host3.com']],
766              
767             Set more email addresses for the C<To:> field, and also set the names that should be displayed:
768              
769             to => [['you@host.com', 'Your Name'], ['he@host2.com', 'His Name']],
770              
771             or as a method to the C<Mail::Builder::Simple> object:
772              
773             Set an email address for the C<To:> field:
774              
775             $mail->to('you@host.com');
776              
777             Set an email address for the C<To:> field, and also set the name which is displayed:
778              
779             $mail->to('you@host.com', 'Your Name');
780              
781             Add the email address and the name which is displayed in the C<To:> field. You can repeat this for more times.
782              
783             $mail->to->add('you@host.com');
784             $mail->to->add('he@host2.com', 'His Name');
785              
786             You can set the C<CC:> or C<BCC:> fields of the message in the same way.
787              
788             =head2 from
789              
790             The C<From:> field can be set using:
791              
792             from => 'me@myhost.com',
793             or
794             from => ['me@myhost.com', 'My Name'],
795             or
796             $mail->from('me@myhost.com');
797             or
798             $mail->from('me@myhost.com', 'My Name');
799              
800             =head2 subject
801              
802             You can specify the subject field as:
803              
804             subject => 'The subject',
805             or
806             $mail->subject('The subject');
807              
808             =head2 plaintext, htmltext
809              
810             C<Mail::Builder::Simple> can create a plain-text message if you provide just the plaintext part, or a multipart message if you offer the htmltext also. You can even provide just the htmltext, and it will create the plaintext part automaticly.
811              
812             You can create the body of the message using:
813              
814             plaintext => "Hello,\n\nHow are you?",
815             htmltext => "<h1>Hello,</h1> <p>How are you?</p>",
816              
817             =head2 attachments
818              
819             The attachments can be added as parameters to C<new()> and C<send()> methods.
820              
821             Attach a file without specifying an alternative name and its Content-Type:
822              
823             attachment => 'file.pdf',
824              
825             Attach a file specifying an alternative name and its Content-Type:
826              
827             attachment => ['/path/to/file', 'filename.pdf', 'application/pdf'],
828              
829             Attach more file without specifying alternative names and Content-Type in 2 ways:
830              
831             attachment => ['MORE', 'file1.pdf', 'file2.doc'],
832             or
833             attachment => [['file1.pdf'], ['file2.doc'], ['file3.html']],
834              
835             Attach more files specifying their alternative names and Content-Type:
836              
837             attachment => [
838             ['file1', 'file1.pdf', 'application/pdf'],
839             ['file2', 'file2.pdf', 'application/pdf'],
840             ],
841              
842             or attach files using methods of the C<Mail::Builder::Simple> object. You can repeat this for more times:
843              
844             $mail->attachment->add('file1.pdf');
845             or
846             $mail->attachment->add('file', 'file.pdf', 'application/pdf');
847              
848             =head2 images
849              
850             C<Mail::Builder::Simple> allows attaching inline images that won't appear as attachments, but they will be displayed in the HTML part of the mail message.
851              
852             You can add them as parameters to the C<new()> or C<send()> functions.
853              
854             Add an inline image without specifying an alternative ID:
855              
856             image => 'image.png',
857              
858             Add an inline image and specify an alternative ID:
859              
860             image => ['/path/to/image.png', 'image_id'],
861              
862             Add more inline images without specifying an alternative ID:
863              
864             image => ['MORE', 'image1.png', 'image2.gif', 'image3.jpg'],
865             or
866             image => [['image1.png'], ['image2.gif'], ['image3.gif']],
867              
868             Add more inline images specifying an alternative ID:
869              
870             image => [
871             ['/path/to/image1.png', 'logo'],
872             ['image2.gif', 'img'],
873             ['image3.jpg', 'picture'],
874             ],
875              
876             or you can add them using methods of the C<Mail::Builder::Simple> object. You can repeat it for more times:
877              
878             $mail->image->add('image.png');
879             or
880             $mail->image->add('/path/to/image.png', 'logo');
881              
882             Only the .png, .jpg and .gif images can be attached as inline images.
883              
884             The ID of the image is used for displaying the image in the HTML part of the email message, using something like the following HTML element for the "logo" ID:
885              
886             <img src="cid:logo" alt="logo">
887              
888             If you don't provide an ID, one is automaticly generated and it will be the lowercase of the file name of the images, without the extension.
889              
890             =head1 Using templates
891              
892             C<Mail::Builder::Simple> allows to create the text and HTML body of the email message or the attachments using templates.
893              
894             When the value of the parameters C<plaintext>, C<htmltext> and C<attachment> is an arrayref and the last element of that arrayref begins with ":", it means that this field is created using a template. The type of template is specified in that last element of the array.
895              
896             =head2 Types of templates
897              
898             C<Mail::Builder::Simple> uses other plugin modules like L<Mail::Builder::Simple::TT|Mail::Builder::Simple::TT> and L<Mail::Builder::Simple::HTML::Template|Mail::Builder::Simple::HTML::Template> for creating the content using L<Template-Toolkit|Template> or L<HTML::Template|HTML::Template>.
899              
900             If you want to create the content using a templating system for which there isn't a plugin created yet, you can create that plugin. It is pretty simple.
901              
902             The templates that can be used for the moment are:
903              
904             Scalar
905             TT
906             TT-scalar
907             HTML::Template
908             HTML::Template-scalar
909              
910             Here are a few examples for creating a message plain text body using templates:
911              
912             # Create the plain text part of the email message using the TT template file "template.tt"
913            
914             my $mail = Mail::Builder::Simple->new;
915             $mail->send(
916             from => 'me@myhost.com',
917             to => 'you@host.com',
918             subject => 'The subject',
919             plaintext => ['template.tt', ':TT'],
920             template_args => {
921             INCLUDE_PATH => '/path/to/templates',
922             },
923             template_vars => {
924             name => 'My Name',
925             age => 20,
926             },
927             );
928              
929             and the template file in /path/to/templates/template.tt could contain:
930              
931             Hello [% name %],
932             My age is [% age %].
933              
934             # Create the plain text part of the email message using a TT template from a scalar variable
935            
936             my $template = <<EOF;
937             Hello [% name %],
938             My age is [% age %].
939             EOF
940              
941             my $mail = Mail::Builder::Simple->new;
942             $mail->send(
943             from => 'me@myhost.com',
944             to => 'you@host.com',
945             subject => 'The subject',
946             plaintext => [$template, ':TT-scalar'],
947             template_vars => {
948             name => 'My Name',
949             age => 20,
950             },
951             );
952              
953             # Create the plain text part of the email message using L<HTML::Template|HTML::Template> from a template file:
954              
955             plaintext => ['template.tmpl', ':HTML::Template'],
956              
957             # Create the plain text part of the email message using L<HTML::Template|HTML::Template> from a template from a scalar variable
958              
959             plaintext => [$template_content, ':HTML::Template-scalar'],
960            
961             The HTML part of the email message can be created in exactly the same way.
962              
963             # Add an attachment created from a template file using TT:
964            
965             attachment => ['template.tt', 'generated_file_name.html', 'text/html', ':TT'],
966            
967             # Add an attachment created from a TT template from a scalar variable:
968            
969             attachment => [$template_content, 'generated_file_name.txt', 'text/plain', ':TT-scalar'],
970            
971             # Add an attachment created from a template file using L<HTML::Template|HTML::Template>:
972            
973             attachment => ['template.tmpl', 'generated_file_name.txt', 'text/plain', ':HTML::Template'],
974            
975             # Add an attachment created from a template from a scalar variable using L<HTML::Template|HTML::Template>:
976            
977             attachment => [$template_content, 'generated_file_name.html', 'text/html', ':HTML::Template-scalar'],
978              
979             # Add an attachment from a scalar variable, without using any templating system:
980            
981             attachment => [$file_content, 'generated_file_name.html', 'text/html', ':Scalar'],
982            
983             Using the ":Scalar" as the last element of the arrayref makes it possible to create any type of file on the fly and add it as attachment to an email message. You can also add any type of file using templates, if the templating system used can create the type of file you want to add.
984              
985             =head2 Advanced use of templates
986              
987             If an email message should be created using more than a single templating system, all the templates can share the arguments from the C<template_args> hashref. For example if both L<Template-Toolkit|Template> and L<HTML::Template|HTML::Template> are used and we want to specify the path to the directory with templates, the C<template_args> parameter could include:
988              
989             template_args => {
990             INCLUDE_PATH => '/path/to/TT/templates',
991             path => '/path/to/HTML-Template/templates',
992             },
993            
994             This is possible because the arguments used by these 2 templating systems are in this case different (C<path> and C<INCLUDE_PATH>). But if 2 templating systems that need to use the same argument are used and if that parameter should have a different value for each one, than it won't be possible to share all the parameters from C<template_args>.
995            
996             In that case, we could add a new element in the arrayref by specifying the C<template_args> hashref separately for each template:
997            
998             plaintext => ['template.tt', {INCLUDE_PATH => '/path/to/TT/templates'}, ':TT'],
999             htmltext => ['template.tmpl', {path => '/path/to/HT/templates'}, ':HTML::Template'],
1000             attachment => ['template.tt', 'file.html', 'text/html', {INCLUDE_PATH => '/another/dir'}, ':TT'],
1001            
1002             As you have seen, the C<template_args> hashref for each template is added as a penultimate element of the arrayref and it can contain the same elements as the main C<template_args> parameter.
1003              
1004             The variables from the C<template_args> hashref overwrite the variables defined in the main C<template_args> hashref if it is used.
1005              
1006             If more templates are used for creating an email message, possibly using more templating systems, all of the templates get the variables specified in the C<template_vars> hashref.
1007              
1008             However, if 2 or more templates use a value with the same name, but that variable should have different values in different templates, you can also add a C<template_vars> hashref for each template, and overwrite the variables specified in the main C<template_vars> hashref.
1009              
1010             This C<template_vars> hashref which is specified for each template is added in the arrayref before the C<template_args> hashref. If you need to add just a local C<template_vars> hashref but not a C<template_args> one, you need to use an empty hashref - {} in place of the C<template_args> hashref, like:
1011              
1012             plaintext => [
1013             'template.tt',
1014             {name => 'Your Name', age => 20},
1015             {INCLUDE_PATH => '/path/to/TT/templates'},
1016             ':TT'
1017             ],
1018              
1019             htmltext => [
1020             'template.tmpl',
1021             {name => 'Another name', address => '...'},
1022             {},
1023             ':HTML::Template'
1024             ],
1025              
1026             attachment => [
1027             'template.tt',
1028             'file.html',
1029             'text/html',
1030             {name => 'Something Else'},
1031             {INCLUDE_PATH => '/another/dir'},
1032             ':TT'
1033             ],
1034              
1035             =head1 Using the module
1036              
1037             After using the C<send()> function, the C<To:>, C<CC:> and C<BCC:> fields are cleared from the Mail::Builder::Simple object, so you can use the same object to send the same email to other recipients.
1038              
1039             Here is an example:
1040              
1041             my $mail = Mail::Builder::Simple->new(from => 'me@myhost.com');
1042              
1043             $mail->send(
1044             to => 'you@host.com',
1045             subject => 'The subject',
1046             plaintext => 'The body of the message',
1047             );
1048              
1049             $mail->send(to => 'he@host2.com');
1050             $mail->send(to => 'she@host3.com');
1051              
1052             The last 2 lines sent the message previously created. If you want to create an entirely new message, you should use the method C<new()> again.
1053              
1054             =head1 DEPENDENCIES
1055              
1056             L<Mail::Builder|Mail::Builder>, L<Email::Sender::Simple|Email::Sender::Simple>, L<Email::Valid|Email::Valid>, L<Module::Load|Module::Load>, L<Config::Any|Config::Any>
1057              
1058             =head1 INCOMPATIBILITIES
1059              
1060             Starting with the version 0.10, the module tries to keep the compatibility with the programs that were using previous versions of this module, because beginning with this version, the email messages will be sent using the module L<Email::Sender::Simple|Email::Sender::Simple> and not L<Email::Send|Email::Send> as before.
1061              
1062             The possible incompatibilities could appear only in the way you use the C<mail_client> key. In the previous versions, you needed to use something like:
1063              
1064             mail_client => {
1065             mailer => 'SMTP',
1066             mailer_args => [Host => 'smtp.host.com'],
1067             },
1068              
1069             This was the promoted style, although it was also possible to use:
1070              
1071             mail_client => {
1072             mailer => 'SMTP',
1073             mailer_args => {Host => 'smtp.host.com'},
1074             },
1075              
1076             So you were also able to use a hashref instead of an arrayref for the mailer_args key.
1077              
1078             Now the promoted style is the one that uses a hashref, although it is also possible to use the arrayref style if you want, so from this point of view it shouldn't be any incompatibilities.
1079              
1080             As you might have seen, the SMTP host is now specified using the "host" key and not "Host" like in the previous versions. The "host" key is the one that should be used in the new programs, but the old "Host" key is also working.
1081              
1082             If you wanted to access an SMTP server on a non-standard port in older versions, you needed to provide it in the form host:port. Now there is a key named "port" that you can use instead, like in the following example:
1083              
1084             mail_client => {
1085             mailer => 'SMTP',
1086             mailer_args => {host => 'smtp.host.com', port => 28},
1087             },
1088              
1089             But you can still use the notation host: port like before if you want, as in:
1090              
1091             mailer_args => [Host => 'smtp.host.com:28'],
1092              
1093             Some of the mailers that could be used with the older versions of this module like L<Email::Send::Gmail|Email::Send::Gmail> can't be used anymore but most of the features offered by them are also offered by similar C<Email::Sender::Transport::> modules.
1094              
1095             If you found an untreated incompatibility, please tell me.
1096              
1097             =head1 BUGS AND LIMITATIONS
1098              
1099             If you find some, please tell me.
1100              
1101             =head1 DIAGNOSTICS
1102              
1103             =head1 SEE ALSO
1104              
1105             L<Mail::Builder|Mail::Builder>, L<Email::Sender::Simple|Email::Sender::Simple>, L<Template-Toolkit|Template>, L<HTML::Template|HTML::Template>, L<Config::Any|Config::Any>
1106              
1107             =head1 AUTHOR
1108              
1109             Octavian Rasnita <orasnita@gmail.com>
1110              
1111             =head1 LICENSE AND COPYRIGHT
1112              
1113             This program is free software; you can redistribute it and/or modify it under
1114             the same terms as Perl itself.
1115              
1116             =cut