File Coverage

blib/lib/Mail/Builder.pm
Criterion Covered Total %
statement 166 194 85.5
branch 70 84 83.3
condition 44 72 61.1
subroutine 24 26 92.3
pod 3 4 75.0
total 307 380 80.7


line stmt bran cond sub pod time code
1             # ============================================================================
2             package Mail::Builder;
3             # ============================================================================
4              
5 8     8   691429 use namespace::autoclean;
  8         117851  
  8         36  
6 8     8   4383 use Moose;
  8         3350066  
  8         64  
7              
8             our $VERSION = '2.13';
9             our $AUTHORITY = 'cpan:MAROS';
10              
11 8     8   63748 use Mail::Builder::TypeConstraints;
  8         30  
  8         370  
12 8     8   4400 use Mail::Builder::Utils;
  8         28  
  8         235  
13              
14 8     8   72 use Carp;
  8         16  
  8         367  
15              
16 8     8   4448 use MIME::Entity;
  8         458262  
  8         291  
17 8     8   76 use Class::Load;
  8         19  
  8         274  
18              
19 8     8   3415 use Email::MessageID;
  8         4565  
  8         210  
20 8     8   3282 use Email::Address;
  8         167414  
  8         481  
21              
22 8     8   3818 use Mail::Builder::Address;
  8         45  
  8         811  
23 8     8   4789 use Mail::Builder::Attachment;
  8         36  
  8         362  
24 8     8   4536 use Mail::Builder::Image;
  8         38  
  8         346  
25 8     8   4337 use Mail::Builder::List;
  8         35  
  8         19209  
