Last updated: August 29, 2026.
Names, subjects, and bodies can contain characters outside ASCII. Use UTF-8 for the complete message and let the mail library apply the required transfer encoding.
Set the message character set
<?php
require __DIR__ . '/vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
$mail = new PHPMailer(true);
$mail->CharSet = PHPMailer::CHARSET_UTF8;
$mail->Encoding = PHPMailer::ENCODING_BASE64;
$mail->setFrom('[email protected]', 'Cafe Example');
$mail->addAddress('[email protected]', 'Zoe');
$mail->Subject = 'Resume received';
$mail->Body = 'Your message was received.';
// Configure SMTP, then call $mail->send().Keep data UTF-8 end to end
- Save PHP source files as UTF-8.
- Use utf8mb4 for database text.
- Avoid unnecessary encoding conversions.
- Test accented text, non-Latin scripts, and emoji.
Keep UTF-8 across every boundary
Save templates as UTF-8, retrieve database text through a utf8mb4 connection, and set the mail library character set before assigning headers or bodies. Avoid converting text unless the source encoding is known.
Test accented sender names, non-Latin subjects, emoji, and both HTML and plain-text bodies. Inspect the received message in more than one client and verify that reply and forward operations preserve the text.
- Let the library encode headers.
- Use utf8mb4 in MySQL.
- Keep templates and source files consistently encoded.
Keep resource limits explicit and make failure cleanup part of the example. Log enough context to diagnose the operation without recording passwords, private message bodies, or uploaded file contents.
Continue with HTML and text email, SMTP with PHPMailer, and encoding comparison.
Practical implementation check
Put the operation in a small function or service with explicit inputs and a clear failure result. Test invalid input, missing extensions, permission failures, and concurrent requests. Production logs should identify the operation and record ID without storing secrets or private content. Clean up partially created files or queue records when any later step fails.
Reference: PHPMailer official repository.