As this tutorial has no doubt shown, PHP is a powerful programming language with many powerful functions. What has still to be covered in this tutorial emphasises this further.
PHP includes a set of functions that permit sending email messages using the web server that the PHP interpreter is run from.
Sending an email using PHP
PHP provides a very simple function called mail
. This function takes three parameters but can
also take additional parameters to extend its functionality.
The function takes three essential parameters which themselves become hearders for the email:
<?php mail($to, $subject, $message); ?>
All of these parameters are self-explanatory.
Additional mail headers
The mail
function also provides support for an additional parameter for additional headers:
<?php mail($to, $subject, $message, $headers); ?>
The $headers
variable is a string variable that specifies more headers for the email. The following table
shows some of these headers and their purpose:
Header | Purpose |
---|---|
From | The email address that the email came from |
Content-Type | What type of content the email contains. For instance text/html; charset=utf-8 |
X-Priority | The priority level of the email. This specifies how high the email should be in terms of priority in the recipient's email inbox. Use with caution. |
Reply-To | The address that should receive any replies. |
Cc | Carbon-copy (CC). Send a 'carbon-copy' of the email to a recipient. |
Bcc | Blind-carbon-copy (BCC). Send a 'carbon-copy' of the email blindly to a recipient. |
Mime-Version | Specifies the Multipurpose Internet Mail Extension (MIME) version used with the email. |
Headers should be separated with the carriage return character (in UNIX it is \r
).
PHP can send HTML-based emails rather than the standard text based emails using the Content-Type
header:
<?php $content = "<strong>Hello world</strong>"; $headers = "MIME-Version: 1.0\rContent-Type: text/html; charset=utf-8\r" mail($to, $subject, $message, $headers); ?>