r/PHPhelp • u/Ok_Strike9189 • May 14 '26
Solved How to send HTML email with mail() without destroying DKIM key or email appearance
I already know how to send HTML email through the PHP mail() function, however many external email providers may ditch my outgoing emails if they are not properly signed with a DKIM key.
Now if I try sending a super short message, I have no problem, but if I'm sending an entire newsletter as HTML with no line breaks (because I like to reduce the payload on the network), then the DKIM key breaks and many validators would complain.
One thing I did was use the chunk_split command on the entire message. Everytime I used that, the DKIM check always passes however, some of the HTML code is corrupted (probably because of the carriage return and line feed within HTML tags).
Is there another built-in PHP command I could use to replace chunk_split?
Then again, I'll probably have to manually split it and put a CRLF at the end of each HTML tag, then that may be a problem if I have a paragraph of text exceeding 80 characters.
Please advise.
11
u/Csysadmin May 14 '26
I would suggest not optimising line break usage to reduce the payload on the network. A line break in email is normally CRLF (two bytes), if you have 500 of them it's only about 1KB.
Email has old transport rules, including line-length limits. Long lines are risky because mail servers will rewrap or re-encode them. If that happens after DKIM signing, DKIM will likely fail.
Good:
Bad:
Keep reasonable line breaks, avoid one giant line.
Look at:
Basically means: "Make my HTML email transport-safe before signing/sending it."
1
u/Ok_Strike9189 May 15 '26
Ok I came up with an answer to my own issue...
As the message is HTML with no base-64 encoded images and all HTML tags being short in length and no spaces between the closing and opening tags, This is my solution:
$outputmessage=wordwrap(str_replace("><",">\r\n<",trim($message)),75,"\r\n");Basically it removes leading and trailing whitespace from the input HTML, injects a CRLF between every closing and opening tag, then word wraps the output to 75 characters, and adds a CRLF at the end of each fragment.
The source code of the resulting email would span dozens of pages, but at least it works. Best of all this even works in PHP 5.6 and higher (including 8).
8
u/colshrapnel May 14 '26 edited May 14 '26
Honestly, I don't understand this kind of reasoning. A whim ("i like to reduce the payload") against a properly functioning application? If your emails are OK without that "reduction" then there is a very simple solution to your problem.
Still your problem is not clear. What exactly do you mean with "DKIM key breaks"?
This one I also don't understand. Why not to use an established library, such as PHPMailer
4
u/chmod777 May 14 '26
If these are transacrional emails, use a service.
Spam, aka marketing emails, also use a service.
Basically... use a service and stop worrying about this.
3
u/allen_jb May 14 '26
At what point are you modifying the message?
The DKIM signature signs the specified headers and includes a hash of the body content. If you modify these values after the DKIM signing has been done, you're going to cause the DKIM signature to become invalid.
The actual content of the message (particularly with regards to line returns) shouldn't matter, as long as you're signing it after that content is finalized.
You might also want to check if your mail server has the option to sign outgoing messages. This way you don't have to handle it in your application code, and the DKIM signature should always be valid.
2
u/MartinMystikJonas May 14 '26
Show us your code you are probably doing something wrong (messing up linebreaks in headers, removing line breaks AFTER you sign content with them,... )
3
u/NoDoze- May 14 '26
Must you use mail()? It is only good for server notifications and intranet email.
PHPMailer is the best for internet emails. It can send html and text emails at the same time, compatible with any email server/service, and is super easy to implement.
1
u/klimenttoshkov May 14 '26
DKIM may be calculated without the body hash. But body hash is also included in the headers. Just to be exact.
1
u/InboxProtector May 21 '26
The DKIM breakage with long lines is a known issue RFC 5322 has a 998 character hard limit per line and DKIM validators get unhappy when lines exceed 76-78 characters. Instead of chunk_split on the whole body, use
quoted_printable_encode()on your HTML body before passing it to mail() and set the Content-Transfer-Encoding header toquoted-printable,that handles line length limits correctly without corrupting HTML tags the way chunk_split does. I am working in PowerDMARC, I know that even the free tool there would help you.0
u/viniciusldemelo May 14 '26
Já tentou usando encoding quoted-printable? Acontecerá a quebra automaticamente em 75 caracteres, encode caracteres especiais e nunca corta tags HTML.