Constants

VERSION

VERSION = '6.6.4' : string

The PHPMailer SMTP version number.

LE

LE = "\r\n" : string

SMTP line break constant.

DEFAULT_PORT

DEFAULT_PORT = 25 : int

The SMTP port to use if one is not specified.

MAX_LINE_LENGTH

MAX_LINE_LENGTH = 998 : int

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

MAX_REPLY_LENGTH

MAX_REPLY_LENGTH = 512 : int

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

DEBUG_OFF

DEBUG_OFF = 0 : int

Debug level for no output.

DEBUG_CLIENT

DEBUG_CLIENT = 1 : int

Debug level to show client -> server messages.

DEBUG_SERVER

DEBUG_SERVER = 2 : int

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

DEBUG_CONNECTION

DEBUG_CONNECTION = 3 : int

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

DEBUG_LOWLEVEL

DEBUG_LOWLEVEL = 4 : int

Debug level to show all messages.

Properties

$do_debug

$do_debug : int

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.

Type

int

$Debugoutput

$Debugoutput : string|callable|\Psr\Log\LoggerInterface

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;

Type

LoggerInterface

$do_verp

$do_verp : bool

Whether to use VERP.

Type

bool

$Timeout

$Timeout : int

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.

Type

int

$Timelimit

$Timelimit : int

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

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

Type

int

$smtp_transaction_id_patterns

$smtp_transaction_id_patterns : string[]

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.

Type

string[]

$last_smtp_transaction_id

$last_smtp_transaction_id : string|bool|null

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

Type

string|bool|null

$smtp_conn

$smtp_conn : ?resource

The socket for the server connection.

Type

?resource

$error

$error : array

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

Type

array

$helo_rply

$helo_rply : string|null

The reply the server sent to us for HELO.

If null, no HELO string has yet been received.

Type

string|null

$server_caps

$server_caps : array|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.

Type

array|null

$last_reply

$last_reply : string

The most recent reply received from the server.

Type

string

Methods

connect()

connect(string  $host, int  $port = null, int  $timeout = 30, array  $options = []) : bool

Connect to an SMTP server.

Parameters

string $host

SMTP server IP or host name

int $port

The port number to connect to

int $timeout

How long to wait for the connection to open

array $options

An array of options for stream_context_create()

Returns

bool —

startTLS()

startTLS() : bool

Initiate a TLS (encrypted) session.

Returns

bool —

authenticate()

authenticate(string  $username, string  $password, string  $authtype = null, \PHPMailer\PHPMailer\OAuthTokenProvider  $OAuth = null) : bool

Perform SMTP authentication.

Must be run after hello().

Parameters

string $username

The user name

string $password

The password

string $authtype

The auth type (CRAM-MD5, PLAIN, LOGIN, XOAUTH2)

\PHPMailer\PHPMailer\OAuthTokenProvider $OAuth

An optional OAuthTokenProvider instance for XOAUTH2 authentication

Returns

bool —

True if successfully authenticated

connected()

connected() : bool

Check connection state.

Returns

bool —

True if connected

close()

close() : mixed

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

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

Returns

mixed —

data()

data(string  $msg_data) : bool

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 send 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>.

Parameters

string $msg_data

Message data to send

Returns

bool —

hello()

hello(string  $host = '') : bool

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.

Parameters

string $host

The host name or IP to connect to

Returns

bool —

mail()

mail(string  $from) : bool

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>.

Parameters

string $from

Source address of this message

Returns

bool —

quit()

quit(bool  $close_on_error = true) : bool

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>.

Parameters

bool $close_on_error

Should the connection close if an error occurs?

Returns

bool —

recipient()

recipient(string  $address, string  $dsn = '') : bool

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>.

Parameters

string $address

The address the message is being sent to

string $dsn

Comma separated list of DSN notifications. NEVER, SUCCESS, FAILURE or DELAY. If you specify NEVER all other notifications are ignored.

Returns

bool —

reset()

reset() : bool

Send an SMTP RSET command.

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

Returns

bool —

True on success

sendAndMail()

sendAndMail(string  $from) : bool

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>.

Parameters

string $from

The address the message is from

Returns

bool —

verify()

verify(string  $name) : bool

Send an SMTP VRFY command.

Parameters

string $name

The name to verify

