← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

PHP Mail & SMTP

php PHP 4.0+ Beginner

Also Known As

mail() PHPMailer SMTP PHP Symfony Mailer email PHP sendmail PHP

TL;DR

Sending email from PHP using mail(), SMTP libraries like PHPMailer or Symfony Mailer, or transactional email APIs — with critical configuration for deliverability, security, and reliability.

Explanation

PHP's built-in mail() function is a thin wrapper around the server's sendmail binary. It works on correctly configured servers but has no SMTP authentication, no TLS support, no error handling beyond a boolean return, and no queue — if the mail server is unavailable, the email is silently lost. For production, PHPMailer or Symfony Mailer send via authenticated SMTP with TLS, provide proper error exceptions, and support HTML email with embedded images. Transactional email APIs (Mailgun, SendGrid, Amazon SES, Postmark) provide delivery tracking, bounce handling, and high deliverability without managing an SMTP server. Deliverability requires three DNS records: SPF (which servers may send on your behalf), DKIM (cryptographic signature proving origin), and DMARC (policy for failed SPF/DKIM). Missing these records causes emails to land in spam or be rejected silently.

Common Misconception

If mail() returns true the email was delivered. mail() returns true if the message was accepted by the local mail transfer agent — not if it was delivered to the recipient. The MTA may queue it, the recipient server may reject it, or it may land in spam days later with no notification to PHP. PHPMailer with SMTP and exceptions provides actual delivery confirmation to the sending server; for end-to-end delivery tracking, a transactional API with webhooks is required.

Why It Matters

Email is a critical communication channel for most PHP applications — account verification, password resets, order confirmations, and notifications. Using mail() without SPF/DKIM records causes verification emails to land in spam, blocking user registration funnels. Sending from a shared hosting IP with no authentication causes emails to be rejected entirely. PHPMailer with a reputable SMTP service and correct DNS records is a two-hour setup that prevents months of deliverability problems.

Common Mistakes

  • Using mail() in production without SPF, DKIM, and DMARC DNS records — emails go to spam or are rejected.
  • Not using a queue for email sending — synchronous email blocks the HTTP response and fails silently if the SMTP server is slow.
  • Not validating email addresses with filter_var($email, FILTER_VALIDATE_EMAIL) before sending.
  • Exposing SMTP credentials in version-controlled config files — always use environment variables for SMTP passwords and API keys.

Code Examples

✗ Vulnerable
// mail() — no error handling, no TLS, poor deliverability
$sent = mail(
    $_POST['email'],  // unvalidated
    'Welcome',
    'Thanks for signing up',
    'From: noreply@example.com' // likely to be spam
);
// Returns true even if email never delivered
✓ Fixed
// PHPMailer with SMTP + TLS
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

$mail = new PHPMailer(true); // true = throw exceptions
try {
    $mail->isSMTP();
    $mail->Host       = $_ENV['SMTP_HOST'];
    $mail->SMTPAuth   = true;
    $mail->Username   = $_ENV['SMTP_USER'];
    $mail->Password   = $_ENV['SMTP_PASS'];
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $mail->Port       = 587;
    $mail->setFrom('noreply@example.com', 'My App');
    $mail->addAddress(filter_var($email, FILTER_VALIDATE_EMAIL));
    $mail->Subject = 'Welcome';
    $mail->Body    = 'Thanks for signing up';
    $mail->send();
} catch (Exception $e) {
    logger()->error('Mail failed: ' . $e->getMessage());
}

Added 23 Mar 2026
Edited 4 Apr 2026
Views 20
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings F 0 pings S 2 pings S 0 pings M 0 pings T 1 ping W 0 pings T 0 pings F 0 pings S 2 pings S 0 pings M 0 pings T 0 pings W 1 ping T 0 pings F 0 pings S 2 pings S 0 pings M 0 pings T 0 pings W 1 ping T 0 pings F 0 pings S 2 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S
No pings yet today
No pings yesterday
Amazonbot 8 Google 6 Perplexity 2 ChatGPT 1 Meta AI 1 Ahrefs 1
crawler 16 crawler_json 3
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: Low
⚡ Quick Fix
Replace mail() with PHPMailer using SMTP + TLS, or use a transactional API (Mailgun/SendGrid). Add SPF, DKIM, DMARC DNS records. Queue email sending with Laravel Queue or similar
📦 Applies To
PHP 4.0+ web cli

✓ schema.org compliant