26              
27             has 'plaintext' => (
28             is => 'rw',
29             isa => 'Str',
30             predicate => 'has_plaintext',
31             clearer => 'clear_plaintext',
32             trigger => \&_set_autotext_plaintext,
33             );
34              
35             has 'htmltext' => (
36             is => 'rw',
37             isa => 'Str',
38             predicate => 'has_htmltext',
39             clearer => 'clear_htmltext',
40             trigger => \&_generate_plaintext,
41             );
42              
43             has 'date' => (
44             is => 'rw',
45             isa => 'Mail::Builder::Type::Date',
46             lazy_build => 1,
47             clearer => 'clear_date',
48             );
49              
50             has 'subject' => (
51             is => 'rw',
52             isa => 'Str',
53             #required => 1,
54             predicate => 'has_subject',
55             );
56              
57             has 'organization' => (
58             is => 'rw',
59             isa => 'Str',
60             predicate => 'has_organization',
61             clearer => 'clear_organization',
62             );
63              
64             has 'priority' => (
65             is => 'rw',
66             isa => 'Mail::Builder::Type::Priority',
67             default => 3,
68             );
69              
70             has 'language' => (
71             is => 'rw',
72             isa => 'Str',
73             clearer => 'clear_language',
74             predicate => 'has_language',
75             );
76              
77             has 'mailer' => (
78             is => 'rw',
79             isa => 'Str',
80             required => 1,
81             default => "Mail::Builder v$VERSION",
82             );
83              
84             has 'autotext' => (
85             is => 'rw',
86             isa => 'Bool',
87             default => 1,
88             );
89              
90             has 'messageid' => (
91             is => 'ro',
92             isa => 'Email::MessageID',
93             clearer => 'clear_messageid',
94             predicate => 'has_messageid',
95             );
96              
97             has '_plaintext_autotext' => (
98             is => 'rw',
99             isa => 'Bool',
100             default => 0,
101             );
102              
103             has '_boundary' => (
104             is => 'rw',
105             isa => 'Int',
106             default => 1,
107             );
108              
109             foreach my $field (qw(from reply returnpath sender)) {
110             has $field => (
111             is => 'rw',
112             isa => 'Mail::Builder::Type::Address',
113             coerce => 1,
114             predicate => 'has_'.$field,
115             clearer => 'clear'.$field,
116             );
117             around $field => \&_address_accessor;
118             }
119              
120             has [qw(to cc bcc)] => (
121             is => 'rw',
122             isa => 'Mail::Builder::Type::AddressList',
123             required => 1,
124             coerce => 1,
125             default => sub { return Mail::Builder::List->new( type => 'Mail::Builder::Address' ) },
126             );
127              
128             has 'attachment' => (
129             is => 'rw',
130             isa => 'Mail::Builder::Type::AttachmentList',
131             coerce => 1,
132             required => 1,
133             default => sub { Mail::Builder::List->new( type => 'Mail::Builder::Attachment' ) }
134             );
135              
136             has 'image' => (
137             is => 'rw',
138             isa => 'Mail::Builder::Type::ImageList',
139             coerce => 1,
140             required => 1,
141             default => sub { Mail::Builder::List->new( type => 'Mail::Builder::Image' ) }
142             );
143              
144             sub _address_accessor {
145 34     34   2086 my ($method,$self,@params) = @_;
146              
147 34 100       109 if (scalar @params) {
148 8 100 100     68 if (scalar @params == 1
      100        
149             && blessed($params[0])
150             && $params[0]->isa('Mail::Builder::Address')) {
151 1         27 return $method->($self,$params[0]);
152             } else {
153 7         262 return $method->($self,Mail::Builder::Address->new(@params));
154             }
155             } else {
156 26         678 return $method->($self);
157             }
158             }
159              
160             sub _set_raw {
161 5     5   20 my ($self,$key,$value) = @_;
162 5         46 my $attr = __PACKAGE__->meta->get_attribute($key);
163 5         389 $attr->set_raw_value($self,$value);
164 5         373 return;
165             }
166              
167             sub _generate_plaintext {
168 1     1   2 my ($self,$htmltext) = @_;
169 1   33     4 $htmltext ||= $self->htmltext;
170              
171 1 50 33     24 if ($self->autotext
      33        
172             && ($self->_plaintext_autotext == 1 || ! $self->has_plaintext)) {
173 1         7 Class::Load::load_class('HTML::TreeBuilder');
174              
175 1         26107 my $html_tree = HTML::TreeBuilder->new_from_content($self->htmltext);
176             # Only use the body
177 1         10682 my $html_body = $html_tree->find('body');
178             # And now convert all elements
179 1         57 my $plaintext = $self->_convert_text($html_body);
180             # Store in plaintext accessor and set _plaintext_autotext flag
181 1         8 $self->_set_raw('plaintext',$plaintext);
182 1         49 $self->_plaintext_autotext(1);
183             }
184 1         26 return;
185             }
186              
187             sub _set_autotext_plaintext {
188 2     2   12 my ($self,$plaintext) = @_;
189 2         58 $self->_plaintext_autotext(0);
190 2         46 return;
191             }
192              
193             sub _convert_text {
194 26     26   66 my ($self,$html_element,$params) = @_;
195              
196 26         42 my $plain_text = q[];
197 26   100     90 $params ||= {};
198              
199             # Loop all children of the HTML element
200 26         73 foreach my $html_content ($html_element->content_list) {
201             # HTML element
202 44 100 66     359 if (ref($html_content)
203             && $html_content->isa('HTML::Element')) {
204 26         65 my $html_tagname = $html_content->tag;
205 26 100 66     546 if ($html_tagname eq 'i' || $html_tagname eq 'em') {
    100 66        
    100 100        
    50 66        
    50 66        
    100 66        
    100 66        
    100 66        
    100 66        
    100 33        
    100 33        
    100 33        
    100          
    50          
    50          
206 1         5 $plain_text .= '_'.$self->_convert_text($html_content,$params).'_';
207             } elsif ($html_tagname =~ m/^h\d$/) {
208 1         10 $plain_text .= '=='.$self->_convert_text($html_content,$params).qq[\n];
209             } elsif ($html_tagname eq 'strong' || $html_tagname eq 'b') {
210 1         5 $plain_text .= '*'.$self->_convert_text($html_content,$params).'*';
211             } elsif ($html_tagname eq 'hr') {
212 0         0 $plain_text .= qq[\n---------------------------------------------------------\n];
213             } elsif ($html_tagname eq 'br') {
214 0         0 $plain_text .= qq[\n];
215             } elsif ($html_tagname eq 'ul' || $html_tagname eq 'ol') {
216 2         4 my $count_old = $params->{count};
217 2 100       6 $params->{count} = ($html_tagname eq 'ol') ? 1:'*';
218 2         26 $plain_text .= qq[\n].$self->_convert_text($html_content,$params).qq[\n\n];
219 2 50       6 if (defined $count_old) {
220 0         0 $params->{count} = $count_old;
221             } else {
222 2         7 delete $params->{count};
223             }
224             } elsif ($html_tagname eq 'div' || $html_tagname eq 'p') {
225 2         9 $plain_text .= $self->_convert_text($html_content,$params).qq[\n\n];
226             } elsif ($html_tagname eq 'table') {
227 1         753 require Text::Table; # Load Text::Table lazily
228              
229 1         12775 my $table_old = $params->{table};
230 1         6 $params->{table} = Text::Table->new();
231 1         169 $self->_convert_text($html_content,$params);
232 1         4 $params->{table}->body_rule('-','+');
233 1         6994 $params->{table}->rule('-','+');
234 1         231 $plain_text .= qq[\n].$params->{table}->rule('-').$params->{table}.$params->{table}->rule('-').qq[\n];
235 1 50       434 if (defined $table_old) {
236 0         0 $params->{table} = $table_old;
237             } else {
238 1         27 delete $params->{table};
239             }
240             } elsif ($html_tagname eq 'tr'
241             && defined $params->{table}) {
242 3         9 my $tablerow_old = $params->{tablerow};
243 3         8 $params->{tablerow} = [];
244 3         11 $self->_convert_text($html_content,$params);
245 3         5 $params->{table}->add(@{$params->{tablerow}});
  3         11  
246 3 50       792 if (defined $tablerow_old) {
247 0         0 $params->{tablerow} = $tablerow_old;
248             } else {
249 3         13 delete $params->{tablerow};
250             }
251             } elsif (($html_tagname eq 'td' || $html_tagname eq 'th') && $params->{tablerow}) {
252 8         12 push @{$params->{tablerow}},$self->_convert_text($html_content,$params);
  8         19  
253 8 100       25 if ($html_content->attr('colspan')) {
254 1   50     23 my $colspan = $html_content->attr('colspan') || 1;
255 1         15 $colspan --;
256 1         5 push @{$params->{tablerow}},''
257 1         6 for (1..$colspan);
258             }
259             } elsif ($html_tagname eq 'img' && $html_content->attr('alt')) {
260 1         19 $plain_text .= '['.$html_content->attr('alt').']';
261             } elsif ($html_tagname eq 'a' && $html_content->attr('href')) {
262 1         17 $plain_text .= '['.$html_content->attr('href').' '.$self->_convert_text($html_content,$params).']';
263             } elsif ($html_tagname eq 'li') {
264 4         8 $plain_text .= qq[\n\t];
265 4   50     13 $params->{count} ||= '*';
266 4 100       19 if ($params->{count} eq '*') {
    50          
267 2         5 $plain_text .= '*';
268             } elsif ($params->{count} =~ /^\d+$/) {
269 2         5 $plain_text .= $params->{count}.'.';
270 2         18 $params->{count} ++;
271             }
272 4         20 $plain_text .= q[ ].$self->_convert_text($html_content);
273             } elsif ($html_tagname eq 'pre') {
274 0         0 $params->{pre} = 1;
275 0         0 $plain_text .= qq[\n].$self->_convert_text($html_content,$params).qq[\n\n];
276 0         0 delete $params->{pre};
277             } elsif ($html_tagname eq 'head'
278             || $html_tagname eq 'script'
279             || $html_tagname eq 'frameset'
280             || $html_tagname eq 'style') {
281 0         0 next;
282             } else {
283 1         4 $plain_text .= $self->_convert_text($html_content,$params);
284             }
285             # CDATA
286             } else {
287 18 50       43 unless ($params->{pre}) {
288 18         48 $html_element =~ s/(\n|\n)//g;
289 18         53 $html_element =~ s/(\t|\n)/ /g;
290             }
291 18         45 $plain_text .= $html_content;
292             }
293             }
294              
295 26         98 return $plain_text;
296             }
297              
298             sub _get_boundary {
299 3     3   7 my ($self) = @_;
300 3         73 my $boundary = $self->_boundary( $self->_boundary + 1 );
301 3         44 return sprintf('----_=_NextPart_%04i_%lx',$boundary,time);
302             }
303              
304             sub charset {
305 0     0 0 0 warn('DEPRECATED: The charset accessor has been removed.');
306 0         0 return;
307             }
308              
309             sub purge_cache {
310 0     0 1 0 my ($self) = @_;
311              
312 0         0 $self->clear_messageid;
313 0         0 $self->clear_date;
314              
315             # Remove plaintext if it has been derived form htmltext
316 0 0       0 if ($self->_plaintext_autotext) {
317 0         0 $self->clear_plaintext;
318             }
319              
320             # Empty attachment images
321 0         0 foreach my $list (qw(attachment image)) {
322 0         0 foreach my $element ($self->$list->list) {
323 0         0 $element->clear_cache;
324             }
325             }
326 0         0 return 1;
327             }
328              
329             sub _build_text {
330 4     4   29 my ($self,%mime_params) = @_;
331              
332             # Build plaintext message from HTML
333 4 50 66     123 if ($self->has_htmltext
      33        
334             && ! $self->has_plaintext
335             && $self->autotext) {
336             # Parse HTML tree. Load HTML::TreeBuilder lazily
337 0         0 require HTML::TreeBuilder;
338              
339 0         0 my $html_tree = HTML::TreeBuilder->new_from_content($self->html_text);
340             # Only use the body
341 0         0 my $html_body = $html_tree->find('body');
342             # And now convert all elements
343 0         0 $self->plaintext(_convert_text($html_body));
344             }
345              
346 4         9 my $mime_part;
347              
348             # We have HTML and plaintext
349 4 100 66     94 if ($self->has_htmltext
350             && $self->has_plaintext) {
351              
352             # Build multipart/alternative envelope for HTML and plaintext
353 2         14 $mime_part = MIME::Entity->build(
354             %mime_params,
355             Type => q[multipart/alternative],
356             Boundary => $self->_get_boundary(),
357             Encoding => 'binary',
358             );
359              
360             # Add the plaintext entity first
361 2         6986 $mime_part->add_part(MIME::Entity->build(
362             Top => 0,
363             Type => qq[text/plain; charset="utf-8"],
364             Data => $self->plaintext,
365             Encoding => 'quoted-printable',
366             ));
367              
368             # Add the html entity (the last entity is prefered in multipart/alternative context)
369 2         1711 $mime_part->add_part($self->_build_html(Top => 0));
370             # We only have plaintext
371             } else {
372 2         52 $mime_part = MIME::Entity->build(
373             %mime_params,
374             Type => qq[text/plain; charset="utf-8"],
375             Data => $self->plaintext,
376             Encoding => 'quoted-printable',
377             );
378             }
379              
380 4         3967 return $mime_part;
381             }
382              
383             sub _build_html {
384 2     2   7 my ($self,%mime_params) = @_;
385              
386 2         6 my $mime_part;
387              
388             # We have inline images
389 2 50       66 if ($self->image->length) {
390             # So we need a multipart/related envelope first
391 0         0 $mime_part = MIME::Entity->build(
392             %mime_params,
393             Type => q[multipart/related],
394             Boundary => $self->_get_boundary(),
395             Encoding => 'binary',
396             );
397             # Add the html body
398 0         0 $mime_part->add_part(MIME::Entity->build(
399             Top => 0,
400             Type => qq[text/html; charset="utf-8"],
401             Data => $self->htmltext,
402             Encoding => 'quoted-printable',
403             ));
404             # And now all the inline images
405 0         0 foreach ($self->image->list) {
406 0         0 $mime_part->add_part($_->serialize);
407             }
408             # We don't have any inline images
409             } else {
410 2         56 $mime_part = MIME::Entity->build(
411             %mime_params,
412             Type => qq[text/html; charset="utf-8"],
413             Data => $self->htmltext,
414             Encoding => 'quoted-printable',
415             );
416             }
417 2         1619 return $mime_part;
418             }
419              
420             sub _build_date {
421 2     2   11 my ($self) = @_;
422 2         14 Class::Load::load_class('Email::Date::Format');
423 2         2506 return Email::Date::Format::email_date();
424             }
425              
426              
427             sub build_message {
428 8     8 1 174 my ($self) = @_;
429              
430 8 100       254 croak(q[Recipient address missing])
431             unless ($self->to->length());
432 7 100       175 croak(q[From address missing])
433             unless ($self->has_from);
434 6 100       152 croak(q[e-mail subject missing])
435             unless ($self->has_subject);
436 5 100 66     133 croak(q[e-mail content missing])
437             unless ($self->has_plaintext || $self->has_htmltext);
438              
439 4         67 my $messageid = Email::MessageID->new;
440 4         3640 $self->_set_raw('messageid',$messageid);
441              
442             # Set header fields
443 4         24 my %email_header = (
444             'Top' => 1,
445             'From' => $self->from->serialize,
446             'To' => $self->to->join(','),
447             'Cc' => $self->cc->join(','),
448             'Bcc' => $self->bcc->join(','),
449             'Date' => $self->date,
450             'Subject' => Mail::Builder::Utils::encode_mime($self->subject),
451             'Message-ID' => $messageid->in_brackets(),
452             'X-Priority' => $self->priority,
453             'X-Mailer' => Mail::Builder::Utils::encode_mime($self->mailer),
454             );
455              
456             # Set reply address
457 4 100       126 if ($self->has_reply) {
458 3         15 $email_header{'Reply-To'} = $self->reply->serialize;
459             }
460              
461             # Set sender address
462 4 100       108 if ($self->has_sender) {
463 3         15 $email_header{'Sender'} = $self->sender->serialize;
464             }
465              
466             # Set language
467 4 100       108 if ($self->has_language) {
468 3         68 $email_header{'Content-language'} = $self->language;
469             }
470              
471             # Set return path
472 4 100       105 if ($self->has_returnpath) {
    100          
473 2         10 $email_header{'Return-Path'} = $self->returnpath->address();
474             } elsif ($self->has_reply) {
475 1         5 $email_header{'Return-Path'} = $self->reply->address();
476             } else {
477 1         5 $email_header{'Return-Path'} = $self->from->address();
478             }
479              
480             # Set organizsation
481 4 100       109 if ($self->has_organization) {
482 3         65 $email_header{'Organization'} = Mail::Builder::Utils::encode_mime($self->organization);
483             }
484              
485             # Build e-mail entity ...
486 4         11 my $mime_entity;
487              
488             # ... with attachments
489 4 100       92 if ($self->attachment->length()) {
490 1         9 $mime_entity = MIME::Entity->build(
491             %email_header,
492             Type => 'multipart/mixed',
493             Boundary => $self->_get_boundary(),
494             Encoding => 'binary',
495             );
496 1         3557 $mime_entity->add_part($self->_build_text(Top => 0));
497 1         44 foreach my $attachment ($self->attachment->list()) {
498 1         9 $mime_entity->add_part($attachment->serialize());
499             }
500             # ... without attachments
501             } else {
502 3         37 $mime_entity = $self->_build_text(%email_header);
503             }
504              
505 4         73 return $mime_entity;
506             }
507              
508             sub stringify {
509 1     1 1 9390 my $obj = shift;
510 1         5 return $obj->build_message->stringify;
511             }
512              
513             __PACKAGE__->meta->make_immutable;
514              
515             =encoding utf8
516              
517             =head1 NAME
518              
519             Mail::Builder - Easily create plaintext/html e-mail messages with attachments
520             and inline images
521              
522             =head1 SYNOPSIS
523              
524             use Mail::Builder;
525            
526             my $mail = Mail::Builder->new({
527             subject => 'Party at Sam\'s place',
528             from => 'mightypirate@meele-island.mq',
529             htmltext => '<h1>Party invitation</h1> ... ',
530             attachment=> '/path/to/direction_samandmax.pdf',
531             });
532            
533             # Alter from name
534             $mail->from->name('Guybrush Threepwood');
535             # Set recipent
536             $mail->to('manuel.calavera@dod.mx','Manuel Calavera');
537             # Add one more recipient
538             $mail->to->add('glotis@dod.mx');
539            
540             # Send it with your favourite module (e.g. Email::Send)
541             my $mailer = Email::Send->new({mailer => 'Sendmail'})->send($mail->stringify);
542            
543             # Or mess with MIME::Entity objects
544             my $mime = $mail->build_message;
545             $mime-> ....
546              
547             =head1 DESCRIPTION
548              
549             This module helps you to build correct e-mails with attachments, inline
550             images, multiple recipients, ... without having to worry about the underlying
551             MIME and encoding issues. Mail::Builder relies heavily on the L<MIME::Entity>
552             module from the L<MIME::Tools> distribution.
553              
554             The module will create the correct MIME bodies, headers and containers
555             (multipart/mixed, multipart/related, multipart/alternative) depending on if
556             you use attachments, HTML text and inline images.
557              
558             Furthermore it will encode non-ascii header data and autogenerate plaintext
559             messages (if you don't provide it yourself or disable the L<autotext> option)
560             from html content.
561              
562             Addresses, attachments and inline images are handled as objects by helper
563             classes:
564              
565             =over
566              
567             =item * L<Mail::Builder::Address>
568              
569             Stores an e-mail address and a display name.
570              
571             =item * Attachments: L<Mail::Builder::Attachment>
572              
573             This class manages attachments which can be created either from files in the
574             filesystem, filehandles or from data in memory.
575              
576             =item * Inline images:L<Mail::Builder::Image>
577              
578             The Mail::Builder::Image class manages images that should be displayed in the
579             html e-mail body. (E<lt>img src="cid:imageid" /E<gt>)
580              
581             =item * L<Mail::Builder::List>
582              
583             Helper class for handling list of varoius items (i.e. recipient lists,
584             attachment lists and image lists)
585              
586             =back
587              
588             =head1 METHODS
589              
590             =head2 Constructors
591              
592             =head3 new
593              
594             This is a simple constructor. It accepts all defined acccessors as a Hash or
595             HashRef.
596              
597             =head2 Public methods
598              
599             =head3 stringify
600              
601             Returns the e-mail message as a string. This string can be passed to modules
602             like L<Email::Send>.
603              
604             This method is just a shortcut to C<$mb-E<gt>build_message-E<gt>stringify>
605              
606             =head3 build_message
607              
608             my $entity = $mb->build_message();
609            
610             # Print the entire message:
611             $entity->print(\*STDOUT);
612            
613             # Stringify the entire message:
614             print $entity->stringify;
615              
616             Returns the e-mail message as a L<MIME::Entity> object. You can mess around
617             with the object, change parts, ... as you wish.
618              
619             Every time you call build_message the MIME::Entity object will be created,
620             which can take some time if you are sending bulk e-mails. In
621             order to increase the processing speed L<Mail::Builder::Attachment> and
622             L<Mail::Builder::Image> entities will be cached and only rebuilt if something
623             has changed.
624              
625             Each call to this method also changes the C<messageid>.
626              
627             =head3 purge_cache
628              
629             Emptys the attachment and image cache and removes the plaintext text if it has
630             been autogenerated from html text. Also resets the Message ID and date header.
631              
632             =head2 Accessors
633              
634             =head3 from, returnpath, reply, sender
635              
636             These accessors set/return the from, sender and reply address as well as the
637             returnpath for bounced messages.
638              
639             $obj->from(EMAIL[,NAME[,COMMENT]])
640             OR
641             $obj->from(Mail::Builder::Address)
642             OR
643             $obj->from(Email::Address)
644             OR
645             $obj->from({
646             email => EMAIL,
647             [name => NAME,]
648             [comment => COMMENT,]
649             });
650              
651             This accessor always returns a Mail::Builder::Address object.
652              
653             To change the attribute value you can either supply
654              
655             =over
656              
657             =item * a L<Mail::Builder::Address> object
658              
659             =item * an L<Email::Address> object
660              
661             =item * a list containing an e-mail address and optionally name and comment
662              
663             =item * a HashRef that will be passed to the L<Mail::Builder::Address>
664             constructor (Keys: email, name and comment)
665              
666             =back
667              
668             The presence of a value can be checked with the C<has_from>, C<has_reply>,
669             C<has_reply> and C<has_sender> methods.
670              
671             Values can be cleared with the C<clear_from>, C<clear_reply>,
672             C<clear_reply> and C<clear_sender> methods.
673              
674             L<Email::Valid> options may be changed by setting the appropriate values
675             in the %Mail::Builder::TypeConstraints::EMAILVALID hash.
676              
677             Eg. if you want to disable the check for valid TLDs you can set the 'tldcheck'
678             option (without dashes 'tldcheck' and not '-tldcheck'):
679              
680             $Mail::Builder::TypeConstraints::EMAILVALID{tldcheck} = 0;
681              
682             =head3 to, cc, bcc
683              
684             $obj->to(Mail::Builder::List)
685             OR
686             $obj->to(Mail::Builder::Address)
687             OR
688             $obj->to(Email::Address)
689             OR
690             $obj->to(EMAIL[,NAME[,COMMENT]])
691             OR
692             $obj->to({
693             email => EMAIL,
694             [name => NAME,]
695             [comment => COMMENT,]
696             })
697             OR
698             $obj->to([
699             Mail::Builder::Address | Email::Address | HashRef | EMAIL
700             ])
701              
702             This accessor always returns a L<Mail::Builder::List> object containing
703             L<Mail::Builder::Address> objects.
704              
705             To alter the values you can either
706              
707             =over
708              
709             =item * Manipulate the L<Mail::Builder::List> object (add, remove, ...)
710              
711             =item * Supply a L<Mail::Builder::Address> object. This will reset the current
712             list and add the object to the list.
713              
714             =item * Supply a L<Mail::Builder::List> object. The list object replaces the
715             old one if the list types matches.
716              
717             =item * Scalar and HashRef values will be passed to
718             C<Mail::Builder::Address-E<gt>new>. The returned object will be added to the
719             object list.
720              
721             =item * Supply a L<Email::Address> object. This will reset the current
722             list, generate a L<Mail::Builder::Address> object and add it to the list.
723              
724             =back
725              
726             The L<Mail::Builder::List> package provides some basic methods for
727             manipulating the list of recipients. e.g.
728              
729             $obj->to->add(EMAIL[,NAME[,COMMENT]])
730             OR
731             $obj->to->add(Mail::Builder::Address)
732             OR
733             $obj->to->add(Email::Address)
734              
735             =head3 date
736              
737             e-mail date header. Accepts/returns a RFC2822 date string. Also accepts a
738             DateTime object or epoch integer. Will be autogenerated if not provided.
739             C<clear_date> can be used to reset the date accessor.
740              
741             =head3 language
742              
743             e-mail text language. Additionally the C<has_language> and C<clear_language>
744             methods can be used to check respectively clear the value.
745              
746             =head3 messageid
747              
748             Message ID of the e-mail as a L<Email::MessageID> object. Read only and
749             available only after the C<build_message> or C<stringify> methods have been
750             called. Each call to C<build_message> or C<stringify> changes the message ID.
751             The C<has_messageid> and C<clear_messageid> methods can be used to check
752             respectively clear the value.
753              
754             =head3 organization
755              
756             Accessor for the name of the sender's organisation. This header field is not
757             part of the RFC 4021, however supported by many mailer applications.
758             Additionally the C<has_organization> and C<clear_organization> methods can be
759             used to check or clear the value.
760              
761             =head3 priority
762              
763             Priority accessor. Accepts values from 1 to 5. The default priority is 3.
764              
765             =head3 subject
766              
767             e-mail subject accessor. Must be specified.
768              
769             =head3 htmltext
770              
771             HTML mail body accessor. Additionally the C<has_htmltext> and
772             C<clear_htmltext> methods can be used to check or clear the value.
773              
774             =head3 mailer
775              
776             Mailer name.
777              
778             =head3 plaintext
779              
780             Plaintext mail body accessor. The C<clear_plaintext> and C<has_plaintext>
781             methods can be used to check or clear the value.
782              
783             This text will be autogenerated from htmltext if not provided by the user
784             and the C<autotext> option is not turned off. Simple formating (e.g. <strong>,
785             <em>, ...) will be converted to pseudo formating.
786              
787             If you want to disable the autogeneration of plaintext parts set the
788             L<autotext> accessor to a false value. However be aware that most anti-spam
789             enginges tag html e-mail messages without a plaintext part as spam.
790              
791             The following html tags will be transformed to simple markup:
792              
793             =over
794              
795             =item * I, EM
796              
797             Italic text will be surrounded by underscores. (_italic text_)
798              
799             =item * H1, H2, H3, ...
800              
801             Two equal signs are prepended to headlines (== Headline)
802              
803             =item * STRONG, B
804              
805             Bold text will be marked by stars (*bold text*)
806              
807             =item * HR
808              
809             A horizontal rule is replaced with 60 dashes.
810              
811             =item * BR
812              
813             Single linebreak
814              
815             =item * P, DIV
816              
817             Two linebreaks
818              
819             =item * IMG
820              
821             Prints the alt text of the image (if any).
822              
823             =item * A
824              
825             Prints the link url surrounded by brackets ([http://myurl.com text])
826              
827             =item * UL, OL
828              
829             All list items will be indented with a tab and prefixed with a start
830             (*) or an index number.
831              
832             =item * TABLE, TR, TD, TH
833              
834             Tables are converted into text using L<Text::Table>.
835              
836             =back
837              
838             =head3 autotext
839              
840             Enables the autogeneration of plaintext parts from HTML.
841              
842             Default TRUE.
843              
844             =head3 attachment
845              
846             $obj->attachment(Mail::Builder::List)
847             OR
848             $obj->attachment(Mail::Builder::Attachment)
849             OR
850             $obj->attachment(PATH | Path::Class::File | FH | IO::File | SCALARREF)
851             OR
852             $obj->attachment({
853             file => PATH | Path::Class::File | FH | IO::File | SCALARREF,
854             [name => NAME,]
855             [mimetype => MIME,]
856             })
857             OR
858             $obj->attachment([
859             HashRef | Mail::Builder::Attachment | PATH | Path::Class::File | FH | IO::File | SCALARREF
860             ]);
861              
862             This accessor always returns a Mail::Builder::List object. If you supply
863             a L<Mail::Builder::List> the list will be replaced.
864              
865             If you pass a Mail::Builder::Attachment object, a filehandle, an IO::File
866             object, a L<Path::Class::File> object, a path or a scalar reference
867             or a scalar path the current list will be reset and the new
868             attachment will be added.
869              
870             The L<Mail::Builder::List> package provides some basic methods for
871             manipulating the list of attachments.
872              
873             If you want to append an additional attachment to the list use
874              
875             $obj->attachment->add(PATH | Path::Class::File | FH | IO::File | ScalarRef)
876             OR
877             $obj->attachment->add({
878             file => PATH | Path::Class::File | FH | IO::File | ScalarRef,
879             [name => NAME,]
880             [mimetype => MIME,]
881             })
882             OR
883             $obj->attachment->add(Mail::Builder::Attachment)
884              
885             =head3 image
886              
887             $obj->image(Mail::Builder::List)
888             OR
889             $obj->image(Mail::Builder::Image)
890             OR
891             $obj->image(PATH | Path::Class::File | FH | IO::File | ScalarRef)
892             OR
893             $obj->image({
894             file => PATH | Path::Class::File | FH | IO::File | ScalarRef,
895             [id => ID,]
896             [mimetype => MIME,]
897             })
898             OR
899             $obj->image([
900             HashRef | Mail::Builder::Image | PATH | Path::Class::File | FH | IO::File | ScalarRef
901             ]);
902              
903             This accessor always returns a Mail::Builder::List object. If you supply
904             a L<Mail::Builder::List> the list will be replaced.
905              
906             If you pass a Mail::Builder::Image object or a scalar path (with an
907             optional id) the current list will be reset and the new image will be added.
908              
909             The L<Mail::Builder::List> package provides some basic methods for
910             manipulating the list of inline images.
911              
912             If you want to append an additional attachment to the list use
913              
914             $obj->image->add(PATH | Path::Class::File | FH | IO::File | ScalarRef)
915             OR
916             $obj->image->add(Mail::Builder::Image)
917              
918             You can embed the image into the html mail body code by referencing the ID. If
919             you don't provide an ID the lowercase filename without the file extension will
920             be used as the ID.
921              
922             <img src="cid:logo"/>
923              
924             Only jpg, gif and png images may be added as inline images.
925              
926             =head1 EXAMPLE
927              
928             If you want to send multiple e-mail messages from one Mail::Builder object
929             (e.g. a solicited mailing to multiple recipients) you have to pay special
930             attention, or else you might end up with growing recipients lists.
931              
932             # Example for a mass mailing
933             foreach my $recipient (@recipients) {
934             $mb->to->reset; # Remove all recipients
935             $mb->to->add($recipient); # Add current recipient
936            
937             # Alternatively you could use $mb->to($recipient); which has the
938             # same effect as the two previous commands. Same applies to 'cc' and 'bcc'
939            
940             # Autogenerated Plaintext will be reset
941             $mb->htmltext(qq[<h1>Hello $recipient!</h1> Text, yadda yadda! ]);
942            
943             # Plaintext is autogenerated
944             my $mail = $mb->stringify();
945            
946             # Send $mail ...
947             }
948              
949             =head1 IMPORTANT CHANGES
950              
951             From 1.10 on Mail::Builder only supports utf-8 charsets for mails. Supporting
952             multiple encodings turned out to be error prone and not necessary since all
953             modern mail clients support utf-8.
954              
955             Starting with Mail::Builder 2.0 the Mail::Builder::Attachment::* and
956             Mail::Builder::Image::* classes have been deprecated. Use the base classes
957             Mail::Builder::Attachment and Mail::Builder::Image instead.
958              
959             =head1 CAVEATS
960              
961             Watch out when sending Mail::Builder generated mails with
962             L<Email::Send::SMTP>: The 'Return-Path' headers are ignored by the MTA
963             since L<Email::Send::SMTP> uses the 'From' header for SMTP handshake. Postfix
964             (any maybe some other MTAs) overwrites the 'Return-Path' field in the data
965             with the e-mail used in the handshake ('From'). The behaviour of
966             L<Email::Send::SMTP> may however be modified by replacing the
967             C<get_env_sender> and C<get_env_recipients> methods. See L<Email::Send::SMTP>
968             for more details.
969              
970             =head1 SUPPORT
971              
972             Please report any bugs or feature requests to
973             C<bug-mail-builder@rt.cpan.org>, or through the web interface at
974             L<http://rt.cpan.org/Public/Bug/Report.html?Queue=Mail::Builder>.
975             I will be notified, and then you'll automatically be notified of progress on
976             your report as I make changes.
977              
978             =head1 AUTHOR
979              
980             MaroÅ¡ Kollár
981             CPAN ID: MAROS
982             maros [at] k-1.com
983             http://www.k-1.com
984              
985             =head1 COPYRIGHT
986              
987             Mail::Builder is Copyright (c) 2007-2010 MaroÅ¡ Kollár.
988              
989             This program is free software; you can redistribute it and/or modify it under
990             the same terms as Perl itself as long it is not used for sending
991             unsolicited mail (SPAM):
992              
993             "Thou shalt not send SPAM with this module."
994              
995             The full text of the license can be found in the
996             LICENSE file included with this module.
997              
998             =head1 SEE ALSO
999              
1000             The L<Mime::Entity> module in the L<Mime::Tools> distribution.
1001              
1002             L<Mail::Builder::Simple> provides a wrapper around this module and itegrates
1003             it with L<Email::Sender::Simple>
1004              
1005             Furthermore these modules are being used for various tasks:
1006              
1007             =over
1008              
1009             =item * L<Email::Valid> for validating e-mail addresses
1010              
1011             =item * L<Email::MessageID> for generating unique message IDs
1012              
1013             =item * L<HTML::TreeBuilder> for parsing html and generating plaintext
1014              
1015             =item * L<MIME::Types> for guessing attachment mime types
1016              
1017             =back
1018              
1019             =cut
1020              
1021             1;