{
    "mode": "perldoc",
    "parameter": "Net::SMTP",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Net%3A%3ASMTP/json",
    "generated": "2026-06-09T16:28:51Z",
    "synopsis": "use Net::SMTP;\n# Constructors\n$smtp = Net::SMTP->new('mailhost');\n$smtp = Net::SMTP->new('mailhost', Timeout => 60);",
    "sections": {
        "NAME": {
            "content": "Net::SMTP - Simple Mail Transfer Protocol Client\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use Net::SMTP;\n\n# Constructors\n$smtp = Net::SMTP->new('mailhost');\n$smtp = Net::SMTP->new('mailhost', Timeout => 60);\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "This module implements a client interface to the SMTP and ESMTP protocol, enabling a perl5\napplication to talk to SMTP servers. This documentation assumes that you are familiar with the\nconcepts of the SMTP protocol described in RFC2821. With IO::Socket::SSL installed it also\nprovides support for implicit and explicit TLS encryption, i.e. SMTPS or SMTP+STARTTLS.\n\nThe Net::SMTP class is a subclass of Net::Cmd and (depending on avaibility) of IO::Socket::IP,\nIO::Socket::INET6 or IO::Socket::INET.\n",
            "subsections": [
                {
                    "name": "Class Methods",
                    "content": "\"new([$host][, %options])\"\nThis is the constructor for a new Net::SMTP object. $host is the name of the remote host to\nwhich an SMTP connection is required.\n\nOn failure \"undef\" will be returned and $@ will contain the reason for the failure.\n\n$host is optional. If $host is not given then it may instead be passed as the \"Host\" option\ndescribed below. If neither is given then the \"SMTPHosts\" specified in \"Net::Config\" will\nbe used.\n\n%options are passed in a hash like fashion, using key and value pairs. Possible options are:\n\nHello - SMTP requires that you identify yourself. This option specifies a string to pass as\nyour mail domain. If not given localhost.localdomain will be used.\n\nSendHello - If false then the EHLO (or HELO) command that is normally sent when constructing\nthe object will not be sent. In that case the command will have to be sent manually by\ncalling \"hello()\" instead.\n\nHost - SMTP host to connect to. It may be a single scalar (hostname[:port]), as defined for\nthe \"PeerAddr\" option in IO::Socket::INET, or a reference to an array with hosts to try in\nturn. The \"host\" method will return the value which was used to connect to the host. Format\n- \"PeerHost\" from IO::Socket::INET new method.\n\nPort - port to connect to. Default - 25 for plain SMTP and 465 for immediate SSL.\n\nSSL - If the connection should be done from start with SSL, contrary to later upgrade with\n\"starttls\". You can use SSL arguments as documented in IO::Socket::SSL, but it will usually\nuse the right arguments already.\n\nLocalAddr and LocalPort - These parameters are passed directly to IO::Socket to allow\nbinding the socket to a specific local address and port.\n\nDomain - This parameter is passed directly to IO::Socket and makes it possible to enforce\nIPv4 connections even if IO::Socket::IP is used as super class. Alternatively Family can be\nused.\n\nTimeout - Maximum time, in seconds, to wait for a response from the SMTP server (default:\n120)\n\nExactAddresses - If true then all $address arguments must be as defined by \"addr-spec\" in\nRFC2822. If not given, or false, then Net::SMTP will attempt to extract the address from the\nvalue passed.\n\nDebug - Enable debugging information\n\nExample:\n\n$smtp = Net::SMTP->new('mailhost',\nHello => 'my.mail.domain',\nTimeout => 30,\nDebug   => 1,\n);\n\n# the same\n$smtp = Net::SMTP->new(\nHost => 'mailhost',\nHello => 'my.mail.domain',\nTimeout => 30,\nDebug   => 1,\n);\n\n# the same with direct SSL\n$smtp = Net::SMTP->new('mailhost',\nHello => 'my.mail.domain',\nTimeout => 30,\nDebug   => 1,\nSSL     => 1,\n);\n\n# Connect to the default server from Net::config\n$smtp = Net::SMTP->new(\nHello => 'my.mail.domain',\nTimeout => 30,\n);\n"
                }
            ]
        },
        "Object Methods": {
            "content": "Unless otherwise stated all methods return either a *true* or *false* value, with *true* meaning\nthat the operation was a success. When a method states that it returns a value, failure will be\nreturned as *undef* or an empty list.\n\n\"Net::SMTP\" inherits from \"Net::Cmd\" so methods defined in \"Net::Cmd\" may be used to send\ncommands to the remote SMTP server in addition to the methods documented here.\n\n\"banner()\"\nReturns the banner message which the server replied with when the initial connection was\nmade.\n\n\"domain()\"\nReturns the domain that the remote SMTP server identified itself as during connection.\n\n\"hello($domain)\"\nTell the remote server the mail domain which you are in using the EHLO command (or HELO if\nEHLO fails). Since this method is invoked automatically when the Net::SMTP object is\nconstructed the user should normally not have to call it manually.\n\n\"host()\"\nReturns the value used by the constructor, and passed to IO::Socket::INET, to connect to the\nhost.\n\n\"etrn($domain)\"\nRequest a queue run for the $domain given.\n\n\"starttls(%sslargs)\"\nUpgrade existing plain connection to SSL. You can use SSL arguments as documented in\nIO::Socket::SSL, but it will usually use the right arguments already.\n\n\"auth($username, $password)\"\n\"auth($sasl)\"\nAttempt SASL authentication. Requires Authen::SASL module. The first form constructs a new\nAuthen::SASL object using the given username and password; the second form uses the given\nAuthen::SASL object.\n\n\"mail($address[, %options])\"\n\"send($address)\"\n\"sendormail($address)\"\n\"sendandmail($address)\"\nSend the appropriate command to the server MAIL, SEND, SOML or SAML. $address is the address\nof the sender. This initiates the sending of a message. The method \"recipient\" should be\ncalled for each address that the message is to be sent to.\n\nThe \"mail\" method can take some additional ESMTP %options which is passed in hash like\nfashion, using key and value pairs. Possible options are:\n\nSize        => <bytes>\nReturn      => \"FULL\" | \"HDRS\"\nBits        => \"7\" | \"8\" | \"binary\"\nTransaction => <ADDRESS>\nEnvelope    => <ENVID>     # xtext-encodes its argument\nENVID       => <ENVID>     # similar to Envelope, but expects argument encoded\nXVERP       => 1\nAUTH        => <submitter> # encoded address according to RFC 2554\n\nThe \"Return\" and \"Envelope\" parameters are used for DSN (Delivery Status Notification).\n\nThe submitter address in \"AUTH\" option is expected to be in a format as required by RFC\n2554, in an RFC2821-quoted form and xtext-encoded, or <> .\n\n\"reset()\"\nReset the status of the server. This may be called after a message has been initiated, but\nbefore any data has been sent, to cancel the sending of the message.\n\n\"recipient($address[, $address[, ...]][, %options])\"\nNotify the server that the current message should be sent to all of the addresses given.\nEach address is sent as a separate command to the server. Should the sending of any address\nresult in a failure then the process is aborted and a *false* value is returned. It is up to\nthe user to call \"reset\" if they so desire.\n\nThe \"recipient\" method can also pass additional case-sensitive %options as an anonymous hash\nusing key and value pairs. Possible options are:\n\nNotify  => ['NEVER'] or ['SUCCESS','FAILURE','DELAY']  (see below)\nORcpt   => <ORCPT>\nSkipBad => 1        (to ignore bad addresses)\n\nIf \"SkipBad\" is true the \"recipient\" will not return an error when a bad address is\nencountered and it will return an array of addresses that did succeed.\n\n$smtp->recipient($recipient1,$recipient2);  # Good\n$smtp->recipient($recipient1,$recipient2, { SkipBad => 1 });  # Good\n$smtp->recipient($recipient1,$recipient2, { Notify => ['FAILURE','DELAY'], SkipBad => 1 });  # Good\n@goodrecips=$smtp->recipient(@recipients, { Notify => ['FAILURE'], SkipBad => 1 });  # Good\n$smtp->recipient(\"$recipient,$recipient2\"); # BAD\n\nNotify is used to request Delivery Status Notifications (DSNs), but your SMTP/ESMTP service\nmay not respect this request depending upon its version and your site's SMTP configuration.\n\nLeaving out the Notify option usually defaults an SMTP service to its default behavior\nequivalent to ['FAILURE'] notifications only, but again this may be dependent upon your\nsite's SMTP configuration.\n\nThe NEVER keyword must appear by itself if used within the Notify option and \"requests that\na DSN not be returned to the sender under any conditions.\"\n\n{Notify => ['NEVER']}\n\n$smtp->recipient(@recipients, { Notify => ['NEVER'], SkipBad => 1 });  # Good\n\nYou may use any combination of these three values 'SUCCESS','FAILURE','DELAY' in the\nanonymous array reference as defined by RFC3461 (see <https://www.ietf.org/rfc/rfc3461.txt>\nfor more information. Note: quotations in this topic from same.).\n\nA Notify parameter of 'SUCCESS' or 'FAILURE' \"requests that a DSN be issued on successful\ndelivery or delivery failure, respectively.\"\n\nA Notify parameter of 'DELAY' \"indicates the sender's willingness to receive delayed DSNs.\nDelayed DSNs may be issued if delivery of a message has been delayed for an unusual amount\nof time (as determined by the Message Transfer Agent (MTA) at which the message is delayed),\nbut the final delivery status (whether successful or failure) cannot be determined. The\nabsence of the DELAY keyword in a NOTIFY parameter requests that a \"delayed\" DSN NOT be\nissued under any conditions.\"\n\n{Notify => ['SUCCESS','FAILURE','DELAY']}\n\n$smtp->recipient(@recipients, { Notify => ['FAILURE','DELAY'], SkipBad => 1 });  # Good\n\nORcpt is also part of the SMTP DSN extension according to RFC3461. It is used to pass along\nthe original recipient that the mail was first sent to. The machine that generates a DSN\nwill use this address to inform the sender, because he can't know if recipients get\nrewritten by mail servers. It is expected to be in a format as required by RFC3461,\nxtext-encoded.\n\n\"to($address[, $address[, ...]])\"\n\"cc($address[, $address[, ...]])\"\n\"bcc($address[, $address[, ...]])\"\nSynonyms for \"recipient\".\n\n\"data([$data])\"\nInitiate the sending of the data from the current message.\n\n$data may be a reference to a list or a list and must be encoded by the caller to octets of\nwhatever encoding is required, e.g. by using the Encode module's \"encode()\" function.\n\nIf specified the contents of $data and a termination string \".\\r\\n\" is sent to the server.\nThe result will be true if the data was accepted.\n\nIf $data is not specified then the result will indicate that the server wishes the data to\nbe sent. The data must then be sent using the \"datasend\" and \"dataend\" methods described in\nNet::Cmd.\n\n\"bdat($data)\"\n\"bdatlast($data)\"\nUse the alternate $data command \"BDAT\" of the data chunking service extension defined in\nRFC1830 for efficiently sending large MIME messages.\n\n\"expand($address)\"\nRequest the server to expand the given address Returns an array which contains the text read\nfrom the server.\n\n\"verify($address)\"\nVerify that $address is a legitimate mailing address.\n\nMost sites usually disable this feature in their SMTP service configuration. Use \"Debug =>\n1\" option under new() to see if disabled.\n\n\"help([$subject])\"\nRequest help text from the server. Returns the text or undef upon failure\n\n\"quit()\"\nSend the QUIT command to the remote SMTP server and close the socket connection.\n\n\"caninet6()\"\nReturns whether we can use IPv6.\n\n\"canssl()\"\nReturns whether we can use SSL.\n",
            "subsections": [
                {
                    "name": "Addresses",
                    "content": "Net::SMTP attempts to DWIM with addresses that are passed. For example an application might\nextract The From: line from an email and pass that to mail(). While this may work, it is not\nrecommended. The application should really use a module like Mail::Address to extract the mail\naddress and pass that.\n\nIf \"ExactAddresses\" is passed to the constructor, then addresses should be a valid\nrfc2821-quoted address, although Net::SMTP will accept the address surrounded by angle brackets.\n\nfunny user@domain      WRONG\n\"funny user\"@domain    RIGHT, recommended\n<\"funny user\"@domain>  OK\n"
                }
            ]
        },
        "EXAMPLES": {
            "content": "This example prints the mail domain name of the SMTP server known as mailhost:\n\n#!/usr/local/bin/perl -w\n\nuse Net::SMTP;\n\n$smtp = Net::SMTP->new('mailhost');\nprint $smtp->domain,\"\\n\";\n$smtp->quit;\n\nThis example sends a small message to the postmaster at the SMTP server known as mailhost:\n\n#!/usr/local/bin/perl -w\n\nuse Net::SMTP;\n\nmy $smtp = Net::SMTP->new('mailhost');\n\n$smtp->mail($ENV{USER});\nif ($smtp->to('postmaster')) {\n$smtp->data();\n$smtp->datasend(\"To: postmaster\\n\");\n$smtp->datasend(\"\\n\");\n$smtp->datasend(\"A simple test message\\n\");\n$smtp->dataend();\n} else {\nprint \"Error: \", $smtp->message();\n}\n\n$smtp->quit;\n",
            "subsections": []
        },
        "EXPORTS": {
            "content": "*None*.\n",
            "subsections": []
        },
        "KNOWN BUGS": {
            "content": "See <https://rt.cpan.org/Dist/Display.html?Status=Active&Queue=libnet>.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "Net::Cmd, IO::Socket::SSL.\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Graham Barr <gbarr@pobox.com <mailto:gbarr@pobox.com>>.\n\nSteve Hay <shay@cpan.org <mailto:shay@cpan.org>> is now maintaining libnet as of version\n1.2202.\n",
            "subsections": []
        },
        "COPYRIGHT": {
            "content": "Copyright (C) 1995-2004 Graham Barr. All rights reserved.\n\nCopyright (C) 2013-2016, 2020 Steve Hay. All rights reserved.\n",
            "subsections": []
        },
        "LICENCE": {
            "content": "This module is free software; you can redistribute it and/or modify it under the same terms as\nPerl itself, i.e. under the terms of either the GNU General Public License or the Artistic\nLicense, as specified in the LICENCE file.\n",
            "subsections": []
        },
        "VERSION": {
            "content": "Version 3.13\n",
            "subsections": []
        },
        "DATE": {
            "content": "23 Dec 2020\n",
            "subsections": []
        },
        "HISTORY": {
            "content": "See the Changes file.\n",
            "subsections": []
        }
    },
    "summary": "Net::SMTP - Simple Mail Transfer Protocol Client",
    "flags": [],
    "examples": [
        "This example prints the mail domain name of the SMTP server known as mailhost:",
        "#!/usr/local/bin/perl -w",
        "use Net::SMTP;",
        "$smtp = Net::SMTP->new('mailhost');",
        "print $smtp->domain,\"\\n\";",
        "$smtp->quit;",
        "This example sends a small message to the postmaster at the SMTP server known as mailhost:",
        "#!/usr/local/bin/perl -w",
        "use Net::SMTP;",
        "my $smtp = Net::SMTP->new('mailhost');",
        "$smtp->mail($ENV{USER});",
        "if ($smtp->to('postmaster')) {",
        "$smtp->data();",
        "$smtp->datasend(\"To: postmaster\\n\");",
        "$smtp->datasend(\"\\n\");",
        "$smtp->datasend(\"A simple test message\\n\");",
        "$smtp->dataend();",
        "} else {",
        "print \"Error: \", $smtp->message();",
        "$smtp->quit;"
    ],
    "see_also": []
}