PHP Class PHPMailer\PHPMailer\SMTP

Implements RFC 821 SMTP commands and provides some utility methods for sending mail to an SMTP server.
Author: Chris Ryan
Author: Marcus Bointon ([email protected])
Show file Open project: nukeviet/nukeviet

Public Properties

Property Type Description
$Debugoutput string | callable Options: * echo Output plain-text as-is, appropriate for CLI * html Output escaped, line breaks converted to
, 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";};
$Timelimit integer Default of 5 minutes (300sec) is from RFC2821 section 4.5.3.2
$Timeout integer 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.
$do_debug integer 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
$do_verp boolean Whether to use VERP.

Protected Properties

Property Type Description
$error array Error information, if any, for the last SMTP command.
$helo_rply string | null If null, no HELO string has yet been received.
$last_reply string The most recent reply received from the server.
$server_caps array | null 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.
$smtp_conn resource The socket for the server connection.

Public Methods

Method Description
authenticate ( string $username, string $password, string $authtype = null, OAuth $OAuth = null ) : boolean Perform SMTP authentication.
client_send ( string $data ) : integer | boolean Send raw data to the server.
close ( ) : void Close the socket and clean up the state of the class.
connect ( string $host, integer $port = null, integer $timeout = 30, array $options = [] ) : boolean Connect to an SMTP server.
connected ( ) : boolean Check connection state.
data ( string $msg_data ) : boolean Send an SMTP DATA command.
getDebugLevel ( ) : integer Get debug output level.
getDebugOutput ( ) : string Get debug output method.
getError ( ) : array Get the latest error.
getLastReply ( ) : string Get the last reply from the server.
getServerExt ( string $name ) : mixed A multipurpose method The method works in three ways, dependent on argument value and current state 1. HELO/EHLO was not sent - returns null and set up $this->error 2. HELO was sent $name = 'HELO': returns server name $name = 'EHLO': returns boolean false $name = any string: returns null and set up $this->error 3. EHLO was sent $name = 'HELO'|'EHLO': returns server name $name = any string: if extension $name exists, returns boolean True or its options. Otherwise returns boolean False In other words, one can use this method to detect 3 conditions: - null returned: handshake was not or we don't know about ext (refer to $this->error) - false returned: the requested feature exactly not exists - positive value returned: the requested feature exists
getServerExtList ( ) : array | null Get SMTP extensions available on the server
getTimeout ( ) : integer Get SMTP timeout.
getVerp ( ) : boolean Get VERP address generation mode.
hello ( string $host = '' ) : boolean Send an SMTP HELO or EHLO command.
mail ( string $from ) : boolean Send an SMTP MAIL command.
noop ( ) : boolean Send an SMTP NOOP command.
quit ( boolean $close_on_error = true ) : boolean Send an SMTP QUIT command.
recipient ( string $address ) : boolean Send an SMTP RCPT command.
reset ( ) : boolean Send an SMTP RSET command.
sendAndMail ( string $from ) : boolean Send an SMTP SAML command.
setDebugLevel ( integer $level ) Set debug output level.
setDebugOutput ( string | callable $method = 'echo' ) Set debug output method.
setTimeout ( integer $timeout ) Set SMTP timeout.
setVerp ( boolean $enabled = false ) Enable or disable VERP address generation.
startTLS ( ) : boolean Initiate a TLS (encrypted) session.
turn ( ) : boolean Send an SMTP TURN command.
verify ( string $name ) : boolean Send an SMTP VRFY command.

Protected Methods

Method Description
edebug ( string $str, integer $level ) : void Output debugging info via a user-selected method.
get_lines ( ) : string Read the SMTP server's response.
hmac ( string $data, string $key ) : string Calculate an MD5 HMAC hash.
parseHelloFields ( string $type ) Parse a reply to HELO/EHLO command to discover server extensions.
sendCommand ( string $command, string $commandstring, integer | array $expect ) : boolean Send a command to an SMTP server and check its return code.
sendHello ( string $hello, string $host ) : boolean Send an SMTP HELO or EHLO command.
setError ( string $message, string $detail = '', string $smtp_code = '', string $smtp_code_ex = '' ) Set error messages and codes.

Method Details

authenticate() public method

Must be run after hello().
See also: hello()
public authenticate ( string $username, string $password, string $authtype = null, OAuth $OAuth = null ) : boolean
$username string The user name
$password string The password
$authtype string The auth type (PLAIN, LOGIN, NTLM, CRAM-MD5, XOAUTH2)
$OAuth OAuth An optional OAuth instance for XOAUTH2 authentication
return boolean True if successfully authenticated.

client_send() public method

Send raw data to the server.
public client_send ( string $data ) : integer | boolean
$data string The data to send
return integer | boolean The number of bytes sent to the server or false on error

close() public method

Don't use this function without first trying to use QUIT.
See also: quit()
public close ( ) : void
return void

connect() public method

Connect to an SMTP server.
public connect ( string $host, integer $port = null, integer $timeout = 30, array $options = [] ) : boolean
$host string SMTP server IP or host name
$port integer The port number to connect to
$timeout integer How long to wait for the connection to open
$options array An array of options for stream_context_create()
return boolean

connected() public method

Check connection state.
public connected ( ) : boolean
return boolean True if connected.

data() public method

Issues a data command and sends the msg_data to the server, finializing 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 with the message headers and the message body being separated by and additional . Implements rfc 821: DATA
public data ( string $msg_data ) : boolean
$msg_data string Message data to send
return boolean

edebug() protected method

Output debugging info via a user-selected method.
See also: SMTP::$Debugoutput
See also: SMTP::$do_debug
protected edebug ( string $str, integer $level ) : void
$str string Debug string to output
$level integer The debug level of this message; see DEBUG_* constants
return void

