Copied!

PHPMailer RFC821 SMTP email transport class.

Implements RFC 821 SMTP commands and provides some utility methods for sending mail to an SMTP server.

CloneableInstantiable
Constants
public PHPMailer\PHPMailer\SMTP::DEBUG_CLIENT = 1
 

Debug level to show client -> server messages.

  • var int
public PHPMailer\PHPMailer\SMTP::DEBUG_CONNECTION = 3
 

Debug level to show connection status, client -> server and server -> client messages.

  • var int
public PHPMailer\PHPMailer\SMTP::DEBUG_LOWLEVEL = 4
 

Debug level to show all messages.

  • var int
public PHPMailer\PHPMailer\SMTP::DEBUG_OFF = 0
 

Debug level for no output.

  • var int
public PHPMailer\PHPMailer\SMTP::DEBUG_SERVER = 2
 

Debug level to show client -> server and server -> client messages.

  • var int
public PHPMailer\PHPMailer\SMTP::DEFAULT_PORT = 25
 

The SMTP port to use if one is not specified.

  • var int
public PHPMailer\PHPMailer\SMTP::DEFAULT_SECURE_PORT = 465
 

The SMTPs port to use if one is not specified.

  • var int
public PHPMailer\PHPMailer\SMTP::LE = ' '
 

SMTP line break constant.

  • var string
public PHPMailer\PHPMailer\SMTP::MAX_LINE_LENGTH = 998
 

The maximum line length allowed by RFC 5321 section 4.5.3.1.6, excluding a trailing CRLF break.

  • see https://www.rfc-editor.org/rfc/rfc5321#section-4.5.3.1.6
  • var int
public PHPMailer\PHPMailer\SMTP::MAX_REPLY_LENGTH = 512
 

The maximum line length allowed for replies in RFC 5321 section 4.5.3.1.5, including a trailing CRLF line break.

  • see https://www.rfc-editor.org/rfc/rfc5321#section-4.5.3.1.5
  • var int
public PHPMailer\PHPMailer\SMTP::VERSION = '6.10.0'
 

The PHPMailer SMTP version number.

  • var string
Properties
public $Debugoutput = 'echo'
 

How to handle debug output.

Options:

  • echo Output plain-text as-is, appropriate for CLI
  • html Output escaped, line breaks converted to <br>, appropriate for browser output
  • error_log Output to error log as configured in php.ini Alternatively, you can provide a callable expecting two params: a message string and the debug level:
$smtp->Debugoutput = function($str, $level) {echo "debug level $level; message: $str";};

Alternatively, you can pass in an instance of a PSR-3 compatible logger, though only debug level output is used:

$mail->Debugoutput = new myPsr3Logger;
  • var string|callable|\Psr\Log\LoggerInterface
public $do_debug = 0
 

Debug output level.

Options:

  • self::DEBUG_OFF (0) No debug output, default
  • self::DEBUG_CLIENT (1) Client commands
  • self::DEBUG_SERVER (2) Client commands and server responses
  • self::DEBUG_CONNECTION (3) As DEBUG_SERVER plus connection status
  • self::DEBUG_LOWLEVEL (4) Low-level data output, all messages.
  • var int
public $do_smtputf8 = false
 

Whether to use SMTPUTF8.

  • see https://www.rfc-editor.org/rfc/rfc6531
  • var bool
public $do_verp = false
 

Whether to use VERP.

  • see https://en.wikipedia.org/wiki/Variable_envelope_return_path
  • see https://www.postfix.org/VERP_README.htmlInfo on VERP
  • var bool
public $Timelimit = 300
 

How long to wait for commands to complete, in seconds.

Default of 5 minutes (300sec) is from RFC2821 section 4.5.3.2.

  • var int
public $Timeout = 300
 

The timeout value for connection, in seconds.

Default of 5 minutes (300sec) is from RFC2821 section 4.5.3.2. This needs to be quite high to function correctly with hosts using greetdelay as an anti-spam measure.

  • see https://www.rfc-editor.org/rfc/rfc2821#section-4.5.3.2
  • var int
