PHP

Send Plain Email via mail()

admin by @admin ADMIN
5h ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
A thin wrapper around PHP's mail() with proper headers (From, Reply-To, Content-Type with UTF-8). For anything serious reach for PHPMailer or Symfony Mailer — this is fine for transactional one-offs.
PHP
Raw
<?php
function sendEmail(string $to, string $subject, string $body, string $from): bool {
    $headers = [
        'From'         => $from,
        'Reply-To'     => $from,
        'MIME-Version' => '1.0',
        'Content-Type' => 'text/plain; charset=UTF-8',
        'X-Mailer'     => 'PHP/' . PHP_VERSION,
    ];
    $headerLines = '';
    foreach ($headers as $k => $v) $headerLines .= "$k: $v\r\n";
    // Encode the subject so non-ASCII renders correctly.
    $subject = '=?UTF-8?B?' . base64_encode($subject) . '?=';
    return mail($to, $subject, $body, $headerLines);
}

sendEmail('user@example.com', 'Welcome!', "Thanks for signing up.\n", 'noreply@myapp.com');
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.