Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402] [Versions 402 and 403]

PHPMailer RFC821 SMTP email transport class. PHP Version 5.5.

Author: Marcus Bointon (Synchro/coolbru) <phpmailer@synchromedia.co.uk>
Author: Jim Jagielski (jimjag) <jimjag@gmail.com>
Author: Andy Prevost (codeworxtech) <codeworxtech@users.sourceforge.net>
Author: Brent R. Matzelle (original founder)
Copyright: 2012 - 2020 Marcus Bointon
Copyright: 2010 - 2012 Jim Jagielski
Copyright: 2004 - 2009 Andy Prevost
License: http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
File Size: 1458 lines (47 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class

SMTP:: (39 methods):
  edebug()
  connect()
  getSMTPConnection()
  startTLS()
  authenticate()
  hmac()
  connected()
  close()
  data()
  hello()
  sendHello()
  parseHelloFields()
  mail()
  quit()
  recipient()
  reset()
  sendCommand()
  sendAndMail()
  verify()
  noop()
  turn()
  client_send()
  getError()
  getServerExtList()
  getServerExt()
  getLastReply()
  get_lines()
  setVerp()
  getVerp()
  setError()
  setDebugOutput()
  getDebugOutput()
  setDebugLevel()
  getDebugLevel()
  setTimeout()
  getTimeout()
  errorHandler()
  recordLastTransactionID()
  getLastTransactionID()


Class: SMTP  - X-Ref

PHPMailer RFC821 SMTP email transport class.
Implements RFC 821 SMTP commands and provides some utility methods for sending mail to an SMTP server.

edebug($str, $level = 0)   X-Ref
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

connect($host, $port = null, $timeout = 30, $options = [])   X-Ref
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

getSMTPConnection($host, $port = null, $timeout = 30, $options = [])   X-Ref
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

startTLS()   X-Ref
Initiate a TLS (encrypted) session.

return: bool

authenticate($username,$password,$authtype = null,$OAuth = null)   X-Ref
Perform SMTP authentication.
Must be run after 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

hmac($data, $key)   X-Ref
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

connected()   X-Ref
Check connection state.

return: bool True if connected

close()   X-Ref
Close the socket and clean up the state of the class.
Don't use this function without first trying to use QUIT.


data($msg_data)   X-Ref
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>.

param: string $msg_data Message data to send
return: bool

hello($host = '')   X-Ref
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> <domain> <CRLF>
and RFC 2821 EHLO.

param: string $host The host name or IP to connect to
return: bool

sendHello($hello, $host)   X-Ref
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

parseHelloFields($type)   X-Ref
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`

mail($from)   X-Ref
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:<reverse-path> <CRLF>.

param: string $from Source address of this message
return: bool

quit($close_on_error = true)   X-Ref
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

recipient($address, $dsn = '')   X-Ref
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:<forward-path> <CRLF>.

param: string $address The address the message is being sent to
param: string $dsn     Comma separated list of DSN notifications. NEVER, SUCCESS, FAILURE
return: bool

reset()   X-Ref
Send an SMTP RSET command.
Abort any transaction that is currently in progress.
Implements RFC 821: RSET <CRLF>.

return: bool True on success

sendCommand($command, $commandstring, $expect)   X-Ref
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

sendAndMail($from)   X-Ref
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:<reverse-path> <CRLF>.

param: string $from The address the message is from
return: bool

verify($name)   X-Ref
Send an SMTP VRFY command.

param: string $name The name to verify
return: bool

noop()   X-Ref
Send an SMTP NOOP command.
Used to keep keep-alives alive, doesn't actually do anything.

return: bool

turn()   X-Ref
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

client_send($data, $command = '')   X-Ref
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

getError()   X-Ref
Get the latest error.

return: array

getServerExtList()   X-Ref
Get SMTP extensions available on the server.

return: array|null

getServerExt($name)   X-Ref
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

getLastReply()   X-Ref
Get the last reply from the server.

return: string

get_lines()   X-Ref
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

setVerp($enabled = false)   X-Ref
Enable or disable VERP address generation.

param: bool $enabled

getVerp()   X-Ref
Get VERP address generation mode.

return: bool

setError($message, $detail = '', $smtp_code = '', $smtp_code_ex = '')   X-Ref
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

setDebugOutput($method = 'echo')   X-Ref
Set debug output method.

param: string|callable $method The name of the mechanism to use for debugging output, or a callable to handle it

getDebugOutput()   X-Ref
Get debug output method.

return: string

setDebugLevel($level = 0)   X-Ref
Set debug output level.

param: int $level

getDebugLevel()   X-Ref
Get debug output level.

return: int

setTimeout($timeout = 0)   X-Ref
Set SMTP timeout.

param: int $timeout The timeout duration in seconds

getTimeout()   X-Ref
Get SMTP timeout.

return: int

errorHandler($errno, $errmsg, $errfile = '', $errline = 0)   X-Ref
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

recordLastTransactionID()   X-Ref
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

getLastTransactionID()   X-Ref
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