# PHPMailer

:::info
This guide covers SMTP integration. For full configuration options and available headers, see the [SMTP Introduction](/guides/send-email-with-smtp).
:::

## Installation

Install PHPMailer via Composer:

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

## Basic Configuration

Use environment variables for credentials:

```php
<?php
// Load from environment or config
$smtpHost = 'smtp.lettermint.co';
$smtpUsername = 'lettermint';
$smtpPassword = getenv('LETTERMINT_PROJECT_TOKEN'); // Your API token
```

<CodeTabs>
```php title="Port 587 (STARTTLS)"
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

$mail = new PHPMailer(true);

try {
    // Server settings
    $mail->isSMTP();
    $mail->Host       = 'smtp.lettermint.co';
    $mail->SMTPAuth   = true;
    $mail->Username   = 'lettermint';
    $mail->Password   = getenv('LETTERMINT_PROJECT_TOKEN');
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $mail->Port       = 587;

    // Recipients
    $mail->setFrom('sender@yourdomain.com', 'Your Name');
    $mail->addAddress('recipient@example.com', 'Recipient Name');

    // Content
    $mail->isHTML(true);
    $mail->Subject = 'Test Email';
    $mail->Body    = '<h1>Hello!</h1><p>This is a test email.</p>';
    $mail->AltBody = 'Hello! This is a test email.';

    $mail->send();
    echo 'Message sent successfully';
} catch (Exception $e) {
    echo "Message could not be sent. Error: {$mail->ErrorInfo}";
}
```

```php title="Port 465 (Implicit TLS)"
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

$mail = new PHPMailer(true);

try {
    // Server settings
    $mail->isSMTP();
    $mail->Host       = 'smtp.lettermint.co';
    $mail->SMTPAuth   = true;
    $mail->Username   = 'lettermint';
    $mail->Password   = getenv('LETTERMINT_PROJECT_TOKEN');
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // Implicit TLS
    $mail->Port       = 465;

    // Recipients
    $mail->setFrom('sender@yourdomain.com', 'Your Name');
    $mail->addAddress('recipient@example.com', 'Recipient Name');

    // Content
    $mail->isHTML(true);
    $mail->Subject = 'Test Email';
    $mail->Body    = '<h1>Hello!</h1><p>This is a test email.</p>';
    $mail->AltBody = 'Hello! This is a test email.';

    $mail->send();
    echo 'Message sent successfully';
} catch (Exception $e) {
    echo "Message could not be sent. Error: {$mail->ErrorInfo}";
}
```
</CodeTabs>

## Advanced Features

### Multiple Recipients

```php
$mail->addAddress('user1@example.com');
$mail->addAddress('user2@example.com');
$mail->addCC('manager@yourdomain.com');
$mail->addBCC('archive@yourdomain.com');
```

### Attachments

```php
$mail->addAttachment('/path/to/file.pdf');
$mail->addAttachment('/path/to/image.jpg', 'custom-name.jpg');
```

### Custom Headers

```php
$mail->addCustomHeader('X-Custom-Header', 'Custom Value');
$mail->addCustomHeader('X-Priority', '1');
```

### Lettermint Headers

Add Lettermint-specific headers for tags, metadata, and routing:

```php
// Tag for categorization
$mail->addCustomHeader('X-LM-Tag', 'order-confirmation');

// Metadata for tracking (included in webhooks)
$mail->addCustomHeader('X-LM-Metadata-order_id', '12345');
$mail->addCustomHeader('X-LM-Metadata-customer_id', 'cust_789');

// Route selection
$mail->addCustomHeader('X-Lettermint-Route', 'transactional');
```

:::note
See the [SMTP Introduction](/guides/send-email-with-smtp#custom-headers) for full details on available headers and their formats.
:::
