PHP Mail Tutorial (3 ways to send emails)

How to send and receive emails in PHP. Access email in PHP without an SMTP server. PHP SDK for email testing and development.

  • Table of contents

How to send and receive emails in PHP. Tutorial with example code.

Why emails?

In many modern PHP applications you will at some point need to send (or receive) email. This could be for a newsletter, transactional emails, user sign-up and more. There are lots of ways to send emails in PHP. Here are 3 ways with examples:

1) Sending emails with SMTP

If you have access to a MailServer you can configure an SMTP instance. Then you can use the inbuilt PHP mail() function. Note this requires considerable setup on the server side.

// assuming you have configured a mailserver
mail("", "subject line", "body hello")

2) Email APIs

Most people don't want to mess around maintaining a mail server. For them an easier way to use php mail is with an email API. Email APIs let you send and receive emails in PHP code. They let you create inboxes and addresses dyanmically too.

With the PHP MailSlurp library you can send and receive emails in PHP. Here is an example:

require_once(__DIR__ . '/vendor/autoload.php');

// Configure API key authorization: API_KEY
$config = MailSlurp\Configuration::getDefaultConfiguration()->setApiKey('x-api-key', 'YOUR_API_KEY');
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
// $config = MailSlurp\Configuration::getDefaultConfiguration()->setApiKeyPrefix('x-api-key', 'Bearer');

$apiInstance = new MailSlurp\Api\InboxControllerApi(
    // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
    // This is optional, `GuzzleHttp\Client` will be used as default.
    new GuzzleHttp\Client(),
$inbox_id = 'inbox_id_example'; // string | ID of the inbox you want to send the email from
$send_email_options = new \MailSlurp\Models\SendEmailOptions(); // \MailSlurp\Models\SendEmailOptions | Options for the email

try {
    $apiInstance->sendEmail($inbox_id, $send_email_options);
} catch (Exception $e) {
    echo 'Exception when calling InboxControllerApi->sendEmail: ', $e->getMessage(), PHP_EOL;

For more information see the MailSlurp documentation. It's free to sign-up!

3) Serverless MailServers

A final way to send emails in PHP is by using serverless email forms. These are HTML forms that you can submit directly to MailSlurp. You'll receive the results as emails. No sign-up is required.

// template for an html form that submits to mailslurp

  <!-- destination specified with hidden `_to` field -->

  <!-- use any named inputs, selects, textareas etc. -->
  <label>Test message</label>
  <textarea name="message"><textarea>

  <!-- files will be sent as attachments -->
  <label>Optional files</label>
  <input multiple name="files" type="file">

  <button type="submit">Send to me</button>


When submitted the form will be sent directly to the email you provided - no PHP code required!


Those are three easy ways to use emails in PHP: mail() function, MailSlurp library, or serverless forms.

Have fun!

Email and SMS Platform
Create a free account in 3 clicks