Development
How to Send Transactional Email From PHP

Transactional email from PHP should use an authenticated mail transport or provider API, a verified sender domain, and an application record that tracks delivery attempts. A successful API response means the provider accepted the message; it does not prove inbox placement.
Choose the transport around your application's operating requirements: credentials, regional needs, throughput, retry behavior, and the events you need to observe. Verify current provider limits and pricing directly rather than embedding assumptions in application code.
Send transactional email with a maintained library
A library handles message formatting and SMTP details more safely than hand-built headers. This example uses PHPMailer through Composer and an authenticated SMTP service. Install it with:
composer require phpmailer/phpmailer
The following PHP code assumes Composer's autoloader, validated recipient input, and environment variables configured outside the public directory. Replace the example sender with an address on your verified domain.
<?php
declare(strict_types=1);
use PHPMailer\PHPMailer\PHPMailer;
require __DIR__ . '/vendor/autoload.php';
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = (string) getenv('SMTP_HOST');
$mail->SMTPAuth = true;
$mail->Username = (string) getenv('SMTP_USER');
$mail->Password = (string) getenv('SMTP_PASSWORD');
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
$mail->Timeout = 15;
$mail->CharSet = 'UTF-8';
$mail->setFrom('receipts@example.com', 'Example Store');
$mail->addAddress('customer@example.net');
$mail->Subject = 'Your receipt is ready';
$mail->Body = 'Sign in to your account to view your receipt.';
$mail->send();
The transport settings must match your SMTP provider; some use implicit TLS on a different port. Do not disable certificate verification to resolve a connection failure. Consult the maintained PHPMailer project documentation for supported configuration and examples.
Fail application startup if required credentials are missing. Catch transport exceptions at the worker boundary, record a sanitized error, and avoid showing server details or credentials to the recipient.
Keep sender identity under your control
Use a verified domain in From. If a contact form needs replies to reach the visitor, validate the submitted address and set Reply-To through the library's API. Do not allow a user to provide arbitrary From headers or raw recipient lists.
Configure SPF and DKIM according to the sending provider, then align your DMARC policy with the actual sender setup. DNS presence alone is not proof of authentication success: inspect received message headers in your test accounts.
For HTML email, send a useful plain-text alternative and escape dynamic HTML content. Avoid placing sensitive data directly in the message when an authenticated account page can provide it instead.
Queue the business event, not just the network call
Save an email job alongside the business operation that requires it, using a transaction or an outbox pattern when appropriate. A background worker can retry transient transport failures without making checkout wait on SMTP.
Give each business message a stable identity, such as an order receipt version. Enforce uniqueness for that identity in storage. Otherwise a retried payment event can generate multiple email jobs before the transport is involved.
An SMTP timeout can leave the send outcome uncertain. Blind retries may duplicate mail. Record attempts and provider identifiers where available, and choose a documented policy for uncertain delivery rather than promising exactly-once sending.
Observe delivery after acceptance
Track queued, attempted, accepted, bounced, and permanently failed states separately. Verify provider webhook signatures and make event processing idempotent. Suppress addresses after relevant permanent failures according to your sending policy.
Do not log password-reset tokens, complete message bodies, or SMTP passwords. A request or job identifier is usually enough to connect application logs with provider events.
Test against a non-production mailbox or mail capture service. Cover malformed addresses, missing credentials, authentication failure, timeout, retry exhaustion, duplicate business events, and bounce processing. Do not send test messages to real customers.
For application boundaries, see incremental PHP refactoring. Discuss your transactional email workflow when delivery failures are hard to trace.