public static $xclient_allowed_attributes = ['NAME', 'ADDR', 'PORT', 'PROTO', 'HELO', 'LOGIN', 'DESTADDR', 'DESTPORT']
 

Allowed SMTP XCLIENT attributes.

Must be allowed by the SMTP server. EHLO response is not checked.

  • see https://www.postfix.org/XCLIENT_README.html
  • var array
Methods
public authenticate( $username, $password, $authtype = NULL, $OAuth = NULL)
 

Perform SMTP authentication.

Must be run after hello().

  • see \hello()
  • param string $username The user name
  • param string $password The password
  • param string $authtype The auth type (CRAM-MD5, PLAIN, LOGIN, XOAUTH2)
  • param \OAuthTokenProvider $OAuth An optional OAuthTokenProvider instance for XOAUTH2 authentication
  • return bool True if successfully authenticated
public client_send( $data, $command = '')
 

Send raw data to the server.

  • param string $data The data to send
  • param string $command Optionally, the command this is part of, used only for controlling debug output
  • return int|bool The number of bytes sent to the server or false on error
public close()
 

Close the socket and clean up the state of the class.

Don't use this function without first trying to use QUIT.

  • see \quit()
public connect( $host, $port = NULL, $timeout = 30, $options = [])
 

Connect to an SMTP server.

  • param string $host SMTP server IP or host name
  • param int $port The port number to connect to
  • param int $timeout How long to wait for the connection to open
  • param array $options An array of options for stream_context_create()
  • return bool
public connected()
 

Check connection state.

  • return bool True if connected
public data( $msg_data)
 

Send an SMTP DATA command.

Issues a data command and sends the msg_data to the server, finalizing the mail transaction. $msg_data is the message that is to be sent with the headers. Each header needs to be on a single line followed by a <CRLF> with the message headers and the message body being separated by an additional <CRLF>. Implements RFC 821: DATA <CRLF>.

  • param string $msg_data Message data to send
  • return bool
public getDebugLevel()
 

Get debug output level.

  • return int
public getDebugOutput()
 

Get debug output method.

  • return string
public getError()
 

Get the latest error.

  • return array
public getLastReply()
 

Get the last reply from the server.

  • return string
public getLastTransactionID()
 

Get the queue/transaction ID of the last SMTP transaction If no reply has been received yet, it will return null.

If no pattern was matched, it will return false.

  • return bool|string|null
  • see \recordLastTransactionID()
public getServerExt( $name)
 

Get metadata about the SMTP server from its HELO/EHLO response.

The method works in three ways, dependent on argument value and current state:

  1. HELO/EHLO has not been sent - returns null and populates $this->error.
  2. HELO has been sent - $name == 'HELO': returns server name $name == 'EHLO': returns boolean false $name == any other string: returns null and populates $this->error
  3. EHLO has been sent - $name == 'HELO'|'EHLO': returns the server name $name == any other string: if extension $name exists, returns True or its options (e.g. AUTH mechanisms supported). Otherwise returns False.
  • param string $name Name of SMTP extension or 'HELO'|'EHLO'
  • return string|bool|null
public getServerExtList()
 

Get SMTP extensions available on the server.

  • return array|null
public getSMTPUTF8()
 

Get SMTPUTF8 use.

  • return bool
public getTimeout()
 

Get SMTP timeout.

  • return int
public getVerp()
 

Get VERP address generation mode.

  • return bool
public hello( $host = '')
 

Send an SMTP HELO or EHLO command.

Used to identify the sending server to the receiving server. This makes sure that client and server are in a known state. Implements RFC 821: HELO <SP> <CRLF> and RFC 2821 EHLO.

  • param string $host The host name or IP to connect to
  • return bool
public mail( $from)
 

Send an SMTP MAIL command.

Starts a mail transaction from the email address specified in $from. Returns true if successful or false otherwise. If True the mail transaction is started and then one or more recipient commands may be called followed by a data command. Implements RFC 821: MAIL <SP> FROM: <CRLF> and two extensions, namely XVERP and SMTPUTF8.

