# PHPMailer (SMTP)

> Send your first email from PHP with PHPMailer over the MailBlastr SMTP relay.

If your PHP app already uses [PHPMailer](https://github.com/PHPMailer/PHPMailer), you can send through MailBlastr over SMTP. Point PHPMailer at the MailBlastr relay and authenticate with your API key. SMTP sends appear in your dashboard and event log just like API sends.

## Prerequisites

- A MailBlastr [API key](https://www.mailblastr.com/docs/api-keys/overview).
- A [verified domain](https://www.mailblastr.com/docs/domains/managing) to send from.

## 1. Install

Add the `phpmailer/phpmailer` package with Composer.

```bash
composer require phpmailer/phpmailer
```

## 2. SMTP credentials

Configure PHPMailer with the following credentials:

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `Host` | string | No | `smtp.mailblastr.com` |
| `Port` | number | No | `587` (STARTTLS) or `465` (implicit TLS). |
| `Username` | string | No | `mailblastr` |
| `Password` | string | No | Your API key, e.g. `mb_xxxxxxxxx`. |

## 3. Send email using SMTP

Use those credentials to send. The `setFrom` address must be on a verified domain.

```php
<?php

// Include Composer autoload file to load PHPMailer classes
require __DIR__ . '/vendor/autoload.php';

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

$mail = new PHPMailer(true);

try {
    $mail->isSMTP();
    $mail->Host = 'smtp.mailblastr.com';
    $mail->SMTPAuth = true;
    $mail->Username = 'mailblastr';
    $mail->Password = 'mb_xxxxxxxxx';
    $mail->SMTPSecure = 'tls';
    $mail->Port = 587;

    // Set email format to HTML
    $mail->isHTML(true);

    $mail->setFrom('onboarding@yourdomain.com');
    $mail->addAddress('delivered@example.com');
    $mail->Subject = 'Hello World';
    $mail->Body = '<strong>It works!</strong>';

    $mail->send();

    // Log the successfully sent message
    echo 'Email successfully sent';
} catch (Exception $e) {
    // Log the detailed error for debugging
    error_log('Mailer Error: ' . $mail->ErrorInfo);
    // Show a generic error message to the user
    echo 'There was an error sending the email.';
}
```

> **Note:** For implicit TLS, set `$mail->SMTPSecure = 'ssl';` and `$mail->Port = 465;`. See [Send an email](https://www.mailblastr.com/docs/api/emails-send) if you would rather use the HTTP API.
