Skip to main content
Send emails through Lettr using PHPMailer, the popular PHP email library. PHPMailer works in any PHP project — WordPress, custom applications, or legacy codebases.

Prerequisites

Before you begin, make sure you have: You’ll also need:
  • PHP 8.0+ installed
  • Composer for package management (or manual installation)

Installation

Install PHPMailer via Composer:
composer require phpmailer/phpmailer
Or download manually from GitHub and require the files:
require 'path/to/PHPMailer/src/Exception.php';
require 'path/to/PHPMailer/src/PHPMailer.php';
require 'path/to/PHPMailer/src/SMTP.php';

Basic Configuration

Here’s a complete example showing how to send an email through Lettr with PHPMailer:
<?php

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

require 'vendor/autoload.php';

$mail = new PHPMailer(true);

try {
    // Server settings
    $mail->SMTPDebug = SMTP::DEBUG_OFF;                 // Disable debug output
    $mail->isSMTP();                                    // Use SMTP
    $mail->Host       = 'smtp.lettr.com';               // Lettr SMTP server
    $mail->SMTPAuth   = true;                           // Enable authentication
    $mail->Username   = 'lettr';                        // SMTP username
    $mail->Password   = 'lttr_your_api_key_here';       // Your Lettr API key
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption
    $mail->Port       = 587;                            // TCP port

    // Recipients
    $mail->setFrom('you@yourdomain.com', 'Your Name');
    $mail->addAddress('recipient@example.com', 'Recipient Name');
    $mail->addReplyTo('reply@yourdomain.com', 'Reply Address');

    // Content
    $mail->isHTML(true);                                // Set email format to HTML
    $mail->Subject = 'Hello from Lettr';
    $mail->Body    = '<h1>Hello!</h1><p>Welcome to <b>Lettr</b>!</p>';
    $mail->AltBody = 'Hello! Welcome to Lettr!';        // Plain text for non-HTML clients

    $mail->send();
    echo 'Message has been sent successfully';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
Replace lttr_your_api_key_here with your actual Lettr API key. Never commit API keys to version control.

Adding Attachments

Attach files to your emails using addAttachment():
// Attach a PDF file
$mail->addAttachment('/path/to/invoice.pdf', 'invoice.pdf');

// Attach an image
$mail->addAttachment('/path/to/logo.png', 'company-logo.png');

// Attach multiple files
$mail->addAttachment('/path/to/file1.pdf');
$mail->addAttachment('/path/to/file2.docx');
The first parameter is the file path, the second (optional) is the filename shown to recipients.

Adding CC and BCC

Send copies to multiple recipients:
// CC (carbon copy) - visible to all recipients
$mail->addCC('manager@example.com');
$mail->addCC('team@example.com', 'Team Lead');

// BCC (blind carbon copy) - hidden from other recipients
$mail->addBCC('bcc@example.com');
$mail->addBCC('archive@example.com');

// Multiple primary recipients
$mail->addAddress('user1@example.com');
$mail->addAddress('user2@example.com');

Custom Headers

Add custom headers for tracking or additional metadata:
$mail->addCustomHeader('X-Campaign-ID', 'newsletter-2024-01');
$mail->addCustomHeader('X-Customer-ID', '12345');

WordPress Integration

If you’re using PHPMailer in WordPress, hook into phpmailer_init:
add_action('phpmailer_init', function($phpmailer) {
    $phpmailer->isSMTP();
    $phpmailer->Host       = 'smtp.lettr.com';
    $phpmailer->SMTPAuth   = true;
    $phpmailer->Username   = 'lettr';
    $phpmailer->Password   = 'lttr_your_api_key_here';
    $phpmailer->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $phpmailer->Port       = 587;
});
Add this to your theme’s functions.php or a custom plugin.

Troubleshooting

Check your API key is correct and starts with lttr_. Verify the username is exactly lettr (lowercase). Ensure your API key hasn’t been revoked in the dashboard.
Your firewall may be blocking outbound SMTP connections on port 587. Try alternate ports (465, 2465, or 2587). Verify smtp.lettr.com resolves correctly: nslookup smtp.lettr.com.
Check that all email addresses are properly formatted. Ensure the “from” address uses a verified sending domain.
Verify attachment file paths are correct and the PHP process has read permissions. Check that files exist using file_exists() before attaching.
Enable SMTP debugging during development:
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
Debug levels:
  • SMTP::DEBUG_OFF (0) — No output (production)
  • SMTP::DEBUG_CLIENT (1) — Client messages
  • SMTP::DEBUG_SERVER (2) — Client and server messages (recommended)
  • SMTP::DEBUG_CONNECTION (3) — Connection status + messages
  • SMTP::DEBUG_LOWLEVEL (4) — Low-level data output
Never enable debug mode in production. It outputs sensitive information including your API key.
If you prefer port 465 with implicit SSL/TLS:
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->Port       = 465;
Both ports are equally secure. Port 465 establishes an encrypted connection immediately, while port 587 upgrades via STARTTLS.

What’s Next