PHP Class ezcMailSmtpTransport

The implementation supports most of the commands specified in: - {@link http://www.faqs.org/rfcs/rfc821.html RFC821 - SMTP} - {@link http://www.faqs.org/rfcs/rfc2554.html RFC2554 - SMTP Authentication} - {@link http://www.faqs.org/rfcs/rfc2831.html RFC2831 - DIGEST-MD5 Authentication} - {@link http://www.faqs.org/rfcs/rfc2195.html RFC2195 - CRAM-MD5 Authentication} - {@link http://davenport.sourceforge.net/ntlm.html NTLM Authentication} By default, the SMTP transport tries to login anonymously to the SMTP server (if an empty username and password have been provided), or to authenticate with the strongest method supported by the server (if username and password have been provided). The default behaviour can be changed with the option preferredAuthMethod (see {@link ezcMailSmtpTransportOptions}). If the preferred method is specified via options, only that authentication method will be attempted on the SMTP server. If it fails, an exception will be thrown. Supported authentication methods (from strongest to weakest): - DIGEST-MD5 - CRAM-MD5 - NTLM (requires the PHP mcrypt extension) - XOAUTH2 - LOGIN - PLAIN Not all SMTP servers support these methods, and some SMTP servers don't support authentication at all. Example send mail: $mail = new ezcMailComposer(); $mail->from = new ezcMailAddress( '[email protected]', 'Adrian Ripburger' ); $mail->addTo( new ezcMailAddress( '[email protected]', 'Maureen Corley' ) ); $mail->subject = "This is the subject of the example mail"; $mail->plainText = "This is the body of the example mail."; $mail->build(); Create a new SMTP transport object with an SSLv3 connection. The port will be 465 by default, use the 4th argument to change it. Username and password (2nd and 3rd arguments) are left blank, which means the mail host does not need authentication. The 5th parameter is the optional $options object. $options = new ezcMailSmtpTransportOptions(); $options->connectionType = ezcMailSmtpTransport::CONNECTION_SSLV3; $transport = new ezcMailSmtpTransport( 'mailhost.example.com', '', '', null, $options ); Use the SMTP transport to send the created mail object $transport->send( $mail ); Example require NTLM authentication: Create an SMTP transport and demand NTLM authentication. Username and password must be specified, otherwise no authentication will be attempted. If NTLM authentication fails, an exception will be thrown. $options = new ezcMailSmtpTransportOptions(); $options->preferredAuthMethod = ezcMailSmtpTransport::AUTH_NTLM; $transport = new ezcMailSmtpTransport( 'mailhost.example.com', 'username', 'password', null, $options ); The option can also be specified via the option property: $transport->options->preferredAuthMethod = ezcMailSmtpTransport::AUTH_NTLM; See {@link ezcMailSmtpTransportOptions} for options you can specify for SMTP.
Inheritance: implements ezcMailTransport
ファイルを表示 Open project: zetacomponents/mail Class Usage Examples

Protected Properties

Property Type Description
$connection resource The connection to the SMTP server.
$doAuthenticate boolean This variable is set to true if a username is provided for login.
$keepConnection boolean Holds if the connection should be kept open after sending a mail.
$options ezcMailSmtpTransportOptions Holds the options of this class.
$properties array(string=>mixed) Holds the properties of this class.
$status $var int {@link STATUS_NOT_CONNECTED}, {@link STATUS_CONNECTED} or {@link STATUS_AUTHENTICATED}.

Public Methods

Method Description
__construct ( string $host, string $user = '', string $password = '', integer $port = null, ezcMailSmtpTransportOptions | array(string=>mixed) $options = [] ) Constructs a new ezcMailSmtpTransport.
__destruct ( ) Destructs this object.
__get ( string $name ) : mixed Returns the value of the property $name.
__isset ( string $name ) : boolean Returns true if the property $name is set, otherwise false.
__set ( string $name, mixed $value ) Sets the property $name to $value.
disconnect ( ) Sends the QUIT command to the server and breaks the connection.
getSupportedAuthMethods ( ) : array(string) Returns an array with the authentication methods supported by the SMTP transport class (not by the SMTP server!).
keepConnection ( ) Sets if the connection should be kept open after sending an email.
send ( ezcMail $mail ) Sends the ezcMail $mail using the SMTP protocol.

Protected Methods

Method Description
auth ( $method ) : boolean Calls the appropiate authentication method based on $method.
authCramMd5 ( ) : boolean Tries to login to the SMTP server with 'AUTH CRAM-MD5' and returns true if successful.
authDigestMd5 ( ) : boolean Tries to login to the SMTP server with 'AUTH DIGEST-MD5' and returns true if successful.
authLogin ( ) : boolean Tries to login to the SMTP server with 'AUTH LOGIN' and returns true if successful.
authNtlm ( ) : boolean Tries to login to the SMTP server with 'AUTH NTLM' and returns true if successful.
authNtlmMessageType1 ( string $workstation, string $domain ) : string Generates an NTLM type 1 message.
authNtlmMessageType3 ( string $challenge, string $user, string $password, string $workstation, string $domain ) : string Generates an NTLM type 3 message from the $challenge sent by the SMTP server in an NTLM type 2 message.
authNtlmResponse ( string $challenge, string $password ) : string Calculates an NTLM response to be used in the creation of the NTLM type 3 message.
authNtlmSecurityBuffer ( string $text, integer $offset ) : string Creates an NTLM security buffer information string.
authPlain ( ) : boolean Tries to login to the SMTP server with 'AUTH PLAIN' and returns true if successful.
authXOAuth2 ( ) : boolean Tries to login to the SMTP server with 'AUTH XOAUTH2' and returns true if successful.
cmdData ( ) Sends the DATA command to the SMTP server.
cmdMail ( string $from ) Sends the MAIL FROM command, with the sender's mail address $from.
cmdRcpt ( string $email ) Sends the 'RCTP TO' to the server with the address $email.
composeSmtpMailAddress ( string $email ) Returns the $email enclosed within '< >'.
connect ( ) Creates a connection to the SMTP server and initiates the login procedure.
generateNonce ( integer $length = 32 ) : string Generates an alpha-numeric random string with the specified $length.
getData ( ) : string Returns data received from the connection stream.
getReplyCode ( &$line ) : string Returns the reply code of the last message from the server.
login ( ) Performs the initial handshake with the SMTP server and authenticates the user, if login data is provided to the constructor.
sendData ( string $data ) Sends $data to the SMTP server through the connection.
sortAuthMethods ( array $methods ) : array(string) Sorts the specified array of AUTH methods $methods by strength, so higher strength methods will be used first.
startTls ( ) : void Enables TLS on an unencrypted SMTP connection

Method Details

__construct() public method

The constructor expects, at least, the hostname $host of the SMTP server. The username $user will be used for authentication if provided. If it is left blank no authentication will be performed. The password $password will be used for authentication if provided. Use this parameter always in combination with the $user parameter. The value $port specifies on which port to connect to $host. By default it is 25 for plain connections and 465 for TLS/SSL/SSLv2/SSLv3. Note: The ssl option from {@link ezcMailTransportOptions} doesn't apply to SMTP. If you want to connect to SMTP using TLS/SSL/SSLv2/SSLv3 use the connectionType option in {@link ezcMailSmtpTransportOptions}. For options you can specify for SMTP see {@link ezcMailSmtpTransportOptions}.
public __construct ( string $host, string $user = '', string $password = '', integer $port = null, ezcMailSmtpTransportOptions | array(string=>mixed) $options = [] )
$host string
$user string
$password string
$port integer
$options ezcMailSmtpTransportOptions | array(string=>mixed)

__destruct() public method

Closes the connection if it is still open.
public __destruct ( )

__get() public method

Returns the value of the property $name.
public __get ( string $name ) : mixed
$name string
return mixed

__isset() public method

Returns true if the property $name is set, otherwise false.
public __isset ( string $name ) : boolean
$name string
return boolean

__set() public method

Sets the property $name to $value.
public __set ( string $name, mixed $value )
$name string
$value mixed

auth() protected method

Calls the appropiate authentication method based on $method.
protected auth ( $method ) : boolean
return boolean

authCramMd5() protected method

Tries to login to the SMTP server with 'AUTH CRAM-MD5' and returns true if successful.
protected authCramMd5 ( ) : boolean
return boolean

authDigestMd5() protected method

Tries to login to the SMTP server with 'AUTH DIGEST-MD5' and returns true if successful.
protected authDigestMd5 ( ) : boolean
return boolean

authLogin() protected method

Tries to login to the SMTP server with 'AUTH LOGIN' and returns true if successful.
protected authLogin ( ) : boolean
return boolean

authNtlm() protected method

Tries to login to the SMTP server with 'AUTH NTLM' and returns true if successful.
protected authNtlm ( ) : boolean
return boolean

authNtlmMessageType1() protected method

Generates an NTLM type 1 message.
protected authNtlmMessageType1 ( string $workstation, string $domain ) : string
$workstation string
$domain string
return string

authNtlmMessageType3() protected method

Generates an NTLM type 3 message from the $challenge sent by the SMTP server in an NTLM type 2 message.
protected authNtlmMessageType3 ( string $challenge, string $user, string $password, string $workstation, string $domain ) : string
$challenge string
$user string
$password string
$workstation string
$domain string
return string

authNtlmResponse() protected method

Calculates an NTLM response to be used in the creation of the NTLM type 3 message.
protected authNtlmResponse ( string $challenge, string $password ) : string
$challenge string
$password string
return string

authNtlmSecurityBuffer() protected method

The structure of the security buffer is: - a short containing the length of the buffer content in bytes (may be zero). - a short containing the allocated space for the buffer in bytes (greater than or equal to the length; typically the same as the length). - a long containing the offset to the start of the buffer in bytes (from the beginning of the NTLM message). Example: - buffer content length: 1234 bytes (0xd204 in hexa) - allocated space: 1234 bytes( 0xd204 in hexa) - offset: 4321 bytes (0xe1100000 in hexa) then the security buffer would be 0xd204d204e1100000 (in hexa).
protected authNtlmSecurityBuffer ( string $text, integer $offset ) : string
$text string
$offset integer
return string

authPlain() protected method

Tries to login to the SMTP server with 'AUTH PLAIN' and returns true if successful.
protected authPlain ( ) : boolean
return boolean

authXOAuth2() protected method

Tries to login to the SMTP server with 'AUTH XOAUTH2' and returns true if successful.
protected authXOAuth2 ( ) : boolean
return boolean

cmdData() protected method

Sends the DATA command to the SMTP server.
protected cmdData ( )

cmdMail() protected method

This method must be called once to tell the server the sender address. The sender's mail address $from may be enclosed in angle brackets.
protected cmdMail ( string $from )
$from string

cmdRcpt() protected method

This method must be called once for each recipient of the mail including cc and bcc recipients. The RCPT TO commands control where the mail is actually sent. It does not affect the headers of the email. The recipient mail address $email may be enclosed in angle brackets.
protected cmdRcpt ( string $email )
$email string

composeSmtpMailAddress() protected method

If $email is already enclosed within '< >' it is returned unmodified.
protected composeSmtpMailAddress ( string $email )
$email string $return string

connect() protected method

Creates a connection to the SMTP server and initiates the login procedure.
protected connect ( )

disconnect() public method

Sends the QUIT command to the server and breaks the connection.
public disconnect ( )

generateNonce() protected method

Used in the DIGEST-MD5 authentication method.
protected generateNonce ( integer $length = 32 ) : string
$length integer
return string

getData() protected method

Returns data received from the connection stream.
protected getData ( ) : string
return string

getReplyCode() protected method

$line contains the complete data retrieved from the stream. This can be used to retrieve the error message in case of an error.
protected getReplyCode ( &$line ) : string
return string

getSupportedAuthMethods() public static method

The returned array has the methods sorted by their relative strengths, so stronger methods are first in the array.
public static getSupportedAuthMethods ( ) : array(string)
return array(string)

keepConnection() public method

This method should be called prior to the first call to send(). Keeping the connection open is useful if you are sending a lot of mail. It removes the overhead of opening the connection after each mail is sent. Use disconnect() to close the connection if you have requested to keep it open.
public keepConnection ( )

login() protected method

Performs the initial handshake with the SMTP server and authenticates the user, if login data is provided to the constructor.
protected login ( )

send() public method

If you want to send several emails use keepConnection() to leave the connection to the server open between each mail.
public send ( ezcMail $mail )
$mail ezcMail

sendData() protected method

This method appends one line-break at the end of $data.
protected sendData ( string $data )
$data string

sortAuthMethods() protected method

For example, if the server supports: $methods = array( 'PLAIN', 'LOGIN', 'CRAM-MD5' ); then this method will return: $methods = array( 'CRAM-MD5', 'LOGIN', 'PLAIN' );
protected sortAuthMethods ( array $methods ) : array(string)
$methods array
return array(string)

startTls() protected method

Enables TLS on an unencrypted SMTP connection
protected startTls ( ) : void
return void

Property Details

$connection protected_oe property

The connection to the SMTP server.
protected resource $connection
return resource

$doAuthenticate protected_oe property

This variable is set to true if a username is provided for login.
protected bool $doAuthenticate
return boolean

$keepConnection protected_oe property

Holds if the connection should be kept open after sending a mail.
protected bool $keepConnection
return boolean

$options protected_oe property

Holds the options of this class.
protected ezcMailSmtpTransportOptions $options
return ezcMailSmtpTransportOptions

$properties protected_oe property

Holds the properties of this class.
protected array(string=>mixed) $properties
return array(string=>mixed)

$status protected_oe property

$var int {@link STATUS_NOT_CONNECTED}, {@link STATUS_CONNECTED} or {@link STATUS_AUTHENTICATED}.
protected $status