When working with PHP, you may need to encode non-ASCII email header text as MIME encoded words. This guide explains the operation, shows what its arguments mean, and uses three separate examples to make the result easier to understand.
mb_encode_mimeheader: Email header fields have encoding and folding requirements distinct from email body text.
What Is mb_encode_mimeheader in PHP?
Encode non-ASCII email header text as MIME encoded words. Email header fields have encoding and folding requirements distinct from email body text.
Syntax:
mb_encode_mimeheader(string $string, ?string $charset = null, ?string $transferEncoding = null, string $newline = "
", int $indent = 0): string
Parameters and return value: The signature above identifies the arguments and return type where the API has a single call. For topic guides, follow the sequence of calls in the examples. Always distinguish a returned value from an object or variable modified by reference.
Important: Do not insert CR or LF characters from untrusted input into a header; avoid email header injection.
Example 1: Basic Usage
This example introduces the operation with a short and predictable input.
<?php
echo mb_encode_mimeheader("Café", "UTF-8", "B");
The code focuses on the main operation. Email header fields have encoding and folding requirements distinct from email body text.
Example 2: Another Case
Now compare a different input, locale, date, or option to see another part of the behavior.
<?php
echo mb_encode_mimeheader("Résumé", "UTF-8", "Q");
What changes? Do not insert CR or LF characters from untrusted input into a header; avoid email header injection.
Practical Example: Applying It in an Application
Use the following separate example as a starting point for a small application task. Check any extension or environment requirements below before running it.
<?php
$subject = "Monthly résumé";
$encodedSubject = mb_encode_mimeheader($subject, "UTF-8", "B");
echo $encodedSubject;
The practical example demonstrates how to apply this operation to multiple values, a reusable task, or a validated input. Email header fields have encoding and folding requirements distinct from email body text.
Important Behavior and Edge Cases
- Core behavior: Email header fields have encoding and folding requirements distinct from email body text.
- Inputs and results: Do not insert CR or LF characters from untrusted input into a header; avoid email header injection.
- Alternatives: mb_decode_mimeheader() decodes encoded incoming header values.
- Compatibility: mbstring is required for mb_* functions.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
mb_encode_mimeheader | Encode non-ascii email header text as mime encoded words. |
| Related PHP approach | mb_decode_mimeheader() decodes encoded incoming header values. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Using an incorrect input or option | Do not insert CR or LF characters from untrusted input into a header; avoid email header injection. |
| Confusing this API with a related operation | mb_decode_mimeheader() decodes encoded incoming header values. |
| Assuming output is identical across environments | Check return values and required extensions or versions. mbstring is required for mb_* functions. |
Key Takeaways
- mb_encode_mimeheader helps you encode non-ASCII email header text as MIME encoded words.
- Email header fields have encoding and folding requirements distinct from email body text.
- Do not insert CR or LF characters from untrusted input into a header; avoid email header injection.
Use mb_encode_mimeheader when you need to encode non-ASCII email header text as MIME encoded words. Start with the first example, compare the second, and adapt the practical example to your inputs. Confirm the return value and environment requirements before adding it to production code.