The server's EHLO response is not checked. If use of either extensions is enabled even though the server does not support that, mail submission will fail.

XVERP is documented at https://www.postfix.org/VERP_README.html and SMTPUTF8 is specified in RFC 6531.

  • param string $from Source address of this message
  • return bool
public noop()
 

Send an SMTP NOOP command.

Used to keep keep-alives alive, doesn't actually do anything.

  • return bool
public quit( $close_on_error = true)
 

Send an SMTP QUIT command.

Closes the socket if there is no error or the $close_on_error argument is true. Implements from RFC 821: QUIT <CRLF>.

  • param bool $close_on_error Should the connection close if an error occurs?
  • return bool
public recipient( $address, $dsn = '')
 

Send an SMTP RCPT command.

Sets the TO argument to $toaddr. Returns true if the recipient was accepted false if it was rejected. Implements from RFC 821: RCPT <SP> TO: <CRLF>.

  • param string $address The address the message is being sent to
  • param string $dsn Comma separated list of DSN notifications. NEVER, SUCCESS, FAILURE or DELAY. If you specify NEVER all other notifications are ignored.
  • return bool
public reset()
 

Send an SMTP RSET command.

Abort any transaction that is currently in progress. Implements RFC 821: RSET <CRLF>.

  • return bool True on success
public sendAndMail( $from)
 

Send an SMTP SAML command.

Starts a mail transaction from the email address specified in $from. Returns true if successful or false otherwise. If True the mail transaction is started and then one or more recipient commands may be called followed by a data command. This command will send the message to the users terminal if they are logged in and send them an email. Implements RFC 821: SAML <SP> FROM: <CRLF>.

  • param string $from The address the message is from
  • return bool
public setDebugLevel( $level = 0)
 

Set debug output level.

  • param int $level
public setDebugOutput( $method = 'echo')
 

Set debug output method.

  • param string|callable $method The name of the mechanism to use for debugging output, or a callable to handle it
public setSMTPUTF8( $enabled = false)
 

Enable or disable use of SMTPUTF8.

  • param bool $enabled
public setTimeout( $timeout = 0)
 

Set SMTP timeout.

  • param int $timeout The timeout duration in seconds
public setVerp( $enabled = false)
 

Enable or disable VERP address generation.

  • param bool $enabled
public startTLS()
 

Initiate a TLS (encrypted) session.

  • return bool
public turn()
 

Send an SMTP TURN command.

This is an optional command for SMTP that this class does not support. This method is here to make the RFC821 Definition complete for this class and may be implemented in future. Implements from RFC 821: TURN <CRLF>.

  • return bool
public verify( $name)
 

Send an SMTP VRFY command.

  • param string $name The name to verify
  • return bool
public xclient(array $vars)
 

Send SMTP XCLIENT command to server and check its return code.

  • return bool True on success
Properties
protected $error = ['error' => '', 'detail' => '', 'smtp_code' => '', 'smtp_code_ex' => '']
 

Error information, if any, for the last SMTP command.

  • var array
protected $helo_rply = NULL
 

The reply the server sent to us for HELO.

If null, no HELO string has yet been received.

  • var string|null
protected $last_reply = ''
 

The most recent reply received from the server.

  • var string
protected $last_smtp_transaction_id = NULL
 

The last transaction ID issued in response to a DATA command, if one was detected.

  • var string|bool|null
protected $server_caps = NULL
 

The set of SMTP extensions sent in reply to EHLO command.

Indexes of the array are extension names. Value at index 'HELO' or 'EHLO' (according to command that was sent) represents the server name. In case of HELO it is the only element of the array. Other values can be boolean TRUE or an array containing extension options. If null, no HELO/EHLO string has yet been received.

  • var array|null
protected $smtp_conn = NULL
 

The socket for the server connection.

  • var ?resource
