1Net::SMTP(3pm) Perl Programmers Reference Guide Net::SMTP(3pm)
2
3
4
6 Net::SMTP - Simple Mail Transfer Protocol Client
7
9 use Net::SMTP;
10
11 # Constructors
12 $smtp = Net::SMTP->new('mailhost');
13 $smtp = Net::SMTP->new('mailhost', Timeout => 60);
14
16 This module implements a client interface to the SMTP and ESMTP proto‐
17 col, enabling a perl5 application to talk to SMTP servers. This docu‐
18 mentation assumes that you are familiar with the concepts of the SMTP
19 protocol described in RFC821.
20
21 A new Net::SMTP object must be created with the new method. Once this
22 has been done, all SMTP commands are accessed through this object.
23
24 The Net::SMTP class is a subclass of Net::Cmd and IO::Socket::INET.
25
27 This example prints the mail domain name of the SMTP server known as
28 mailhost:
29
30 #!/usr/local/bin/perl -w
31
32 use Net::SMTP;
33
34 $smtp = Net::SMTP->new('mailhost');
35 print $smtp->domain,"\n";
36 $smtp->quit;
37
38 This example sends a small message to the postmaster at the SMTP server
39 known as mailhost:
40
41 #!/usr/local/bin/perl -w
42
43 use Net::SMTP;
44
45 $smtp = Net::SMTP->new('mailhost');
46
47 $smtp->mail($ENV{USER});
48 $smtp->to('postmaster');
49
50 $smtp->data();
51 $smtp->datasend("To: postmaster\n");
52 $smtp->datasend("\n");
53 $smtp->datasend("A simple test message\n");
54 $smtp->dataend();
55
56 $smtp->quit;
57
59 new ( [ HOST ] [, OPTIONS ] )
60 This is the constructor for a new Net::SMTP object. "HOST" is the
61 name of the remote host to which an SMTP connection is required.
62
63 "HOST" is optional. If "HOST" is not given then it may instead be
64 passed as the "Host" option described below. If neither is given
65 then the "SMTP_Hosts" specified in "Net::Config" will be used.
66
67 "OPTIONS" are passed in a hash like fashion, using key and value
68 pairs. Possible options are:
69
70 Hello - SMTP requires that you identify yourself. This option spec‐
71 ifies a string to pass as your mail domain. If not given local‐
72 host.localdomain will be used.
73
74 Host - SMTP host to connect to. It may be a single scalar, as
75 defined for the "PeerAddr" option in IO::Socket::INET, or a refer‐
76 ence to an array with hosts to try in turn. The "host" method will
77 return the value which was used to connect to the host.
78
79 LocalAddr and LocalPort - These parameters are passed directly to
80 IO::Socket to allow binding the socket to a local port.
81
82 Timeout - Maximum time, in seconds, to wait for a response from the
83 SMTP server (default: 120)
84
85 ExactAddresses - If true the all ADDRESS arguments must be as
86 defined by "addr-spec" in RFC2822. If not given, or false, then
87 Net::SMTP will attempt to extract the address from the value
88 passed.
89
90 Debug - Enable debugging information
91
92 Example:
93
94 $smtp = Net::SMTP->new('mailhost',
95 Hello => 'my.mail.domain'
96 Timeout => 30,
97 Debug => 1,
98 );
99
100 # the same
101 $smtp = Net::SMTP->new(
102 Host => 'mailhost',
103 Hello => 'my.mail.domain'
104 Timeout => 30,
105 Debug => 1,
106 );
107
108 # Connect to the default server from Net::config
109 $smtp = Net::SMTP->new(
110 Hello => 'my.mail.domain'
111 Timeout => 30,
112 );
113
115 Unless otherwise stated all methods return either a true or false
116 value, with true meaning that the operation was a success. When a
117 method states that it returns a value, failure will be returned as
118 undef or an empty list.
119
120 banner ()
121 Returns the banner message which the server replied with when the
122 initial connection was made.
123
124 domain ()
125 Returns the domain that the remote SMTP server identified itself as
126 during connection.
127
128 hello ( DOMAIN )
129 Tell the remote server the mail domain which you are in using the
130 EHLO command (or HELO if EHLO fails). Since this method is invoked
131 automatically when the Net::SMTP object is constructed the user
132 should normally not have to call it manually.
133
134 host ()
135 Returns the value used by the constructor, and passed to
136 IO::Socket::INET, to connect to the host.
137
138 etrn ( DOMAIN )
139 Request a queue run for the DOMAIN given.
140
141 auth ( USERNAME, PASSWORD )
142 Attempt SASL authentication.
143
144 mail ( ADDRESS [, OPTIONS] )
145 send ( ADDRESS )
146 send_or_mail ( ADDRESS )
147 send_and_mail ( ADDRESS )
148 Send the appropriate command to the server MAIL, SEND, SOML or
149 SAML. "ADDRESS" is the address of the sender. This initiates the
150 sending of a message. The method "recipient" should be called for
151 each address that the message is to be sent to.
152
153 The "mail" method can some additional ESMTP OPTIONS which is passed
154 in hash like fashion, using key and value pairs. Possible options
155 are:
156
157 Size => <bytes>
158 Return => "FULL" ⎪ "HDRS"
159 Bits => "7" ⎪ "8" ⎪ "binary"
160 Transaction => <ADDRESS>
161 Envelope => <ENVID>
162 XVERP => 1
163
164 The "Return" and "Envelope" parameters are used for DSN (Delivery
165 Status Notification).
166
167 reset ()
168 Reset the status of the server. This may be called after a message
169 has been initiated, but before any data has been sent, to cancel
170 the sending of the message.
171
172 recipient ( ADDRESS [, ADDRESS, [...]] [, OPTIONS ] )
173 Notify the server that the current message should be sent to all of
174 the addresses given. Each address is sent as a separate command to
175 the server. Should the sending of any address result in a failure
176 then the process is aborted and a false value is returned. It is up
177 to the user to call "reset" if they so desire.
178
179 The "recipient" method can also pass additional case-sensitive
180 OPTIONS as an anonymous hash using key and value pairs. Possible
181 options are:
182
183 Notify => ['NEVER'] or ['SUCCESS','FAILURE','DELAY'] (see below)
184 SkipBad => 1 (to ignore bad addresses)
185
186 If "SkipBad" is true the "recipient" will not return an error when
187 a bad address is encountered and it will return an array of
188 addresses that did succeed.
189
190 $smtp->recipient($recipient1,$recipient2); # Good
191 $smtp->recipient($recipient1,$recipient2, { SkipBad => 1 }); # Good
192 $smtp->recipient($recipient1,$recipient2, { Notify => ['FAILURE','DELAY'], SkipBad => 1 }); # Good
193 @goodrecips=$smtp->recipient(@recipients, { Notify => ['FAILURE'], SkipBad => 1 }); # Good
194 $smtp->recipient("$recipient,$recipient2"); # BAD
195
196 Notify is used to request Delivery Status Notifications (DSNs), but
197 your SMTP/ESMTP service may not respect this request depending upon
198 its version and your site's SMTP configuration.
199
200 Leaving out the Notify option usually defaults an SMTP service to
201 its default behavior equivalent to ['FAILURE'] notifications only,
202 but again this may be dependent upon your site's SMTP configura‐
203 tion.
204
205 The NEVER keyword must appear by itself if used within the Notify
206 option and "requests that a DSN not be returned to the sender under
207 any conditions."
208
209 {Notify => ['NEVER']}
210
211 $smtp->recipient(@recipients, { Notify => ['NEVER'], SkipBad => 1 }); # Good
212
213 You may use any combination of these three values 'SUCCESS','FAIL‐
214 URE','DELAY' in the anonymous array reference as defined by RFC3461
215 (see http://rfc.net/rfc3461.html for more information. Note: quo‐
216 tations in this topic from same.).
217
218 A Notify parameter of 'SUCCESS' or 'FAILURE' "requests that a DSN
219 be issued on successful delivery or delivery failure, respec‐
220 tively."
221
222 A Notify parameter of 'DELAY' "indicates the sender's willingness
223 to receive delayed DSNs. Delayed DSNs may be issued if delivery of
224 a message has been delayed for an unusual amount of time (as deter‐
225 mined by the Message Transfer Agent (MTA) at which the message is
226 delayed), but the final delivery status (whether successful or
227 failure) cannot be determined. The absence of the DELAY keyword in
228 a NOTIFY parameter requests that a "delayed" DSN NOT be issued
229 under any conditions."
230
231 {Notify => ['SUCCESS','FAILURE','DELAY']}
232
233 $smtp->recipient(@recipients, { Notify => ['FAILURE','DELAY'], SkipBad => 1 }); # Good
234
235 to ( ADDRESS [, ADDRESS [...]] )
236 cc ( ADDRESS [, ADDRESS [...]] )
237 bcc ( ADDRESS [, ADDRESS [...]] )
238 Synonyms for "recipient".
239
240 data ( [ DATA ] )
241 Initiate the sending of the data from the current message.
242
243 "DATA" may be a reference to a list or a list. If specified the
244 contents of "DATA" and a termination string ".\r\n" is sent to the
245 server. And the result will be true if the data was accepted.
246
247 If "DATA" is not specified then the result will indicate that the
248 server wishes the data to be sent. The data must then be sent using
249 the "datasend" and "dataend" methods described in Net::Cmd.
250
251 expand ( ADDRESS )
252 Request the server to expand the given address Returns an array
253 which contains the text read from the server.
254
255 verify ( ADDRESS )
256 Verify that "ADDRESS" is a legitimate mailing address.
257
258 Most sites usually disable this feature in their SMTP service con‐
259 figuration. Use "Debug => 1" option under new() to see if dis‐
260 abled.
261
262 help ( [ $subject ] )
263 Request help text from the server. Returns the text or undef upon
264 failure
265
266 quit ()
267 Send the QUIT command to the remote SMTP server and close the
268 socket connection.
269
271 Net::SMTP attempts to DWIM with addresses that are passed. For example
272 an application might extract The From: line from an email and pass that
273 to mail(). While this may work, it is not reccomended. The application
274 should really use a module like Mail::Address to extract the mail
275 address and pass that.
276
277 If "ExactAddresses" is passed to the contructor, then addresses should
278 be a valid rfc2821-quoted address, although Net::SMTP will accept
279 accept the address surrounded by angle brackets.
280
281 funny user@domain WRONG
282 "funny user"@domain RIGHT, recommended
283 <"funny user"@domain> OK
284
286 Net::Cmd
287
289 Graham Barr <gbarr@pobox.com>
290
292 Copyright (c) 1995-2004 Graham Barr. All rights reserved. This program
293 is free software; you can redistribute it and/or modify it under the
294 same terms as Perl itself.
295
296
297
298perl v5.8.8 2001-09-21 Net::SMTP(3pm)