P H O T O N

CONSOLE

Loading

PHP 8+ Architecture

PHPMailer SMTP Relay Configuration
Secure TLS Authentication, UTF-8 & Error Handling

Replace fragile PHP mail() functions with production-ready PHPMailer 6.x code blocks connected to Photon Console's high-throughput relay.

6 min read • PHP 8.1 - 8.4 Tested • Port 587 STARTTLS
Direct Answer: Configuring PHPMailer with SMTP Relay

Configuring PHPMailer to send through an authenticated external SMTP relay requires initializing $mail->isSMTP(), setting $mail->Host to your relay endpoint, enforcing $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS on port 587, and authenticating with API credentials. This prevents PHP mail() sendmail failures and stops emails from landing in spam.

1. Installing PHPMailer via Composer

Install the official PHPMailer package into your project root using Composer:

composer require phpmailer/phpmailer

2. Complete Production-Ready PHP 8+ Script

Copy and paste this hardened script into your application:

<?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->isSMTP(); $mail->Host = 'smtp.photonconsole.com'; $mail->SMTPAuth = true; $mail->Username = getenv('PHOTON_SMTP_USER'); $mail->Password = getenv('PHOTON_SMTP_PASS'); $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; $mail->Port = 587; $mail->CharSet = 'UTF-8'; // Recipients $mail->setFrom('billing@yourdomain.com', 'Acme Billing'); $mail->addAddress('sarah@example.com', 'Sarah Connor'); $mail->addReplyTo('support@yourdomain.com', 'Support Team'); // Content $mail->isHTML(true); $mail->Subject = 'Your Order Receipt #99281'; $mail->Body = '<h1>Payment Received</h1><p>Thank you for your order!</p>'; $mail->AltBody = 'Thank you for your order! Your payment was received.'; $mail->send(); echo 'Message has been sent successfully!'; } catch (Exception $e) { error_log("PHPMailer Error: {$mail->ErrorInfo}"); echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"; }

3. Adding HTML Templates & PDF Attachments

Attach invoices or reports cleanly with MIME verification:

// Attach files safely $mail->addAttachment('/var/invoices/inv_99281.pdf', 'Invoice_99281.pdf');

4. Diagnosing SMTP Debug Output

In staging or development environments, enable verbose TLS handshake output:

$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Verbose socket debugging

5. Frequently Asked Questions

Why is PHPMailer better than PHP's built-in mail() function?

PHP's native mail() function relies on the local server's sendmail binary without cryptographic authentication, causing emails to fail SPF/DKIM and land in spam. PHPMailer connects directly to an authenticated external SMTP relay over TLS, ensuring 99%+ deliverability.

Which port and encryption should I use with PHPMailer?

Use port 587 with STARTTLS encryption ($mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS), or port 465 with implicit SSL ($mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS). Port 25 should always be avoided as it is blocked by most cloud providers.

How do I handle connection timeouts or failed sends in PHPMailer?

Wrap the $mail->send() call in a try/catch block catching PHPMailer\PHPMailer\Exception, log the $mail->ErrorInfo telemetry, and implement an exponential backoff retry loop with a maximum of 3 attempts.

Send Sub-Second Emails in PHP with PhotonRelay

Plug your PHPMailer script into Photon Console's Anycast edge relay. Experience sub-700ms delivery, automated bounce handling, and 10,000 free monthly emails.