-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathREADME
1424 lines (1087 loc) · 56.1 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
NAME
MIME::Lite - low-calorie MIME generator
WAIT!
MIME::Lite is not recommended by its current maintainer. There are a
number of alternatives, like Email::MIME or MIME::Entity and
Email::Sender, which you should probably use instead. MIME::Lite
continues to accrue weird bug reports, and it is not receiving a large
amount of refactoring due to the availability of better alternatives.
Please consider using something else.
SYNOPSIS
Create and send using the default send method for your OS a single-part
message:
use MIME::Lite;
### Create a new single-part message, to send a GIF file:
$msg = MIME::Lite->new(
From => '[email protected]',
To => '[email protected]',
Cc => '[email protected], [email protected]',
Subject => 'Helloooooo, nurse!',
Type => 'image/gif',
Encoding => 'base64',
Path => 'hellonurse.gif'
);
$msg->send; # send via default
Create a multipart message (i.e., one with attachments) and send it SMTP
### Create a new multipart message:
$msg = MIME::Lite->new(
From => '[email protected]',
To => '[email protected]',
Cc => '[email protected], [email protected]',
Subject => 'A message with 2 parts...',
Type => 'multipart/mixed'
);
### Add parts (each "attach" has same arguments as "new"):
$msg->attach(
Type => 'TEXT',
Data => "Here's the GIF file you wanted"
);
$msg->attach(
Type => 'image/gif',
Path => 'aaa000123.gif',
Filename => 'logo.gif',
Disposition => 'attachment'
);
### use Net:SMTP to do the sending
$msg->send('smtp','some.host', Debug=>1 );
Output a message:
### Format as a string:
$str = $msg->as_string;
### Print to a filehandle (say, a "sendmail" stream):
$msg->print(\*SENDMAIL);
Send a message:
### Send in the "best" way (the default is to use "sendmail"):
$msg->send;
### Send a specific way:
$msg->send('type',@args);
Specify default send method:
MIME::Lite->send('smtp','some.host',Debug=>0);
with authentication
MIME::Lite->send('smtp','some.host', AuthUser=>$user, AuthPass=>$pass);
DESCRIPTION
In the never-ending quest for great taste with fewer calories, we
proudly present: *MIME::Lite*.
MIME::Lite is intended as a simple, standalone module for generating
(not parsing!) MIME messages... specifically, it allows you to output a
simple, decent single- or multi-part message with text or binary
attachments. It does not require that you have the Mail:: or MIME::
modules installed, but will work with them if they are.
You can specify each message part as either the literal data itself (in
a scalar or array), or as a string which can be given to open() to get a
readable filehandle (e.g., "<filename" or "somecommand|").
You don't need to worry about encoding your message data: this module
will do that for you. It handles the 5 standard MIME encodings.
EXAMPLES
Create a simple message containing just text
$msg = MIME::Lite->new(
From =>'[email protected]',
To =>'[email protected]',
Cc =>'[email protected], [email protected]',
Subject =>'Helloooooo, nurse!',
Data =>"How's it goin', eh?"
);
Create a simple message containing just an image
$msg = MIME::Lite->new(
From =>'[email protected]',
To =>'[email protected]',
Cc =>'[email protected], [email protected]',
Subject =>'Helloooooo, nurse!',
Type =>'image/gif',
Encoding =>'base64',
Path =>'hellonurse.gif'
);
Create a multipart message
### Create the multipart "container":
$msg = MIME::Lite->new(
From =>'[email protected]',
To =>'[email protected]',
Cc =>'[email protected], [email protected]',
Subject =>'A message with 2 parts...',
Type =>'multipart/mixed'
);
### Add the text message part:
### (Note that "attach" has same arguments as "new"):
$msg->attach(
Type =>'TEXT',
Data =>"Here's the GIF file you wanted"
);
### Add the image part:
$msg->attach(
Type =>'image/gif',
Path =>'aaa000123.gif',
Filename =>'logo.gif',
Disposition => 'attachment'
);
Attach a GIF to a text message
This will create a multipart message exactly as above, but using the
"attach to singlepart" hack:
### Start with a simple text message:
$msg = MIME::Lite->new(
From =>'[email protected]',
To =>'[email protected]',
Cc =>'[email protected], [email protected]',
Subject =>'A message with 2 parts...',
Type =>'TEXT',
Data =>"Here's the GIF file you wanted"
);
### Attach a part... the make the message a multipart automatically:
$msg->attach(
Type =>'image/gif',
Path =>'aaa000123.gif',
Filename =>'logo.gif'
);
Attach a pre-prepared part to a message
### Create a standalone part:
$part = MIME::Lite->new(
Top => 0,
Type =>'text/html',
Data =>'<H1>Hello</H1>',
);
$part->attr('content-type.charset' => 'UTF-8');
$part->add('X-Comment' => 'A message for you');
### Attach it to any message:
$msg->attach($part);
Print a message to a filehandle
### Write it to a filehandle:
$msg->print(\*STDOUT);
### Write just the header:
$msg->print_header(\*STDOUT);
### Write just the encoded body:
$msg->print_body(\*STDOUT);
Print a message into a string
### Get entire message as a string:
$str = $msg->as_string;
### Get just the header:
$str = $msg->header_as_string;
### Get just the encoded body:
$str = $msg->body_as_string;
Send a message
### Send in the "best" way (the default is to use "sendmail"):
$msg->send;
Send an HTML document... with images included!
$msg = MIME::Lite->new(
To =>'[email protected]',
Subject =>'HTML with in-line images!',
Type =>'multipart/related'
);
$msg->attach(
Type => 'text/html',
Data => qq{
<body>
Here's <i>my</i> image:
<img src="cid:myimage.gif">
</body>
},
);
$msg->attach(
Type => 'image/gif',
Id => 'myimage.gif',
Path => '/path/to/somefile.gif',
);
$msg->send();
Change how messages are sent
### Do something like this in your 'main':
if ($I_DONT_HAVE_SENDMAIL) {
MIME::Lite->send('smtp', $host, Timeout=>60,
AuthUser=>$user, AuthPass=>$pass);
}
### Now this will do the right thing:
$msg->send; ### will now use Net::SMTP as shown above
PUBLIC INTERFACE
Global configuration
To alter the way the entire module behaves, you have the following
methods/options:
MIME::Lite->field_order()
When used as a classmethod, this changes the default order in which
headers are output for *all* messages. However, please consider
using the instance method variant instead, so you won't stomp on
other message senders in the same application.
MIME::Lite->quiet()
This classmethod can be used to suppress/unsuppress all warnings
coming from this module.
MIME::Lite->send()
When used as a classmethod, this can be used to specify a different
default mechanism for sending message. The initial default is:
MIME::Lite->send("sendmail", "/usr/lib/sendmail -t -oi -oem");
However, you should consider the similar but smarter and taint-safe
variant:
MIME::Lite->send("sendmail");
Or, for non-Unix users:
MIME::Lite->send("smtp");
$MIME::Lite::AUTO_CC
If true, automatically send to the Cc/Bcc addresses for
send_by_smtp(). Default is true.
$MIME::Lite::AUTO_CONTENT_TYPE
If true, try to automatically choose the content type from the file
name in "new()"/"build()". In other words, setting this true changes
the default "Type" from "TEXT" to "AUTO".
Default is false, since we must maintain backwards-compatibility
with prior behavior. Please consider keeping it false, and just
using Type 'AUTO' when you build() or attach().
$MIME::Lite::AUTO_ENCODE
If true, automatically choose the encoding from the content type.
Default is true.
$MIME::Lite::AUTO_VERIFY
If true, check paths to attachments right before printing, raising
an exception if any path is unreadable. Default is true.
$MIME::Lite::PARANOID
If true, we won't attempt to use MIME::Base64, MIME::QuotedPrint, or
MIME::Types, even if they're available. Default is false. Please
consider keeping it false, and trusting these other packages to do
the right thing.
Construction
new [PARAMHASH]
*Class method, constructor.* Create a new message object.
If any arguments are given, they are passed into "build()";
otherwise, just the empty object is created.
attach PART
attach PARAMHASH...
*Instance method.* Add a new part to this message, and return the
new part.
If you supply a single PART argument, it will be regarded as a
MIME::Lite object to be attached. Otherwise, this method assumes
that you are giving in the pairs of a PARAMHASH which will be sent
into "new()" to create the new part.
One of the possibly-quite-useful hacks thrown into this is the
"attach-to-singlepart" hack: if you attempt to attach a part (let's
call it "part 1") to a message that doesn't have a content-type of
"multipart" or "message", the following happens:
* A new part (call it "part 0") is made.
* The MIME attributes and data (but *not* the other headers) are
cut from the "self" message, and pasted into "part 0".
* The "self" is turned into a "multipart/mixed" message.
* The new "part 0" is added to the "self", and *then* "part 1" is
added.
One of the nice side-effects is that you can create a text message
and then add zero or more attachments to it, much in the same way
that a user agent like Netscape allows you to do.
build [PARAMHASH]
*Class/instance method, initializer.* Create (or initialize) a MIME
message object. Normally, you'll use the following keys in
PARAMHASH:
* Data, FH, or Path (either one of these, or none if multipart)
* Type (e.g., "image/jpeg")
* From, To, and Subject (if this is the "top level" of a message)
The PARAMHASH can contain the following keys:
(fieldname)
Any field you want placed in the message header, taken from the
standard list of header fields (you don't need to worry about
case):
Approved Encrypted Received Sender
Bcc From References Subject
Cc Keywords Reply-To To
Comments Message-ID Resent-* X-*
Content-* MIME-Version Return-Path
Date Organization
To give experienced users some veto power, these fields will be
set *after* the ones I set... so be careful: *don't set any MIME
fields* (like "Content-type") unless you know what you're doing!
To specify a fieldname that's *not* in the above list, even one
that's identical to an option below, just give it with a
trailing ":", like "My-field:". When in doubt, that *always*
signals a mail field (and it sort of looks like one too).
Data
*Alternative to "Path" or "FH".* The actual message data. This
may be a scalar or a ref to an array of strings; if the latter,
the message consists of a simple concatenation of all the
strings in the array.
Datestamp
*Optional.* If given true (or omitted), we force the creation of
a "Date:" field stamped with the current date/time if this is a
top-level message. You may want this if using send_by_smtp(). If
you don't want this to be done, either provide your own Date or
explicitly set this to false.
Disposition
*Optional.* The content disposition, "inline" or "attachment".
The default is "inline".
Encoding
*Optional.* The content transfer encoding that should be used to
encode your data:
Use encoding: | If your message contains:
------------------------------------------------------------
7bit | Only 7-bit text, all lines <1000 characters
8bit | 8-bit text, all lines <1000 characters
quoted-printable | 8-bit text or long lines (more reliable than "8bit")
base64 | Largely non-textual data: a GIF, a tar file, etc.
The default is taken from the Type; generally it is "binary" (no
encoding) for text/*, message/*, and multipart/*, and "base64"
for everything else. A value of "binary" is generally *not*
suitable for sending anything but ASCII text files with lines
under 1000 characters, so consider using one of the other values
instead.
In the case of "7bit"/"8bit", long lines are automatically
chopped to legal length; in the case of "7bit", all 8-bit
characters are automatically *removed*. This may not be what you
want, so pick your encoding well! For more info, see "A MIME
PRIMER".
FH *Alternative to "Data" or "Path".* Filehandle containing the
data, opened for reading. See "ReadNow" also.
Filename
*Optional.* The name of the attachment. You can use this to
supply a recommended filename for the end-user who is saving the
attachment to disk. You only need this if the filename at the
end of the "Path" is inadequate, or if you're using "Data"
instead of "Path". You should *not* put path information in here
(e.g., no "/" or "\" or ":" characters should be used).
Id *Optional.* Same as setting "content-id".
Length
*Optional.* Set the content length explicitly. Normally, this
header is automatically computed, but only under certain
circumstances (see "Benign limitations").
Path
*Alternative to "Data" or "FH".* Path to a file containing the
data... actually, it can be any open()able expression. If it
looks like a path, the last element will automatically be
treated as the filename. See "ReadNow" also.
ReadNow
*Optional, for use with "Path".* If true, will open the path and
slurp the contents into core now. This is useful if the Path
points to a command and you don't want to run the command over
and over if outputting the message several times. Fatal
exception raised if the open fails.
Top *Optional.* If defined, indicates whether or not this is a
"top-level" MIME message. The parts of a multipart message are
*not* top-level. Default is true.
Type
*Optional.* The MIME content type, or one of these special
values (case-sensitive):
"TEXT" means "text/plain"
"BINARY" means "application/octet-stream"
"AUTO" means attempt to guess from the filename, falling back
to 'application/octet-stream'. This is good if you have
MIME::Types on your system and you have no idea what
file might be used for the attachment.
The default is "TEXT", but it will be "AUTO" if you set
$AUTO_CONTENT_TYPE to true (sorry, but you have to enable it
explicitly, since we don't want to break code which depends on
the old behavior).
A picture being worth 1000 words (which is of course 2000 bytes, so
it's probably more of an "icon" than a "picture", but I digress...),
here are some examples:
$msg = MIME::Lite->build(
From => '[email protected]',
To => '[email protected]',
Subject => "Hi there!",
Type => 'TEXT',
Encoding => '7bit',
Data => "Just a quick note to say hi!"
);
$msg = MIME::Lite->build(
From => '[email protected]',
To => '[email protected]',
Subject => "A gif for U"
Type => 'image/gif',
Path => "/home/httpd/logo.gif"
);
$msg = MIME::Lite->build(
From => '[email protected]',
To => '[email protected]',
Subject => "A gzipp'ed tar file",
Type => 'x-gzip',
Path => "gzip < /usr/inc/somefile.tar |",
ReadNow => 1,
Filename => "somefile.tgz"
);
To show you what's really going on, that last example could also
have been written:
$msg = new MIME::Lite;
$msg->build(
Type => 'x-gzip',
Path => "gzip < /usr/inc/somefile.tar |",
ReadNow => 1,
Filename => "somefile.tgz"
);
$msg->add(From => "[email protected]");
$msg->add(To => "[email protected]");
$msg->add(Subject => "A gzipp'ed tar file");
Setting/getting headers and attributes
add TAG,VALUE
*Instance method.* Add field TAG with the given VALUE to the end of
the header. The TAG will be converted to all-lowercase, and the
VALUE will be made "safe" (returns will be given a trailing space).
Beware: any MIME fields you "add" will override any MIME attributes
I have when it comes time to output those fields. Normally, you will
use this method to add *non-MIME* fields:
$msg->add("Subject" => "Hi there!");
Giving VALUE as an arrayref will cause all those values to be added.
This is only useful for special multiple-valued fields like
"Received":
$msg->add("Received" => ["here", "there", "everywhere"]
Giving VALUE as the empty string adds an invisible placeholder to
the header, which can be used to suppress the output of the
"Content-*" fields or the special "MIME-Version" field. When
suppressing fields, you should use replace() instead of add():
$msg->replace("Content-disposition" => "");
*Note:* add() is probably going to be more efficient than
"replace()", so you're better off using it for most applications if
you are certain that you don't need to delete() the field first.
*Note:* the name comes from Mail::Header.
attr ATTR,[VALUE]
*Instance method.* Set MIME attribute ATTR to the string VALUE. ATTR
is converted to all-lowercase. This method is normally used to
set/get MIME attributes:
$msg->attr("content-type" => "text/html");
$msg->attr("content-type.charset" => "US-ASCII");
$msg->attr("content-type.name" => "homepage.html");
This would cause the final output to look something like this:
Content-type: text/html; charset=US-ASCII; name="homepage.html"
Note that the special empty sub-field tag indicates the anonymous
first sub-field.
Giving VALUE as undefined will cause the contents of the named
subfield to be deleted.
Supplying no VALUE argument just returns the attribute's value:
$type = $msg->attr("content-type"); ### returns "text/html"
$name = $msg->attr("content-type.name"); ### returns "homepage.html"
delete TAG
*Instance method.* Delete field TAG with the given VALUE to the end
of the header. The TAG will be converted to all-lowercase.
$msg->delete("Subject");
*Note:* the name comes from Mail::Header.
field_order FIELD,...FIELD
*Class/instance method.* Change the order in which header fields are
output for this object:
$msg->field_order('from', 'to', 'content-type', 'subject');
When used as a class method, changes the default settings for all
objects:
MIME::Lite->field_order('from', 'to', 'content-type', 'subject');
Case does not matter: all field names will be coerced to lowercase.
In either case, supply the empty array to restore the default
ordering.
fields
*Instance method.* Return the full header for the object, as a ref
to an array of "[TAG, VALUE]" pairs, where each TAG is
all-lowercase. Note that any fields the user has explicitly set will
override the corresponding MIME fields that we would otherwise
generate. So, don't say...
$msg->set("Content-type" => "text/html; charset=US-ASCII");
unless you want the above value to override the "Content-type" MIME
field that we would normally generate.
*Note:* I called this "fields" because the header() method of
Mail::Header returns something different, but similar enough to be
confusing.
You can change the order of the fields: see "field_order". You
really shouldn't need to do this, but some people have to deal with
broken mailers.
filename [FILENAME]
*Instance method.* Set the filename which this data will be reported
as. This actually sets both "standard" attributes.
With no argument, returns the filename as dictated by the
content-disposition.
get TAG,[INDEX]
*Instance method.* Get the contents of field TAG, which might have
been set with set() or replace(). Returns the text of the field.
$ml->get('Subject', 0);
If the optional 0-based INDEX is given, then we return the INDEX'th
occurrence of field TAG. Otherwise, we look at the context: In a
scalar context, only the first (0th) occurrence of the field is
returned; in an array context, *all* occurrences are returned.
*Warning:* this should only be used with non-MIME fields. Behavior
with MIME fields is TBD, and will raise an exception for now.
get_length
*Instance method.* Recompute the content length for the message *if
the process is trivial*, setting the "content-length" attribute as a
side-effect:
$msg->get_length;
Returns the length, or undefined if not set.
*Note:* the content length can be difficult to compute, since it
involves assembling the entire encoded body and taking the length of
it (which, in the case of multipart messages, means freezing all the
sub-parts, etc.).
This method only sets the content length to a defined value if the
message is a singlepart with "binary" encoding, *and* the body is
available either in-core or as a simple file. Otherwise, the content
length is set to the undefined value.
Since content-length is not a standard MIME field anyway (that's
right, kids: it's not in the MIME RFCs, it's an HTTP thing), this
seems pretty fair.
parts
*Instance method.* Return the parts of this entity, and this entity
only. Returns empty array if this entity has no parts.
This is not recursive! Parts can have sub-parts; use parts_DFS() to
get everything.
parts_DFS
*Instance method.* Return the list of all MIME::Lite objects
included in the entity, starting with the entity itself, in
depth-first-search order. If this object has no parts, it alone will
be returned.
preamble [TEXT]
*Instance method.* Get/set the preamble string, assuming that this
object has subparts. Set it to undef for the default string.
replace TAG,VALUE
*Instance method.* Delete all occurrences of fields named TAG, and
add a new field with the given VALUE. TAG is converted to
all-lowercase.
Beware the special MIME fields (MIME-version, Content-*): if you
"replace" a MIME field, the replacement text will override the
*actual* MIME attributes when it comes time to output that field. So
normally you use attr() to change MIME fields and add()/replace() to
change *non-MIME* fields:
$msg->replace("Subject" => "Hi there!");
Giving VALUE as the *empty string* will effectively *prevent* that
field from being output. This is the correct way to suppress the
special MIME fields:
$msg->replace("Content-disposition" => "");
Giving VALUE as *undefined* will just cause all explicit values for
TAG to be deleted, without having any new values added.
*Note:* the name of this method comes from Mail::Header.
scrub
*Instance method.* This is Alpha code. If you use it, please let me
know how it goes. Recursively goes through the "parts" tree of this
message and tries to find MIME attributes that can be removed. With
an array argument, removes exactly those attributes; e.g.:
$msg->scrub(['content-disposition', 'content-length']);
Is the same as recursively doing:
$msg->replace('Content-disposition' => '');
$msg->replace('Content-length' => '');
Setting/getting message data
binmode [OVERRIDE]
*Instance method.* With no argument, returns whether or not it
thinks that the data (as given by the "Path" argument of "build()")
should be read using binmode() (for example, when "read_now()" is
invoked).
The default behavior is that any content type other than "text/*" or
"message/*" is binmode'd; this should in general work fine.
With a defined argument, this method sets an explicit "override"
value. An undefined argument unsets the override. The new current
value is returned.
data [DATA]
*Instance method.* Get/set the literal DATA of the message. The DATA
may be either a scalar, or a reference to an array of scalars (which
will simply be joined).
*Warning:* setting the data causes the "content-length" attribute to
be recomputed (possibly to nothing).
fh [FILEHANDLE]
*Instance method.* Get/set the FILEHANDLE which contains the message
data.
Takes a filehandle as an input and stores it in the object. This
routine is similar to path(); one important difference is that no
attempt is made to set the content length.
path [PATH]
*Instance method.* Get/set the PATH to the message data.
*Warning:* setting the path recomputes any existing "content-length"
field, and re-sets the "filename" (to the last element of the path
if it looks like a simple path, and to nothing if not).
resetfh [FILEHANDLE]
*Instance method.* Set the current position of the filehandle back
to the beginning. Only applies if you used "FH" in build() or
attach() for this message.
Returns false if unable to reset the filehandle (since not all
filehandles are seekable).
read_now
*Instance method.* Forces data from the path/filehandle (as
specified by "build()") to be read into core immediately, just as
though you had given it literally with the "Data" keyword.
Note that the in-core data will always be used if available.
Be aware that everything is slurped into a giant scalar: you may not
want to use this if sending tar files! The benefit of *not* reading
in the data is that very large files can be handled by this module
if left on disk until the message is output via "print()" or
"print_body()".
sign PARAMHASH
*Instance method.* Sign the message. This forces the message to be
read into core, after which the signature is appended to it.
Data
As in "build()": the literal signature data. Can be either a
scalar or a ref to an array of scalars.
Path
As in "build()": the path to the file.
If no arguments are given, the default is:
Path => "$ENV{HOME}/.signature"
The content-length is recomputed.
verify_data
*Instance method.* Verify that all "paths" to attached data exist,
recursively. It might be a good idea for you to do this before a
print(), to prevent accidental partial output if a file might be
missing. Raises exception if any path is not readable.
Output
print [OUTHANDLE]
*Instance method.* Print the message to the given output handle, or
to the currently-selected filehandle if none was given.
All OUTHANDLE has to be is a filehandle (possibly a glob ref), or
any object that responds to a print() message.
print_body [OUTHANDLE] [IS_SMTP]
*Instance method.* Print the body of a message to the given output
handle, or to the currently-selected filehandle if none was given.
All OUTHANDLE has to be is a filehandle (possibly a glob ref), or
any object that responds to a print() message.
Fatal exception raised if unable to open any of the input files, or
if a part contains no data, or if an unsupported encoding is
encountered.
IS_SMPT is a special option to handle SMTP mails a little more
intelligently than other send mechanisms may require. Specifically
this ensures that the last byte sent is NOT '\n' (octal \012) if the
last two bytes are not '\r\n' (\015\012) as this will cause some
SMTP servers to hang.
print_header [OUTHANDLE]
*Instance method.* Print the header of the message to the given
output handle, or to the currently-selected filehandle if none was
given.
All OUTHANDLE has to be is a filehandle (possibly a glob ref), or
any object that responds to a print() message.
as_string
*Instance method.* Return the entire message as a string, with a
header and an encoded body.
body_as_string
*Instance method.* Return the encoded body as a string. This is the
portion after the header and the blank line.
*Note:* actually prepares the body by "printing" to a scalar. Proof
that you can hand the "print*()" methods any blessed object that
responds to a "print()" message.
header_as_string
*Instance method.* Return the header as a string.
Sending
send
send HOW, HOWARGS...
*Class/instance method.* This is the principal method for sending
mail, and for configuring how mail will be sent.
*As a class method* with a HOW argument and optional HOWARGS, it
sets the default sending mechanism that the no-argument instance
method will use. The HOW is a facility name (see below), and the
HOWARGS is interpreted by the facility. The class method returns the
previous HOW and HOWARGS as an array.
MIME::Lite->send('sendmail', "d:\\programs\\sendmail.exe");
...
$msg = MIME::Lite->new(...);
$msg->send;
*As an instance method with arguments* (a HOW argument and optional
HOWARGS), sends the message in the requested manner; e.g.:
$msg->send('sendmail', "d:\\programs\\sendmail.exe");
*As an instance method with no arguments,* sends the message by the
default mechanism set up by the class method. Returns whatever the
mail-handling routine returns: this should be true on success,
false/exception on error:
$msg = MIME::Lite->new(From=>...);
$msg->send || die "you DON'T have mail!";
On Unix systems (or rather non-Win32 systems), the default setting
is equivalent to:
MIME::Lite->send("sendmail", "/usr/lib/sendmail -t -oi -oem");
On Win32 systems the default setting is equivalent to:
MIME::Lite->send("smtp");
The assumption is that on Win32 your site/lib/Net/libnet.cfg file
will be preconfigured to use the appropriate SMTP server. See below
for configuring for authentication.
There are three facilities:
"sendmail", ARGS...
Send a message by piping it into the "sendmail" command. Uses
the send_by_sendmail() method, giving it the ARGS. This usage
implements (and deprecates) the "sendmail()" method.
"smtp", [HOSTNAME, [NAMEDPARMS] ]
Send a message by SMTP, using optional HOSTNAME as SMTP-sending
host. Net::SMTP will be required. Uses the send_by_smtp()
method. Any additional arguments passed in will also be passed
through to send_by_smtp. This is useful for things like mail
servers requiring authentication where you can say something
like the following
MIME::Lite->send('smtp', $host, AuthUser=>$user, AuthPass=>$pass);
which will configure things so future uses of
$msg->send();
do the right thing.
"sub", \&SUBREF, ARGS...
Sends a message MSG by invoking the subroutine SUBREF of your
choosing, with MSG as the first argument, and ARGS following.
*For example:* let's say you're on an OS which lacks the usual Unix
"sendmail" facility, but you've installed something a lot like it,
and you need to configure your Perl script to use this
"sendmail.exe" program. Do this following in your script's setup:
MIME::Lite->send('sendmail', "d:\\programs\\sendmail.exe");
Then, whenever you need to send a message $msg, just say:
$msg->send;
That's it. Now, if you ever move your script to a Unix box, all you
need to do is change that line in the setup and you're done. All of
your $msg->send invocations will work as expected.
After sending, the method last_send_successful() can be used to
determine if the send was successful or not.
send_by_sendmail SENDMAILCMD
send_by_sendmail PARAM=>VALUE, ARRAY, HASH...
*Instance method.* Send message via an external "sendmail" program
(this will probably only work out-of-the-box on Unix systems).
Returns true on success, false or exception on error.
You can specify the program and all its arguments by giving a single
string, SENDMAILCMD. Nothing fancy is done; the message is simply
piped in.
However, if your needs are a little more advanced, you can specify
zero or more of the following PARAM/VALUE pairs (or a reference to
hash or array of such arguments as well as any combination thereof);
a Unix-style, taint-safe "sendmail" command will be constructed for
you:
Sendmail
Full path to the program to use. Default is "/usr/lib/sendmail".
BaseArgs
Ref to the basic array of arguments we start with. Default is
"["-t", "-oi", "-oem"]".
SetSender
Unless this is *explicitly* given as false, we attempt to
automatically set the "-f" argument to the first address that
can be extracted from the "From:" field of the message (if there
is one).
*What is the -f, and why do we use it?* Suppose we did *not* use
"-f", and you gave an explicit "From:" field in your message: in
this case, the sendmail "envelope" would indicate the *real*
user your process was running under, as a way of preventing mail
forgery. Using the "-f" switch causes the sender to be set in
the envelope as well.
*So when would I NOT want to use it?* If sendmail doesn't regard
you as a "trusted" user, it will permit the "-f" but also add an
"X-Authentication-Warning" header to the message to indicate a
forged envelope. To avoid this, you can either (1) have
SetSender be false, or (2) make yourself a trusted user by
adding a "T" configuration command to your *sendmail.cf* file
(e.g.: "Teryq" if the script is running as user "eryq").
FromSender
If defined, this is identical to setting SetSender to true,
except that instead of looking at the "From:" field we use the
address given by this option. Thus:
FromSender => '[email protected]'
After sending, the method last_send_successful() can be used to
determine if the send was successful or not.
send_by_smtp HOST, ARGS...
send_by_smtp REF, HOST, ARGS
*Instance method.* Send message via SMTP, using Net::SMTP -- which
will be required for this feature.
HOST is the name of SMTP server to connect to, or undef to have
Net::SMTP use the defaults in Libnet.cfg.
ARGS are a list of key value pairs which may be selected from the
list below. Many of these are just passed through to specific
Net::SMTP commands and you should review that module for details.
Please see Good-vs-bad email addresses with send_by_smtp()
Hello
LocalAddr
LocalPort
Timeout
Port
ExactAddresses
Debug
See Net::SMTP::new() for details.
Size
Return
Bits
Transaction
Envelope
See Net::SMTP::mail() for details.
SkipBad
If true doesn't throw an error when multiple email addresses are
provided and some are not valid. See Net::SMTP::recipient() for
details.
AuthUser
Authenticate with Net::SMTP::auth() using this username.
AuthPass
Authenticate with Net::SMTP::auth() using this password.