Returns

bool —

noop()

noop() : bool

Send an SMTP NOOP command.

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

Returns

bool —

turn()

turn() : bool

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>.

Returns

bool —

client_send()

client_send(string  $data, string  $command = '') : int|bool

Send raw data to the server.

Parameters

string $data

The data to send

string $command

Optionally, the command this is part of, used only for controlling debug output

Returns

int|bool —

The number of bytes sent to the server or false on error

getError()

getError() : array

Get the latest error.

Returns

array —

getServerExtList()

getServerExtList() : array|null

Get SMTP extensions available on the server.

Returns

array|null —

getServerExt()

getServerExt(string  $name) : string|bool|null

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.

Parameters

string $name

Name of SMTP extension or 'HELO'|'EHLO'

Returns

string|bool|null —

getLastReply()

getLastReply() : string

Get the last reply from the server.

Returns

string —

setVerp()

setVerp(bool  $enabled = false) : mixed

Enable or disable VERP address generation.

Parameters

bool $enabled

Returns

mixed —

getVerp()

getVerp() : bool

Get VERP address generation mode.

Returns

bool —

setDebugOutput()

setDebugOutput(string|callable  $method = 'echo') : mixed

Set debug output method.

Parameters

string|callable $method

The name of the mechanism to use for debugging output, or a callable to handle it

Returns

mixed —

getDebugOutput()

getDebugOutput() : string

Get debug output method.

Returns

string —

setDebugLevel()

setDebugLevel(int  $level) : mixed

Set debug output level.

Parameters

int $level

Returns

mixed —

getDebugLevel()

getDebugLevel() : int

Get debug output level.

Returns

int —

setTimeout()

setTimeout(int  $timeout) : mixed

Set SMTP timeout.

Parameters

int $timeout

The timeout duration in seconds

Returns

mixed —

getTimeout()

getTimeout() : int

Get SMTP timeout.

Returns

int —

getLastTransactionID()

getLastTransactionID() : bool|string|null

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.

Returns

bool|string|null —

edebug()

edebug(string  $str, int  $level) : mixed

Output debugging info via a user-selected method.

Parameters

string $str

Debug string to output

int $level

The debug level of this message; see DEBUG_* constants

Returns

mixed —

getSMTPConnection()

getSMTPConnection(string  $host, int  $port = null, int  $timeout = 30, array  $options = []) : false|resource

Create connection to the SMTP server.

Parameters

string $host

SMTP server IP or host name

int $port

The port number to connect to

int $timeout

How long to wait for the connection to open

array $options

An array of options for stream_context_create()

Returns

false|resource —

hmac()

hmac(string  $data, string  $key) : string

Calculate an MD5 HMAC hash.

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

Parameters

string $data

The data to hash

string $key

The key to hash with

Returns

string —

sendHello()

sendHello(string  $hello, string  $host) : bool

Send an SMTP HELO or EHLO command.

Low-level implementation used by hello().

Parameters

string $hello

The HELO string

string $host

The hostname to say we are

Returns

bool —

parseHelloFields()

parseHelloFields(string  $type) : mixed

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.

Parameters

string $type

HELO or EHLO

Returns

mixed —

sendCommand()

sendCommand(string  $command, string  $commandstring, int|array  $expect) : bool

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

Parameters

string $command

The command name - not sent to the server

string $commandstring

The actual command to send

int|array $expect

One or more expected integer success codes

Returns

bool —

True on success

get_lines()

get_lines() : string

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.

Returns

string —

setError()

setError(string  $message, string  $detail = '', string  $smtp_code = '', string  $smtp_code_ex = '') : mixed

Set error messages and codes.

Parameters

string $message

The error message

string $detail

Further detail on the error

string $smtp_code

An associated SMTP error code

string $smtp_code_ex

Extended SMTP code

Returns

mixed —

errorHandler()

errorHandler(int  $errno, string  $errmsg, string  $errfile = '', int  $errline) : mixed

Reports an error number and string.

Parameters

int $errno

The error number returned by PHP

string $errmsg

The error message returned by PHP

string $errfile

The file the error occurred in

int $errline

The line number the error occurred on

Returns

mixed —

recordLastTransactionID()

recordLastTransactionID() : bool|string|null

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.

Returns

bool|string|null —