1Net::SMTP(3) User Contributed Perl Documentation Net::SMTP(3)
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
17 protocol, enabling a perl5 application to talk to SMTP servers. This
18 documentation assumes that you are familiar with the concepts of the
19 SMTP protocol described in RFC2821. With IO::Socket::SSL installed it
20 also provides support for implicit and explicit TLS encryption, i.e.
21 SMTPS or SMTP+STARTTLS.
22
23 The Net::SMTP class is a subclass of Net::Cmd and (depending on
24 avaibility) of IO::Socket::IP, IO::Socket::INET6 or 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 my $smtp = Net::SMTP->new('mailhost');
46
47 $smtp->mail($ENV{USER});
48 if ($smtp->to('postmaster')) {
49 $smtp->data();
50 $smtp->datasend("To: postmaster\n");
51 $smtp->datasend("\n");
52 $smtp->datasend("A simple test message\n");
53 $smtp->dataend();
54 } else {
55 print "Error: ", $smtp->message();
56 }
57
58 $smtp->quit;
59
61 new ( [ HOST ] [, OPTIONS ] )
62 This is the constructor for a new Net::SMTP object. "HOST" is the
63 name of the remote host to which an SMTP connection is required.
64
65 On failure "undef" will be returned and $@ will contain the reason
66 for the failure.
67
68 "HOST" is optional. If "HOST" is not given then it may instead be
69 passed as the "Host" option described below. If neither is given
70 then the "SMTP_Hosts" specified in "Net::Config" will be used.
71
72 "OPTIONS" are passed in a hash like fashion, using key and value
73 pairs. Possible options are:
74
75 Hello - SMTP requires that you identify yourself. This option
76 specifies a string to pass as your mail domain. If not given
77 localhost.localdomain will be used.
78
79 SendHello - If false then the EHLO (or HELO) command that is
80 normally sent when constructing the object will not be sent. In
81 that case the command will have to be sent manually by calling
82 "hello()" instead.
83
84 Host - SMTP host to connect to. It may be a single scalar
85 (hostname[:port]), as defined for the "PeerAddr" option in
86 IO::Socket::INET, or a reference to an array with hosts to try in
87 turn. The "host" method will return the value which was used to
88 connect to the host. Format - "PeerHost" from IO::Socket::INET new
89 method.
90
91 Port - port to connect to. Default - 25 for plain SMTP and 465 for
92 immediate SSL.
93
94 SSL - If the connection should be done from start with SSL,
95 contrary to later upgrade with "starttls". You can use SSL
96 arguments as documented in IO::Socket::SSL, but it will usually use
97 the right arguments already.
98
99 LocalAddr and LocalPort - These parameters are passed directly to
100 IO::Socket to allow binding the socket to a specific local address
101 and port.
102
103 Domain - This parameter is passed directly to IO::Socket and makes
104 it possible to enforce IPv4 connections even if IO::Socket::IP is
105 used as super class. Alternatively Family can be used.
106
107 Timeout - Maximum time, in seconds, to wait for a response from the
108 SMTP server (default: 120)
109
110 ExactAddresses - If true the all ADDRESS arguments must be as
111 defined by "addr-spec" in RFC2822. If not given, or false, then
112 Net::SMTP will attempt to extract the address from the value
113 passed.
114
115 Debug - Enable debugging information
116
117 Example:
118
119 $smtp = Net::SMTP->new('mailhost',
120 Hello => 'my.mail.domain',
121 Timeout => 30,
122 Debug => 1,
123 );
124
125 # the same
126 $smtp = Net::SMTP->new(
127 Host => 'mailhost',
128 Hello => 'my.mail.domain',
129 Timeout => 30,
130 Debug => 1,
131 );
132
133 # the same with direct SSL
134 $smtp = Net::SMTP->new('mailhost',
135 Hello => 'my.mail.domain',
136 Timeout => 30,
137 Debug => 1,
138 SSL => 1,
139 );
140
141 # Connect to the default server from Net::config
142 $smtp = Net::SMTP->new(
143 Hello => 'my.mail.domain',
144 Timeout => 30,
145 );
146
148 Unless otherwise stated all methods return either a true or false
149 value, with true meaning that the operation was a success. When a
150 method states that it returns a value, failure will be returned as
151 undef or an empty list.
152
153 "Net::SMTP" inherits from "Net::Cmd" so methods defined in "Net::Cmd"
154 may be used to send commands to the remote SMTP server in addition to
155 the methods documented here.
156
157 banner ()
158 Returns the banner message which the server replied with when the
159 initial connection was made.
160
161 domain ()
162 Returns the domain that the remote SMTP server identified itself as
163 during connection.
164
165 hello ( DOMAIN )
166 Tell the remote server the mail domain which you are in using the
167 EHLO command (or HELO if EHLO fails). Since this method is invoked
168 automatically when the Net::SMTP object is constructed the user
169 should normally not have to call it manually.
170
171 host ()
172 Returns the value used by the constructor, and passed to
173 IO::Socket::INET, to connect to the host.
174
175 etrn ( DOMAIN )
176 Request a queue run for the DOMAIN given.
177
178 starttls ( SSLARGS )
179 Upgrade existing plain connection to SSL. You can use SSL
180 arguments as documented in IO::Socket::SSL, but it will usually use
181 the right arguments already.
182
183 auth ( USERNAME, PASSWORD )
184 auth ( SASL )
185 Attempt SASL authentication. Requires Authen::SASL module. The
186 first form constructs a new Authen::SASL object using the given
187 username and password; the second form uses the given Authen::SASL
188 object.
189
190 mail ( ADDRESS [, OPTIONS] )
191 send ( ADDRESS )
192 send_or_mail ( ADDRESS )
193 send_and_mail ( ADDRESS )
194 Send the appropriate command to the server MAIL, SEND, SOML or
195 SAML. "ADDRESS" is the address of the sender. This initiates the
196 sending of a message. The method "recipient" should be called for
197 each address that the message is to be sent to.
198
199 The "mail" method can some additional ESMTP OPTIONS which is passed
200 in hash like fashion, using key and value pairs. Possible options
201 are:
202
203 Size => <bytes>
204 Return => "FULL" | "HDRS"
205 Bits => "7" | "8" | "binary"
206 Transaction => <ADDRESS>
207 Envelope => <ENVID> # xtext-encodes its argument
208 ENVID => <ENVID> # similar to Envelope, but expects argument encoded
209 XVERP => 1
210 AUTH => <submitter> # encoded address according to RFC 2554
211
212 The "Return" and "Envelope" parameters are used for DSN (Delivery
213 Status Notification).
214
215 The submitter address in "AUTH" option is expected to be in a
216 format as required by RFC 2554, in an RFC2821-quoted form and
217 xtext-encoded, or <> .
218
219 reset ()
220 Reset the status of the server. This may be called after a message
221 has been initiated, but before any data has been sent, to cancel
222 the sending of the message.
223
224 recipient ( ADDRESS [, ADDRESS, [...]] [, OPTIONS ] )
225 Notify the server that the current message should be sent to all of
226 the addresses given. Each address is sent as a separate command to
227 the server. Should the sending of any address result in a failure
228 then the process is aborted and a false value is returned. It is up
229 to the user to call "reset" if they so desire.
230
231 The "recipient" method can also pass additional case-sensitive
232 OPTIONS as an anonymous hash using key and value pairs. Possible
233 options are:
234
235 Notify => ['NEVER'] or ['SUCCESS','FAILURE','DELAY'] (see below)
236 ORcpt => <ORCPT>
237 SkipBad => 1 (to ignore bad addresses)
238
239 If "SkipBad" is true the "recipient" will not return an error when
240 a bad address is encountered and it will return an array of
241 addresses that did succeed.
242
243 $smtp->recipient($recipient1,$recipient2); # Good
244 $smtp->recipient($recipient1,$recipient2, { SkipBad => 1 }); # Good
245 $smtp->recipient($recipient1,$recipient2, { Notify => ['FAILURE','DELAY'], SkipBad => 1 }); # Good
246 @goodrecips=$smtp->recipient(@recipients, { Notify => ['FAILURE'], SkipBad => 1 }); # Good
247 $smtp->recipient("$recipient,$recipient2"); # BAD
248
249 Notify is used to request Delivery Status Notifications (DSNs), but
250 your SMTP/ESMTP service may not respect this request depending upon
251 its version and your site's SMTP configuration.
252
253 Leaving out the Notify option usually defaults an SMTP service to
254 its default behavior equivalent to ['FAILURE'] notifications only,
255 but again this may be dependent upon your site's SMTP
256 configuration.
257
258 The NEVER keyword must appear by itself if used within the Notify
259 option and "requests that a DSN not be returned to the sender under
260 any conditions."
261
262 {Notify => ['NEVER']}
263
264 $smtp->recipient(@recipients, { Notify => ['NEVER'], SkipBad => 1 }); # Good
265
266 You may use any combination of these three values
267 'SUCCESS','FAILURE','DELAY' in the anonymous array reference as
268 defined by RFC3461 (see http://www.ietf.org/rfc/rfc3461.txt for
269 more information. Note: quotations in this topic from same.).
270
271 A Notify parameter of 'SUCCESS' or 'FAILURE' "requests that a DSN
272 be issued on successful delivery or delivery failure,
273 respectively."
274
275 A Notify parameter of 'DELAY' "indicates the sender's willingness
276 to receive delayed DSNs. Delayed DSNs may be issued if delivery of
277 a message has been delayed for an unusual amount of time (as
278 determined by the Message Transfer Agent (MTA) at which the message
279 is delayed), but the final delivery status (whether successful or
280 failure) cannot be determined. The absence of the DELAY keyword in
281 a NOTIFY parameter requests that a "delayed" DSN NOT be issued
282 under any conditions."
283
284 {Notify => ['SUCCESS','FAILURE','DELAY']}
285
286 $smtp->recipient(@recipients, { Notify => ['FAILURE','DELAY'], SkipBad => 1 }); # Good
287
288 ORcpt is also part of the SMTP DSN extension according to RFC3461.
289 It is used to pass along the original recipient that the mail was
290 first sent to. The machine that generates a DSN will use this
291 address to inform the sender, because he can't know if recipients
292 get rewritten by mail servers. It is expected to be in a format as
293 required by RFC3461, xtext-encoded.
294
295 to ( ADDRESS [, ADDRESS [...]] )
296 cc ( ADDRESS [, ADDRESS [...]] )
297 bcc ( ADDRESS [, ADDRESS [...]] )
298 Synonyms for "recipient".
299
300 data ( [ DATA ] )
301 Initiate the sending of the data from the current message.
302
303 "DATA" may be a reference to a list or a list and must be encoded
304 by the caller to octets of whatever encoding is required, e.g. by
305 using the Encode module's "encode()" function.
306
307 If specified the contents of "DATA" and a termination string
308 ".\r\n" is sent to the server. The result will be true if the data
309 was accepted.
310
311 If "DATA" is not specified then the result will indicate that the
312 server wishes the data to be sent. The data must then be sent using
313 the "datasend" and "dataend" methods described in Net::Cmd.
314
315 bdat ( DATA )
316 bdatlast ( DATA )
317 Use the alternate DATA command "BDAT" of the data chunking service
318 extension defined in RFC1830 for efficiently sending large MIME
319 messages.
320
321 expand ( ADDRESS )
322 Request the server to expand the given address Returns an array
323 which contains the text read from the server.
324
325 verify ( ADDRESS )
326 Verify that "ADDRESS" is a legitimate mailing address.
327
328 Most sites usually disable this feature in their SMTP service
329 configuration. Use "Debug => 1" option under new() to see if
330 disabled.
331
332 help ( [ $subject ] )
333 Request help text from the server. Returns the text or undef upon
334 failure
335
336 quit ()
337 Send the QUIT command to the remote SMTP server and close the
338 socket connection.
339
340 can_inet6 ()
341 Returns whether we can use IPv6.
342
343 can_ssl ()
344 Returns whether we can use SSL.
345
347 Net::SMTP attempts to DWIM with addresses that are passed. For example
348 an application might extract The From: line from an email and pass that
349 to mail(). While this may work, it is not recommended. The application
350 should really use a module like Mail::Address to extract the mail
351 address and pass that.
352
353 If "ExactAddresses" is passed to the constructor, then addresses should
354 be a valid rfc2821-quoted address, although Net::SMTP will accept the
355 address surrounded by angle brackets.
356
357 funny user@domain WRONG
358 "funny user"@domain RIGHT, recommended
359 <"funny user"@domain> OK
360
362 Net::Cmd, IO::Socket::SSL
363
365 Graham Barr <gbarr@pobox.com>.
366
367 Steve Hay <shay@cpan.org> is now maintaining libnet as of version
368 1.22_02.
369
371 Copyright (C) 1995-2004 Graham Barr. All rights reserved.
372
373 Copyright (C) 2013-2016 Steve Hay. All rights reserved.
374
376 This module is free software; you can redistribute it and/or modify it
377 under the same terms as Perl itself, i.e. under the terms of either the
378 GNU General Public License or the Artistic License, as specified in the
379 LICENCE file.
380
381
382
383perl v5.26.3 2017-11-14 Net::SMTP(3)