1 (edited by peacecop kalmer: 07/05/2020 11:59:41 pm)

Topic: Mailing doesn't work if the header contains "&"

I've struggled hours for figuring out the reason why my mails won't appear in target. It turned out that after installing and configuring "Postfix", "FrontAccounting" told me that the mail was sent but nothing really was sent. So I dug into the files and started to debug. After many hours, it turned out that mails containing a header containing "&" weren't sent and other mails were. So I started to look for the file where the company name that contains that stuff is being read out and couldn't find it. I decided to make the following modification replacing lines 149-150 in the file reporting/includes/class.mail.inc:

            if (mail($mail, $this->subject, $this->body, $this->header, $this->add_params))


            if (mail($mail, $this->subject, htmlspecialchars_decode($this->body, ENT_NOQUOTES), htmlspecialchars_decode($this->header, ENT_NOQUOTES), $this->add_params))

Please notice that I decided to convert both the header and body for that purpose. The company name is "Tegusad Õpilased & Tudengid" and in the database, it's "Tegusad Õpilased & Tudengid". Now, it works. Maybe, it's just a hack but I couldn't find any other way to solve this problem.

And before that, I had to make one more important change: into the file "/etc/php/7.2/apache2/php.ini":

SMTP = test.tennis24.ee

Before that, the value was localhost. I changed the host name for the sake of Postfix. Why localhost isn't recognized anymore, I don't know.

Re: Mailing doesn't work if the header contains "&"

The localhost is recognised  as junk mailer in all the email servers. So they don't get your emails to their server from localhost.  But it will work if you connect with any realtime smtp servers. Basically the reason is simple. When the marketing people send email. They usually use software before to send bulk mails. That usually happen from local pc. So they detect it as spam message. That's the reason your mail no received any where and it shows you as mail sent.

If you want to good delivery. Try to configure phpmailer with you to get it work better delivery to most of the emails

Subscription service based on FA
HRM CRM POS batch Themes

Re: Mailing doesn't work if the header contains "&"

Make sure you have a valid domain in your /etc/hosts (Windows: C:\Windows\System32\drivers\etc\hosts) for along with localhost (space separated or on a new line with the said IP)

Then the valid domain in your php.ini and the changes to your php script should work fine provided your DNS SPF entry is okay for the IP you will be dishing out.

4 (edited by peacecop kalmer: 08/01/2020 12:35:53 am)

Re: Mailing doesn't work if the header contains "&"

Meanwhile, I discovered that the receiver's name would still contain "&". And as the name contained many parts separated by spaces, the recipient part of the mail was screwed up. Originally, it should be:

Tegusad Õpilased & Tudengid <pandeero@gmail.com>

It was translated to:

Tegusad@test.tennis24.ee, Õpilased@test.tennis24.ee, &@test.tennis24.ee, Tudengid

So I had to remove these two mistakes:

                        if (mail(preg_replace('/(.+)(\s\s\<.+)/','"$1"$2', htmlspecialchars_decode($mail,ENT_NOQUOTES)), $this->subject,\
 htmlspecialchars_decode($this->body, ENT_NOQUOTES), htmlspecialchars_decode($this->header, ENT_NOQUOTES), $this->add_params))

This is only my copy. So I want you to put this change into the repository of the source code. My solution might not cover all the possible test cases but at least it's better than the previous.
You can test regular expressions at https://www.phpliveregex.com/#tab-preg-replace

Re: Mailing doesn't work if the header contains "&"

Thanks for telling us.

I will just wait a while when receiving some test about this. Please try it. If ok, I will commit asap.


Re: Mailing doesn't work if the header contains "&"

Are you still waiting? In the version "2.4.9", the bug's still present.

Re: Mailing doesn't work if the header contains "&"

I am just waiting for somebody else will test this, please. Then I will commit the changes. So please try @peacecop kalmer's solution.


8 (edited by peacecop kalmer: 10/08/2021 08:48:38 pm)

Re: Mailing doesn't work if the header contains "&"

The version "2.4.10" still has this bug present. I think that nobody else cares. Why don't you test it on your own and if you're convinced that my solution works commit it?

The subject line is also affected. Therefore, it also has to be converted:

                        if (mail(preg_replace('/(.+)(\s\s\<.+)/','"$1"$2', htmlspecialchars_decode($mail,ENT_NOQUOTES)), htmlspecialchars_decode($this->subject, ENT_NOQUOTES),\
htmlspecialchars_decode($this->body, ENT_NOQUOTES), htmlspecialchars_decode($this->header, ENT_NOQUOTES), $this->add_params))

Re: Mailing doesn't work if the header contains "&"

Hello Guys,
Please help @peacecop kalmer and me to try this change out. I am not keen on implementing it without some more testing.
Do you Guys think we can make this changes


Re: Mailing doesn't work if the header contains "&"

Hi, I decided to help out so I set up the following environment.
Linux Mint 19
MySql 5.7
Php 7.2
Apache 2.4
Postfix 3.3.0

I also setup Mail Relay thru gmail SMTP [smtp.gmail.com]:587

1. With no change to Code:

Setup Company name Tegusad Õpilased & Tudengid
Created Invoice to Customer Donlad Easter
Emaild the invoice. to user@example.com


Mail is sent and received.
In the company Name the & is displayed as &amp;(Tegusad Õpilased &amp; Tudengid)
The from field is displayed as Tegusad@zkbts, Õpilased@zkbts, &amp@zkbts, Tudengid <>

2. With no change to code but user is user<user@example.com>

Mail is not sent. Error in postfix log (bad address syntax). the < and > are translated into <user&lt;user@example.com&gt;>

3. With changes incorporated as in peacecop suggestions and user is user@example.com

Mail is sent and received, The Subject header (Invoice 17 from Tegusad Õpilased &amp; Tudengid) still show &amp; in all other places its correctly &.

4. With changes incorporated as in peacecop suggestions and user is user<user@example.com>

Mail is not sent. <user@example.com>> (bad address syntax) Extra > in the address.