protected $smtp_transaction_id_patterns = ['exim' => '/[\d]{3} OK id=(.*)/', 'sendmail' => '/[\d]{3} 2\.0\.0 (.*) Message/', 'postfix' => '/[\d]{3} 2\.0\.0 Ok: queued as (.*)/', 'Microsoft_ESMTP' => '/[0-9]{3} 2\.[\d]\.0 (.*)@(?:.*) Queued mail for delivery/', 'Amazon_SES' => '/[\d]{3} Ok (.*)/', 'SendGrid' => '/[\d]{3} Ok: queued as (.*)/', 'CampaignMonitor' => '/[\d]{3} 2\.0\.0 OK:([a-zA-Z\d]{48})/', 'Haraka' => '/[\d]{3} Message Queued \((.*)\)/', 'ZoneMTA' => '/[\d]{3} Message queued as (.*)/', 'Mailjet' => '/[\d]{3} OK queued as (.*)/']
 

Patterns to extract an SMTP transaction id from reply to a DATA command.

The first capture group in each regex will be used as the ID. MS ESMTP returns the message ID, which may not be correct for internal tracking.

  • var string[]
Methods
protected edebug( $str, $level = 0)
 

Output debugging info via a user-selected method.

  • param string $str Debug string to output
  • param int $level The debug level of this message; see DEBUG_* constants
  • see \SMTP::$Debugoutput
  • see \SMTP::$do_debug
protected errorHandler( $errno, $errmsg, $errfile = '', $errline = 0)
 

Reports an error number and string.

  • param int $errno The error number returned by PHP
  • param string $errmsg The error message returned by PHP
  • param string $errfile The file the error occurred in
  • param int $errline The line number the error occurred on
protected get_lines()
 

Read the SMTP server's response.

Either before eof or socket timeout occurs on the operation. With SMTP we can tell if we have more lines to read if the 4th character is '-' symbol. If it is a space then we don't need to read anything else.

  • return string
protected getSMTPConnection( $host, $port = NULL, $timeout = 30, $options = [])
 

Create connection to the SMTP server.

  • param string $host SMTP server IP or host name
  • param int $port The port number to connect to
  • param int $timeout How long to wait for the connection to open
  • param array $options An array of options for stream_context_create()
  • return false|resource
protected hmac( $data, $key)
 

Calculate an MD5 HMAC hash.

Works like hash_hmac('md5', $data, $key) in case that function is not available.

  • param string $data The data to hash
  • param string $key The key to hash with
  • return string
protected parseHelloFields( $type)
 

Parse a reply to HELO/EHLO command to discover server extensions.

In case of HELO, the only parameter that can be discovered is a server name.

  • param string $type HELO or EHLO
protected recordLastTransactionID()
 

Extract and return the ID of the last SMTP transaction based on a list of patterns provided in SMTP::$smtp_transaction_id_patterns.

Relies on the host providing the ID in response to a DATA command. If no reply has been received yet, it will return null. If no pattern was matched, it will return false.

  • return bool|string|null
protected sendCommand( $command, $commandstring, $expect)
 

Send a command to an SMTP server and check its return code.

  • param string $command The command name - not sent to the server
  • param string $commandstring The actual command to send
  • param int|array $expect One or more expected integer success codes
  • return bool True on success
protected sendHello( $hello, $host)
 

Send an SMTP HELO or EHLO command.

Low-level implementation used by hello().

  • param string $hello The HELO string
  • param string $host The hostname to say we are
  • return bool
  • see \hello()
protected setError( $message, $detail = '', $smtp_code = '', $smtp_code_ex = '')
 

Set error messages and codes.

  • param string $message The error message
  • param string $detail Further detail on the error
  • param string $smtp_code An associated SMTP error code
  • param string $smtp_code_ex Extended SMTP code
Properties
public static $xclient_allowed_attributes = ['NAME', 'ADDR', 'PORT', 'PROTO', 'HELO', 'LOGIN', 'DESTADDR', 'DESTPORT']
 

Allowed SMTP XCLIENT attributes.

Must be allowed by the SMTP server. EHLO response is not checked.

  • see https://www.postfix.org/XCLIENT_README.html
  • var array
© 2025 Bruce Wells
Search Namespaces \ Classes
Configuration