ellite/Wallos · error · Exception
$this->lang('encoding') . $encoding
Error message
$this->lang('encoding') . $encoding What it means
addAttachment() throws lang('encoding') plus the given encoding string when validateEncoding() rejects the $encoding argument. Only the constants PHPMailer supports (7bit, 8bit, base64, binary, quoted-printable) are accepted for attachments.
Solutions
- Use the class constants: PHPMailer::ENCODING_BASE64, ENCODING_7BIT, ENCODING_8BIT, ENCODING_BINARY, ENCODING_QUOTED_PRINTABLE
- Remove the explicit encoding argument and rely on the default (base64)
- Check for typos/case: the accepted values are lowercase exact strings
Example fix
// before $mail->addAttachment($path, 'report.pdf', 'base-64'); // after $mail->addAttachment($path, 'report.pdf', PHPMailer::ENCODING_BASE64);
Defensive patterns
Strategy: type-guard
Validate before calling
$allowed = [PHPMailer::ENCODING_7BIT, PHPMailer::ENCODING_8BIT, PHPMailer::ENCODING_BASE64, PHPMailer::ENCODING_BINARY, PHPMailer::ENCODING_QUOTED_PRINTABLE];
if (!in_array($encoding, $allowed, true)) { throw new InvalidArgumentException("Unsupported attachment encoding: $encoding"); } Type guard
function isValidMailEncoding($e): bool { return is_string($e) && in_array($e, ['7bit','8bit','base64','binary','quoted-printable'], true); } Try / catch
try { $mail->addAttachment($path, $name, $encoding); } catch (PHPMailer\PHPMailer\Exception $e) { if (str_contains($e->getMessage(), 'encoding')) { $mail->addAttachment($path, $name, PHPMailer::ENCODING_BASE64); } else { throw $e; } } Prevention
- Always pass the PHPMailer::ENCODING_* constants, never raw strings
- Never pass charset values (utf-8) where an encoding is expected
- Centralize attachment calls in a wrapper that validates the encoding constant
When it happens
Trigger: Passing an unsupported encoding such as 'base 64', 'BASE64', 'utf-8', or a typo to addAttachment($path, $name, $encoding).
Common situations: Developers confusing content encoding with charset and passing 'utf-8' or 'iso-8859-1'; hand-typed 'base64' vs the constant is fine, but misspellings like 'base-64' fail.
Understand the failure class
Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.
Related errors
- %s: %s
- $this->lang('file_access') . $path
- $this->lang('file_open') . $path
- ( )
- Invalid address (to/cc/bcc)
AI-assisted analysis of ellite/Wallos@52820e87ca (2026-09-13).
Data as JSON: /api/errors/b7db2bcbb6a448d7.
Report an issue: GitHub.
Appendix: source
Thrown at libs/PHPMailer/PHPMailer.php:3241
$type = '',
$disposition = 'attachment'
) {
try {
if (!static::fileIsAccessible($path)) {
throw new Exception($this->lang('file_access') . $path, self::STOP_CONTINUE);
}
//If a MIME type is not specified, try to work it out from the file name
if ('' === $type) {
$type = static::filenameToType($path);
}
$filename = (string) static::mb_pathinfo($path, PATHINFO_BASENAME);
if ('' === $name) {
$name = $filename;
}
if (!$this->validateEncoding($encoding)) {
throw new Exception($this->lang('encoding') . $encoding);
}
$this->attachment[] = [
0 => $path,
1 => $filename,
2 => $name,
3 => $encoding,
4 => $type,
5 => false, //isStringAttachment
6 => $disposition,
7 => $name,
];
} catch (Exception $exc) {
$this->setError($exc->getMessage());
$this->edebug($exc->getMessage());
if ($this->exceptions) {
throw $exc;
}View on GitHub (pinned to 52820e87ca)