getDebugLevel() public method

Get debug output level.
public getDebugLevel ( ) : integer
return integer

getDebugOutput() public method

Get debug output method.
public getDebugOutput ( ) : string
return string

getError() public method

Get the latest error.
public getError ( ) : array
return array

getLastReply() public method

Get the last reply from the server.
public getLastReply ( ) : string
return string

getServerExt() public method

A multipurpose method The method works in three ways, dependent on argument value and current state 1. HELO/EHLO was not sent - returns null and set up $this->error 2. HELO was sent $name = 'HELO': returns server name $name = 'EHLO': returns boolean false $name = any string: returns null and set up $this->error 3. EHLO was sent $name = 'HELO'|'EHLO': returns server name $name = any string: if extension $name exists, returns boolean True or its options. Otherwise returns boolean False In other words, one can use this method to detect 3 conditions: - null returned: handshake was not or we don't know about ext (refer to $this->error) - false returned: the requested feature exactly not exists - positive value returned: the requested feature exists
public getServerExt ( string $name ) : mixed
$name string Name of SMTP extension or 'HELO'|'EHLO'
return mixed

getServerExtList() public method

Get SMTP extensions available on the server
public getServerExtList ( ) : array | null
return array | null

getTimeout() public method

Get SMTP timeout.
public getTimeout ( ) : integer
return integer

getVerp() public method

Get VERP address generation mode.
public getVerp ( ) : boolean
return boolean

get_lines() protected method

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.
protected get_lines ( ) : string
return string

hello() public method

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 and RFC 2821 EHLO.
public hello ( string $host = '' ) : boolean
$host string The host name or IP to connect to
return boolean

hmac() protected method

Works like hash_hmac('md5', $data, $key) in case that function is not available
protected hmac ( string $data, string $key ) : string
$data string The data to hash
$key string The key to hash with
return string

mail() public method

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 FROM:
public mail ( string $from ) : boolean
$from string Source address of this message
return boolean

noop() public method

Used to keep keep-alives alive, doesn't actually do anything
public noop ( ) : boolean
return boolean

parseHelloFields() protected method

In case of HELO, the only parameter that can be discovered is a server name.
protected parseHelloFields ( string $type )
$type string - 'HELO' or 'EHLO'

quit() public method

Closes the socket if there is no error or the $close_on_error argument is true. Implements from rfc 821: QUIT
public quit ( boolean $close_on_error = true ) : boolean
$close_on_error boolean Should the connection close if an error occurs?
return boolean

recipient() public method

Sets the TO argument to $toaddr. Returns true if the recipient was accepted false if it was rejected. Implements from rfc 821: RCPT TO:
public recipient ( string $address ) : boolean
$address string The address the message is being sent to
return boolean

reset() public method

Abort any transaction that is currently in progress. Implements rfc 821: RSET
public reset ( ) : boolean
return boolean True on success.

sendAndMail() public method

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 FROM:
public sendAndMail ( string $from ) : boolean
$from string The address the message is from
return boolean

sendCommand() protected method

Send a command to an SMTP server and check its return code.
protected sendCommand ( string $command, string $commandstring, integer | array $expect ) : boolean
$command string The command name - not sent to the server
$commandstring string The actual command to send
$expect integer | array One or more expected integer success codes
return boolean True on success.

sendHello() protected method

Low-level implementation used by hello()
See also: hello()
protected sendHello ( string $hello, string $host ) : boolean
$hello string The HELO string
$host string The hostname to say we are
return boolean

setDebugLevel() public method

Set debug output level.
public setDebugLevel ( integer $level )
$level integer

setDebugOutput() public method

Set debug output method.
public setDebugOutput ( string | callable $method = 'echo' )
$method string | callable The name of the mechanism to use for debugging output, or a callable to handle it.

setError() protected method

Set error messages and codes.
protected setError ( string $message, string $detail = '', string $smtp_code = '', string $smtp_code_ex = '' )
$message string The error message
$detail string Further detail on the error
$smtp_code string An associated SMTP error code
$smtp_code_ex string Extended SMTP code

setTimeout() public method

Set SMTP timeout.
public setTimeout ( integer $timeout )
$timeout integer

setVerp() public method

Enable or disable VERP address generation.
public setVerp ( boolean $enabled = false )
$enabled boolean

startTLS() public method

Initiate a TLS (encrypted) session.
public startTLS ( ) : boolean
return boolean

turn() public method

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
public turn ( ) : boolean
return boolean

verify() public method

Send an SMTP VRFY command.
public verify ( string $name ) : boolean
$name string The name to verify
return boolean

Property Details

$Debugoutput public property

Options: * echo Output plain-text as-is, appropriate for CLI * html Output escaped, line breaks converted to
, 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";};
public string|callable $Debugoutput
return string | callable

$Timelimit public property

Default of 5 minutes (300sec) is from RFC2821 section 4.5.3.2
public int $Timelimit
return integer

$Timeout public property

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.
public int $Timeout
return integer

$do_debug public property

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
public int $do_debug
return integer

$do_verp public property

Whether to use VERP.
public bool $do_verp
return boolean

$error protected property

Error information, if any, for the last SMTP command.
protected array $error
return array

$helo_rply protected property

If null, no HELO string has yet been received.
protected string|null $helo_rply
return string | null

$last_reply protected property

The most recent reply received from the server.
protected string $last_reply
return string

$server_caps protected property

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.
protected array|null $server_caps
return array | null

$smtp_conn protected property

The socket for the server connection.
protected resource $smtp_conn
return resource