home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / perl / 5.10.0 / Net / SMTP.pm < prev    next >
Encoding:
Perl POD Document  |  2009-06-26  |  22.2 KB  |  869 lines

  1. # Net::SMTP.pm
  2. #
  3. # Copyright (c) 1995-2004 Graham Barr <gbarr@pobox.com>. All rights reserved.
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the same terms as Perl itself.
  6.  
  7. package Net::SMTP;
  8.  
  9. require 5.001;
  10.  
  11. use strict;
  12. use vars qw($VERSION @ISA);
  13. use Socket 1.3;
  14. use Carp;
  15. use IO::Socket;
  16. use Net::Cmd;
  17. use Net::Config;
  18.  
  19. $VERSION = "2.31";
  20.  
  21. @ISA = qw(Net::Cmd IO::Socket::INET);
  22.  
  23.  
  24. sub new {
  25.   my $self = shift;
  26.   my $type = ref($self) || $self;
  27.   my ($host, %arg);
  28.   if (@_ % 2) {
  29.     $host = shift;
  30.     %arg  = @_;
  31.   }
  32.   else {
  33.     %arg  = @_;
  34.     $host = delete $arg{Host};
  35.   }
  36.   my $hosts = defined $host ? $host : $NetConfig{smtp_hosts};
  37.   my $obj;
  38.  
  39.   my $h;
  40.   foreach $h (@{ref($hosts) ? $hosts : [$hosts]}) {
  41.     $obj = $type->SUPER::new(
  42.       PeerAddr => ($host = $h),
  43.       PeerPort => $arg{Port} || 'smtp(25)',
  44.       LocalAddr => $arg{LocalAddr},
  45.       LocalPort => $arg{LocalPort},
  46.       Proto     => 'tcp',
  47.       Timeout   => defined $arg{Timeout}
  48.       ? $arg{Timeout}
  49.       : 120
  50.       )
  51.       and last;
  52.   }
  53.  
  54.   return undef
  55.     unless defined $obj;
  56.  
  57.   $obj->autoflush(1);
  58.  
  59.   $obj->debug(exists $arg{Debug} ? $arg{Debug} : undef);
  60.  
  61.   unless ($obj->response() == CMD_OK) {
  62.     $obj->close();
  63.     return undef;
  64.   }
  65.  
  66.   ${*$obj}{'net_smtp_exact_addr'} = $arg{ExactAddresses};
  67.   ${*$obj}{'net_smtp_host'}       = $host;
  68.  
  69.   (${*$obj}{'net_smtp_banner'}) = $obj->message;
  70.   (${*$obj}{'net_smtp_domain'}) = $obj->message =~ /\A\s*(\S+)/;
  71.  
  72.   unless ($obj->hello($arg{Hello} || "")) {
  73.     $obj->close();
  74.     return undef;
  75.   }
  76.  
  77.   $obj;
  78. }
  79.  
  80.  
  81. sub host {
  82.   my $me = shift;
  83.   ${*$me}{'net_smtp_host'};
  84. }
  85.  
  86. ##
  87. ## User interface methods
  88. ##
  89.  
  90.  
  91. sub banner {
  92.   my $me = shift;
  93.  
  94.   return ${*$me}{'net_smtp_banner'} || undef;
  95. }
  96.  
  97.  
  98. sub domain {
  99.   my $me = shift;
  100.  
  101.   return ${*$me}{'net_smtp_domain'} || undef;
  102. }
  103.  
  104.  
  105. sub etrn {
  106.   my $self = shift;
  107.   defined($self->supports('ETRN', 500, ["Command unknown: 'ETRN'"]))
  108.     && $self->_ETRN(@_);
  109. }
  110.  
  111.  
  112. sub auth {
  113.   my ($self, $username, $password) = @_;
  114.  
  115.   eval {
  116.     require MIME::Base64;
  117.     require Authen::SASL;
  118.   } or $self->set_status(500, ["Need MIME::Base64 and Authen::SASL todo auth"]), return 0;
  119.  
  120.   my $mechanisms = $self->supports('AUTH', 500, ["Command unknown: 'AUTH'"]);
  121.   return unless defined $mechanisms;
  122.  
  123.   my $sasl;
  124.  
  125.   if (ref($username) and UNIVERSAL::isa($username, 'Authen::SASL')) {
  126.     $sasl = $username;
  127.     $sasl->mechanism($mechanisms);
  128.   }
  129.   else {
  130.     die "auth(username, password)" if not length $username;
  131.     $sasl = Authen::SASL->new(
  132.       mechanism => $mechanisms,
  133.       callback  => {
  134.         user     => $username,
  135.         pass     => $password,
  136.         authname => $username,
  137.       }
  138.     );
  139.   }
  140.  
  141.   # We should probably allow the user to pass the host, but I don't
  142.   # currently know and SASL mechanisms that are used by smtp that need it
  143.   my $client = $sasl->client_new('smtp', ${*$self}{'net_smtp_host'}, 0);
  144.   my $str    = $client->client_start;
  145.  
  146.   # We dont support sasl mechanisms that encrypt the socket traffic.
  147.   # todo that we would really need to change the ISA hierarchy
  148.   # so we dont inherit from IO::Socket, but instead hold it in an attribute
  149.  
  150.   my @cmd = ("AUTH", $client->mechanism);
  151.   my $code;
  152.  
  153.   push @cmd, MIME::Base64::encode_base64($str, '')
  154.     if defined $str and length $str;
  155.  
  156.   while (($code = $self->command(@cmd)->response()) == CMD_MORE) {
  157.     @cmd = (
  158.       MIME::Base64::encode_base64(
  159.         $client->client_step(MIME::Base64::decode_base64(($self->message)[0])), ''
  160.       )
  161.     );
  162.   }
  163.  
  164.   $code == CMD_OK;
  165. }
  166.  
  167.  
  168. sub hello {
  169.   my $me     = shift;
  170.   my $domain = shift || "localhost.localdomain";
  171.   my $ok     = $me->_EHLO($domain);
  172.   my @msg    = $me->message;
  173.  
  174.   if ($ok) {
  175.     my $h = ${*$me}{'net_smtp_esmtp'} = {};
  176.     my $ln;
  177.     foreach $ln (@msg) {
  178.       $h->{uc $1} = $2
  179.         if $ln =~ /(\w+)\b[= \t]*([^\n]*)/;
  180.     }
  181.   }
  182.   elsif ($me->status == CMD_ERROR) {
  183.     @msg = $me->message
  184.       if $ok = $me->_HELO($domain);
  185.   }
  186.  
  187.   return undef unless $ok;
  188.  
  189.   $msg[0] =~ /\A\s*(\S+)/;
  190.   return ($1 || " ");
  191. }
  192.  
  193.  
  194. sub supports {
  195.   my $self = shift;
  196.   my $cmd  = uc shift;
  197.   return ${*$self}{'net_smtp_esmtp'}->{$cmd}
  198.     if exists ${*$self}{'net_smtp_esmtp'}->{$cmd};
  199.   $self->set_status(@_)
  200.     if @_;
  201.   return;
  202. }
  203.  
  204.  
  205. sub _addr {
  206.   my $self = shift;
  207.   my $addr = shift;
  208.   $addr = "" unless defined $addr;
  209.  
  210.   if (${*$self}{'net_smtp_exact_addr'}) {
  211.     return $1 if $addr =~ /^\s*(<.*>)\s*$/s;
  212.   }
  213.   else {
  214.     return $1 if $addr =~ /(<[^>]*>)/;
  215.     $addr =~ s/^\s+|\s+$//sg;
  216.   }
  217.  
  218.   "<$addr>";
  219. }
  220.  
  221.  
  222. sub mail {
  223.   my $me   = shift;
  224.   my $addr = _addr($me, shift);
  225.   my $opts = "";
  226.  
  227.   if (@_) {
  228.     my %opt = @_;
  229.     my ($k, $v);
  230.  
  231.     if (exists ${*$me}{'net_smtp_esmtp'}) {
  232.       my $esmtp = ${*$me}{'net_smtp_esmtp'};
  233.  
  234.       if (defined($v = delete $opt{Size})) {
  235.         if (exists $esmtp->{SIZE}) {
  236.           $opts .= sprintf " SIZE=%d", $v + 0;
  237.         }
  238.         else {
  239.           carp 'Net::SMTP::mail: SIZE option not supported by host';
  240.         }
  241.       }
  242.  
  243.       if (defined($v = delete $opt{Return})) {
  244.         if (exists $esmtp->{DSN}) {
  245.           $opts .= " RET=" . ((uc($v) eq "FULL") ? "FULL" : "HDRS");
  246.         }
  247.         else {
  248.           carp 'Net::SMTP::mail: DSN option not supported by host';
  249.         }
  250.       }
  251.  
  252.       if (defined($v = delete $opt{Bits})) {
  253.         if ($v eq "8") {
  254.           if (exists $esmtp->{'8BITMIME'}) {
  255.             $opts .= " BODY=8BITMIME";
  256.           }
  257.           else {
  258.             carp 'Net::SMTP::mail: 8BITMIME option not supported by host';
  259.           }
  260.         }
  261.         elsif ($v eq "binary") {
  262.           if (exists $esmtp->{'BINARYMIME'} && exists $esmtp->{'CHUNKING'}) {
  263.             $opts .= " BODY=BINARYMIME";
  264.             ${*$me}{'net_smtp_chunking'} = 1;
  265.           }
  266.           else {
  267.             carp 'Net::SMTP::mail: BINARYMIME option not supported by host';
  268.           }
  269.         }
  270.         elsif (exists $esmtp->{'8BITMIME'} or exists $esmtp->{'BINARYMIME'}) {
  271.           $opts .= " BODY=7BIT";
  272.         }
  273.         else {
  274.           carp 'Net::SMTP::mail: 8BITMIME and BINARYMIME options not supported by host';
  275.         }
  276.       }
  277.  
  278.       if (defined($v = delete $opt{Transaction})) {
  279.         if (exists $esmtp->{CHECKPOINT}) {
  280.           $opts .= " TRANSID=" . _addr($me, $v);
  281.         }
  282.         else {
  283.           carp 'Net::SMTP::mail: CHECKPOINT option not supported by host';
  284.         }
  285.       }
  286.  
  287.       if (defined($v = delete $opt{Envelope})) {
  288.         if (exists $esmtp->{DSN}) {
  289.           $v =~ s/([^\041-\176]|=|\+)/sprintf "+%02x", ord($1)/sge;
  290.           $opts .= " ENVID=$v";
  291.         }
  292.         else {
  293.           carp 'Net::SMTP::mail: DSN option not supported by host';
  294.         }
  295.       }
  296.  
  297.       if (defined($v = delete $opt{ENVID})) {
  298.  
  299.         # expected to be in a format as required by RFC 3461, xtext-encoded
  300.         if (exists $esmtp->{DSN}) {
  301.           $opts .= " ENVID=$v";
  302.         }
  303.         else {
  304.           carp 'Net::SMTP::mail: DSN option not supported by host';
  305.         }
  306.       }
  307.  
  308.       if (defined($v = delete $opt{AUTH})) {
  309.  
  310.         # expected to be in a format as required by RFC 2554,
  311.         # rfc2821-quoted and xtext-encoded, or <>
  312.         if (exists $esmtp->{AUTH}) {
  313.           $v = '<>' if !defined($v) || $v eq '';
  314.           $opts .= " AUTH=$v";
  315.         }
  316.         else {
  317.           carp 'Net::SMTP::mail: AUTH option not supported by host';
  318.         }
  319.       }
  320.  
  321.       if (defined($v = delete $opt{XVERP})) {
  322.         if (exists $esmtp->{'XVERP'}) {
  323.           $opts .= " XVERP";
  324.         }
  325.         else {
  326.           carp 'Net::SMTP::mail: XVERP option not supported by host';
  327.         }
  328.       }
  329.  
  330.       carp 'Net::SMTP::recipient: unknown option(s) ' . join(" ", keys %opt) . ' - ignored'
  331.         if scalar keys %opt;
  332.     }
  333.     else {
  334.       carp 'Net::SMTP::mail: ESMTP not supported by host - options discarded :-(';
  335.     }
  336.   }
  337.  
  338.   $me->_MAIL("FROM:" . $addr . $opts);
  339. }
  340.  
  341.  
  342. sub send          { my $me = shift; $me->_SEND("FROM:" . _addr($me, $_[0])) }
  343. sub send_or_mail  { my $me = shift; $me->_SOML("FROM:" . _addr($me, $_[0])) }
  344. sub send_and_mail { my $me = shift; $me->_SAML("FROM:" . _addr($me, $_[0])) }
  345.  
  346.  
  347. sub reset {
  348.   my $me = shift;
  349.  
  350.   $me->dataend()
  351.     if (exists ${*$me}{'net_smtp_lastch'});
  352.  
  353.   $me->_RSET();
  354. }
  355.  
  356.  
  357. sub recipient {
  358.   my $smtp     = shift;
  359.   my $opts     = "";
  360.   my $skip_bad = 0;
  361.  
  362.   if (@_ && ref($_[-1])) {
  363.     my %opt = %{pop(@_)};
  364.     my $v;
  365.  
  366.     $skip_bad = delete $opt{'SkipBad'};
  367.  
  368.     if (exists ${*$smtp}{'net_smtp_esmtp'}) {
  369.       my $esmtp = ${*$smtp}{'net_smtp_esmtp'};
  370.  
  371.       if (defined($v = delete $opt{Notify})) {
  372.         if (exists $esmtp->{DSN}) {
  373.           $opts .= " NOTIFY=" . join(",", map { uc $_ } @$v);
  374.         }
  375.         else {
  376.           carp 'Net::SMTP::recipient: DSN option not supported by host';
  377.         }
  378.       }
  379.  
  380.       if (defined($v = delete $opt{ORcpt})) {
  381.         if (exists $esmtp->{DSN}) {
  382.           $opts .= " ORCPT=" . $v;
  383.         }
  384.         else {
  385.           carp 'Net::SMTP::recipient: DSN option not supported by host';
  386.         }
  387.       }
  388.  
  389.       carp 'Net::SMTP::recipient: unknown option(s) ' . join(" ", keys %opt) . ' - ignored'
  390.         if scalar keys %opt;
  391.     }
  392.     elsif (%opt) {
  393.       carp 'Net::SMTP::recipient: ESMTP not supported by host - options discarded :-(';
  394.     }
  395.   }
  396.  
  397.   my @ok;
  398.   my $addr;
  399.   foreach $addr (@_) {
  400.     if ($smtp->_RCPT("TO:" . _addr($smtp, $addr) . $opts)) {
  401.       push(@ok, $addr) if $skip_bad;
  402.     }
  403.     elsif (!$skip_bad) {
  404.       return 0;
  405.     }
  406.   }
  407.  
  408.   return $skip_bad ? @ok : 1;
  409. }
  410.  
  411. BEGIN {
  412.   *to  = \&recipient;
  413.   *cc  = \&recipient;
  414.   *bcc = \&recipient;
  415. }
  416.  
  417.  
  418. sub data {
  419.   my $me = shift;
  420.  
  421.   if (exists ${*$me}{'net_smtp_chunking'}) {
  422.     carp 'Net::SMTP::data: CHUNKING extension in use, must call bdat instead';
  423.   }
  424.   else {
  425.     my $ok = $me->_DATA() && $me->datasend(@_);
  426.  
  427.     $ok && @_
  428.       ? $me->dataend
  429.       : $ok;
  430.   }
  431. }
  432.  
  433.  
  434. sub bdat {
  435.   my $me = shift;
  436.  
  437.   if (exists ${*$me}{'net_smtp_chunking'}) {
  438.     my $data = shift;
  439.  
  440.     $me->_BDAT(length $data)
  441.       && $me->rawdatasend($data)
  442.       && $me->response() == CMD_OK;
  443.   }
  444.   else {
  445.     carp 'Net::SMTP::bdat: CHUNKING extension is not in use, call data instead';
  446.   }
  447. }
  448.  
  449.  
  450. sub bdatlast {
  451.   my $me = shift;
  452.  
  453.   if (exists ${*$me}{'net_smtp_chunking'}) {
  454.     my $data = shift;
  455.  
  456.     $me->_BDAT(length $data, "LAST")
  457.       && $me->rawdatasend($data)
  458.       && $me->response() == CMD_OK;
  459.   }
  460.   else {
  461.     carp 'Net::SMTP::bdat: CHUNKING extension is not in use, call data instead';
  462.   }
  463. }
  464.  
  465.  
  466. sub datafh {
  467.   my $me = shift;
  468.   return unless $me->_DATA();
  469.   return $me->tied_fh;
  470. }
  471.  
  472.  
  473. sub expand {
  474.   my $me = shift;
  475.  
  476.   $me->_EXPN(@_)
  477.     ? ($me->message)
  478.     : ();
  479. }
  480.  
  481.  
  482. sub verify { shift->_VRFY(@_) }
  483.  
  484.  
  485. sub help {
  486.   my $me = shift;
  487.  
  488.   $me->_HELP(@_)
  489.     ? scalar $me->message
  490.     : undef;
  491. }
  492.  
  493.  
  494. sub quit {
  495.   my $me = shift;
  496.  
  497.   $me->_QUIT;
  498.   $me->close;
  499. }
  500.  
  501.  
  502. sub DESTROY {
  503.  
  504.   # ignore
  505. }
  506.  
  507. ##
  508. ## RFC821 commands
  509. ##
  510.  
  511.  
  512. sub _EHLO { shift->command("EHLO", @_)->response() == CMD_OK }
  513. sub _HELO { shift->command("HELO", @_)->response() == CMD_OK }
  514. sub _MAIL { shift->command("MAIL", @_)->response() == CMD_OK }
  515. sub _RCPT { shift->command("RCPT", @_)->response() == CMD_OK }
  516. sub _SEND { shift->command("SEND", @_)->response() == CMD_OK }
  517. sub _SAML { shift->command("SAML", @_)->response() == CMD_OK }
  518. sub _SOML { shift->command("SOML", @_)->response() == CMD_OK }
  519. sub _VRFY { shift->command("VRFY", @_)->response() == CMD_OK }
  520. sub _EXPN { shift->command("EXPN", @_)->response() == CMD_OK }
  521. sub _HELP { shift->command("HELP", @_)->response() == CMD_OK }
  522. sub _RSET { shift->command("RSET")->response() == CMD_OK }
  523. sub _NOOP { shift->command("NOOP")->response() == CMD_OK }
  524. sub _QUIT { shift->command("QUIT")->response() == CMD_OK }
  525. sub _DATA { shift->command("DATA")->response() == CMD_MORE }
  526. sub _BDAT { shift->command("BDAT", @_) }
  527. sub _TURN { shift->unsupported(@_); }
  528. sub _ETRN { shift->command("ETRN", @_)->response() == CMD_OK }
  529. sub _AUTH { shift->command("AUTH", @_)->response() == CMD_OK }
  530.  
  531. 1;
  532.  
  533. __END__
  534.  
  535. =head1 NAME
  536.  
  537. Net::SMTP - Simple Mail Transfer Protocol Client
  538.  
  539. =head1 SYNOPSIS
  540.  
  541.     use Net::SMTP;
  542.  
  543.     # Constructors
  544.     $smtp = Net::SMTP->new('mailhost');
  545.     $smtp = Net::SMTP->new('mailhost', Timeout => 60);
  546.  
  547. =head1 DESCRIPTION
  548.  
  549. This module implements a client interface to the SMTP and ESMTP
  550. protocol, enabling a perl5 application to talk to SMTP servers. This
  551. documentation assumes that you are familiar with the concepts of the
  552. SMTP protocol described in RFC821.
  553.  
  554. A new Net::SMTP object must be created with the I<new> method. Once
  555. this has been done, all SMTP commands are accessed through this object.
  556.  
  557. The Net::SMTP class is a subclass of Net::Cmd and IO::Socket::INET.
  558.  
  559. =head1 EXAMPLES
  560.  
  561. This example prints the mail domain name of the SMTP server known as mailhost:
  562.  
  563.     #!/usr/local/bin/perl -w
  564.  
  565.     use Net::SMTP;
  566.  
  567.     $smtp = Net::SMTP->new('mailhost');
  568.     print $smtp->domain,"\n";
  569.     $smtp->quit;
  570.  
  571. This example sends a small message to the postmaster at the SMTP server
  572. known as mailhost:
  573.  
  574.     #!/usr/local/bin/perl -w
  575.  
  576.     use Net::SMTP;
  577.  
  578.     $smtp = Net::SMTP->new('mailhost');
  579.  
  580.     $smtp->mail($ENV{USER});
  581.     $smtp->to('postmaster');
  582.  
  583.     $smtp->data();
  584.     $smtp->datasend("To: postmaster\n");
  585.     $smtp->datasend("\n");
  586.     $smtp->datasend("A simple test message\n");
  587.     $smtp->dataend();
  588.  
  589.     $smtp->quit;
  590.  
  591. =head1 CONSTRUCTOR
  592.  
  593. =over 4
  594.  
  595. =item new ( [ HOST ] [, OPTIONS ] )
  596.  
  597. This is the constructor for a new Net::SMTP object. C<HOST> is the
  598. name of the remote host to which an SMTP connection is required.
  599.  
  600. C<HOST> is optional. If C<HOST> is not given then it may instead be
  601. passed as the C<Host> option described below. If neither is given then
  602. the C<SMTP_Hosts> specified in C<Net::Config> will be used.
  603.  
  604. C<OPTIONS> are passed in a hash like fashion, using key and value pairs.
  605. Possible options are:
  606.  
  607. B<Hello> - SMTP requires that you identify yourself. This option
  608. specifies a string to pass as your mail domain. If not given localhost.localdomain
  609. will be used.
  610.  
  611. B<Host> - SMTP host to connect to. It may be a single scalar, as defined for
  612. the C<PeerAddr> option in L<IO::Socket::INET>, or a reference to
  613. an array with hosts to try in turn. The L</host> method will return the value
  614. which was used to connect to the host.
  615.  
  616. B<LocalAddr> and B<LocalPort> - These parameters are passed directly
  617. to IO::Socket to allow binding the socket to a local port.
  618.  
  619. B<Timeout> - Maximum time, in seconds, to wait for a response from the
  620. SMTP server (default: 120)
  621.  
  622. B<ExactAddresses> - If true the all ADDRESS arguments must be as
  623. defined by C<addr-spec> in RFC2822. If not given, or false, then
  624. Net::SMTP will attempt to extract the address from the value passed.
  625.  
  626. B<Debug> - Enable debugging information
  627.  
  628. B<Port> - Select a port on the remote host to connect to (default is 25)
  629.  
  630. Example:
  631.  
  632.  
  633.     $smtp = Net::SMTP->new('mailhost',
  634.                Hello => 'my.mail.domain',
  635.                Timeout => 30,
  636.                            Debug   => 1,
  637.               );
  638.  
  639.     # the same
  640.     $smtp = Net::SMTP->new(
  641.                Host => 'mailhost',
  642.                Hello => 'my.mail.domain',
  643.                Timeout => 30,
  644.                            Debug   => 1,
  645.               );
  646.  
  647.     # Connect to the default server from Net::config
  648.     $smtp = Net::SMTP->new(
  649.                Hello => 'my.mail.domain',
  650.                Timeout => 30,
  651.               );
  652.  
  653. =back
  654.  
  655. =head1 METHODS
  656.  
  657. Unless otherwise stated all methods return either a I<true> or I<false>
  658. value, with I<true> meaning that the operation was a success. When a method
  659. states that it returns a value, failure will be returned as I<undef> or an
  660. empty list.
  661.  
  662. =over 4
  663.  
  664. =item banner ()
  665.  
  666. Returns the banner message which the server replied with when the
  667. initial connection was made.
  668.  
  669. =item domain ()
  670.  
  671. Returns the domain that the remote SMTP server identified itself as during
  672. connection.
  673.  
  674. =item hello ( DOMAIN )
  675.  
  676. Tell the remote server the mail domain which you are in using the EHLO
  677. command (or HELO if EHLO fails).  Since this method is invoked
  678. automatically when the Net::SMTP object is constructed the user should
  679. normally not have to call it manually.
  680.  
  681. =item host ()
  682.  
  683. Returns the value used by the constructor, and passed to IO::Socket::INET,
  684. to connect to the host.
  685.  
  686. =item etrn ( DOMAIN )
  687.  
  688. Request a queue run for the DOMAIN given.
  689.  
  690. =item auth ( USERNAME, PASSWORD )
  691.  
  692. Attempt SASL authentication.
  693.  
  694. =item mail ( ADDRESS [, OPTIONS] )
  695.  
  696. =item send ( ADDRESS )
  697.  
  698. =item send_or_mail ( ADDRESS )
  699.  
  700. =item send_and_mail ( ADDRESS )
  701.  
  702. Send the appropriate command to the server MAIL, SEND, SOML or SAML. C<ADDRESS>
  703. is the address of the sender. This initiates the sending of a message. The
  704. method C<recipient> should be called for each address that the message is to
  705. be sent to.
  706.  
  707. The C<mail> method can some additional ESMTP OPTIONS which is passed
  708. in hash like fashion, using key and value pairs.  Possible options are:
  709.  
  710.  Size        => <bytes>
  711.  Return      => "FULL" | "HDRS"
  712.  Bits        => "7" | "8" | "binary"
  713.  Transaction => <ADDRESS>
  714.  Envelope    => <ENVID>     # xtext-encodes its argument
  715.  ENVID       => <ENVID>     # similar to Envelope, but expects argument encoded
  716.  XVERP       => 1
  717.  AUTH        => <submitter> # encoded address according to RFC 2554
  718.  
  719. The C<Return> and C<Envelope> parameters are used for DSN (Delivery
  720. Status Notification).
  721.  
  722. The submitter address in C<AUTH> option is expected to be in a format as
  723. required by RFC 2554, in an RFC2821-quoted form and xtext-encoded, or <> .
  724.  
  725. =item reset ()
  726.  
  727. Reset the status of the server. This may be called after a message has been 
  728. initiated, but before any data has been sent, to cancel the sending of the
  729. message.
  730.  
  731. =item recipient ( ADDRESS [, ADDRESS, [...]] [, OPTIONS ] )
  732.  
  733. Notify the server that the current message should be sent to all of the
  734. addresses given. Each address is sent as a separate command to the server.
  735. Should the sending of any address result in a failure then the process is
  736. aborted and a I<false> value is returned. It is up to the user to call
  737. C<reset> if they so desire.
  738.  
  739. The C<recipient> method can also pass additional case-sensitive OPTIONS as an
  740. anonymous hash using key and value pairs.  Possible options are:
  741.  
  742.   Notify  => ['NEVER'] or ['SUCCESS','FAILURE','DELAY']  (see below)
  743.   ORcpt   => <ORCPT>
  744.   SkipBad => 1        (to ignore bad addresses)
  745.  
  746. If C<SkipBad> is true the C<recipient> will not return an error when a bad
  747. address is encountered and it will return an array of addresses that did
  748. succeed.
  749.  
  750.   $smtp->recipient($recipient1,$recipient2);  # Good
  751.   $smtp->recipient($recipient1,$recipient2, { SkipBad => 1 });  # Good
  752.   $smtp->recipient($recipient1,$recipient2, { Notify => ['FAILURE','DELAY'], SkipBad => 1 });  # Good
  753.   @goodrecips=$smtp->recipient(@recipients, { Notify => ['FAILURE'], SkipBad => 1 });  # Good
  754.   $smtp->recipient("$recipient,$recipient2"); # BAD
  755.  
  756. Notify is used to request Delivery Status Notifications (DSNs), but your
  757. SMTP/ESMTP service may not respect this request depending upon its version and
  758. your site's SMTP configuration.
  759.  
  760. Leaving out the Notify option usually defaults an SMTP service to its default
  761. behavior equivalent to ['FAILURE'] notifications only, but again this may be
  762. dependent upon your site's SMTP configuration.
  763.  
  764. The NEVER keyword must appear by itself if used within the Notify option and "requests
  765. that a DSN not be returned to the sender under any conditions."
  766.  
  767.   {Notify => ['NEVER']}
  768.  
  769.   $smtp->recipient(@recipients, { Notify => ['NEVER'], SkipBad => 1 });  # Good
  770.  
  771. You may use any combination of these three values 'SUCCESS','FAILURE','DELAY' in
  772. the anonymous array reference as defined by RFC3461 (see http://rfc.net/rfc3461.html
  773. for more information.  Note: quotations in this topic from same.).
  774.  
  775. A Notify parameter of 'SUCCESS' or 'FAILURE' "requests that a DSN be issued on
  776. successful delivery or delivery failure, respectively."
  777.  
  778. A Notify parameter of 'DELAY' "indicates the sender's willingness to receive
  779. delayed DSNs.  Delayed DSNs may be issued if delivery of a message has been
  780. delayed for an unusual amount of time (as determined by the Message Transfer
  781. Agent (MTA) at which the message is delayed), but the final delivery status
  782. (whether successful or failure) cannot be determined.  The absence of the DELAY
  783. keyword in a NOTIFY parameter requests that a "delayed" DSN NOT be issued under
  784. any conditions."
  785.  
  786.   {Notify => ['SUCCESS','FAILURE','DELAY']}
  787.  
  788.   $smtp->recipient(@recipients, { Notify => ['FAILURE','DELAY'], SkipBad => 1 });  # Good
  789.  
  790. ORcpt is also part of the SMTP DSN extension according to RFC3461.
  791. It is used to pass along the original recipient that the mail was first
  792. sent to.  The machine that generates a DSN will use this address to inform
  793. the sender, because he can't know if recipients get rewritten by mail servers.
  794. It is expected to be in a format as required by RFC3461, xtext-encoded.
  795.  
  796. =item to ( ADDRESS [, ADDRESS [...]] )
  797.  
  798. =item cc ( ADDRESS [, ADDRESS [...]] )
  799.  
  800. =item bcc ( ADDRESS [, ADDRESS [...]] )
  801.  
  802. Synonyms for C<recipient>.
  803.  
  804. =item data ( [ DATA ] )
  805.  
  806. Initiate the sending of the data from the current message. 
  807.  
  808. C<DATA> may be a reference to a list or a list. If specified the contents
  809. of C<DATA> and a termination string C<".\r\n"> is sent to the server. And the
  810. result will be true if the data was accepted.
  811.  
  812. If C<DATA> is not specified then the result will indicate that the server
  813. wishes the data to be sent. The data must then be sent using the C<datasend>
  814. and C<dataend> methods described in L<Net::Cmd>.
  815.  
  816. =item expand ( ADDRESS )
  817.  
  818. Request the server to expand the given address Returns an array
  819. which contains the text read from the server.
  820.  
  821. =item verify ( ADDRESS )
  822.  
  823. Verify that C<ADDRESS> is a legitimate mailing address.
  824.  
  825. Most sites usually disable this feature in their SMTP service configuration.
  826. Use "Debug => 1" option under new() to see if disabled.
  827.  
  828. =item help ( [ $subject ] )
  829.  
  830. Request help text from the server. Returns the text or undef upon failure
  831.  
  832. =item quit ()
  833.  
  834. Send the QUIT command to the remote SMTP server and close the socket connection.
  835.  
  836. =back
  837.  
  838. =head1 ADDRESSES
  839.  
  840. Net::SMTP attempts to DWIM with addresses that are passed. For
  841. example an application might extract The From: line from an email
  842. and pass that to mail(). While this may work, it is not recommended.
  843. The application should really use a module like L<Mail::Address>
  844. to extract the mail address and pass that.
  845.  
  846. If C<ExactAddresses> is passed to the constructor, then addresses
  847. should be a valid rfc2821-quoted address, although Net::SMTP will
  848. accept accept the address surrounded by angle brackets.
  849.  
  850.  funny user@domain      WRONG
  851.  "funny user"@domain    RIGHT, recommended
  852.  <"funny user"@domain>  OK
  853.  
  854. =head1 SEE ALSO
  855.  
  856. L<Net::Cmd>
  857.  
  858. =head1 AUTHOR
  859.  
  860. Graham Barr <gbarr@pobox.com>
  861.  
  862. =head1 COPYRIGHT
  863.  
  864. Copyright (c) 1995-2004 Graham Barr. All rights reserved.
  865. This program is free software; you can redistribute it and/or modify
  866. it under the same terms as Perl itself.
  867.  
  868. =cut
  869.