System sent Mails not arriving, but User sent Mails work

I have made an interesting discovery that I have not yet found in any forum. I use Invoice Ninja “v5.8.33-C156” on Docker and have several companies in the application. 3 of them to be exact. I want to use a separate mail server with a separate sender for each of these 3 companies. I have made this possible with the ENV file by always adding the company ID and a sub-string in front of the variables (MAIL_ENCRYPTION, MAIL_FROM_ADDRESS, MAIL_FROM_NAME, MAIL_HOST, MAIL_PASSWORD, MAIL_PORT, MAIL_USERNAME), for example 1_MAIL_ENCRYPTION.

This also works relatively well. But today I noticed that automatic invoice dispatch no longer works. They are now sent from hello@example.com instead of the correct email address and therefore no longer reach the customer. This also happens when I want to create new users and they receive a confirmation email. This is sent from hello@example.com instead of the correct email address in the ENV file.

The strange thing is, when i manually send invoices, they arrive like a charm with the correct mail sender.

My question now is, how can I fix this error? I would like to run all 3 companies in one Invoice Ninja instance, but in a way that it works and I can use a separate SMTP provider for all 3 companies.

I would be very grateful for any help as I love using Invoice Ninja and supporting it!

This is my ENV:

# IN application vars
APP_URL=https://XXX
APP_KEY=XXX
APP_DEBUG=false
REQUIRE_HTTPS=true
PHANTOMJS_PDF_GENERATION=false
PDF_GENERATOR=snappdf
TRUSTED_PROXIES='*'

QUEUE_CONNECTION=database

# DB connection
DB_HOST=db
DB_PORT=3306
DB_DATABASE=ninja
DB_USERNAME=XXX
DB_PASSWORD=XXX

# Create initial user
# Default to these values if empty
# IN_USER_EMAIL=admin@example.com
# IN_PASSWORD=changeme!
IN_USER_EMAIL=XXX
IN_PASSWORD=XXX

# Mail options

MAIL_MAILER=smtp

# First Company
1_MAIL_HOST=mail.XXX
1_MAIL_PORT=587
1_MAIL_USERNAME=no-reply@XXX1
1_MAIL_PASSWORD=XXX
1_MAIL_ENCRYPTION=STARTTLS
1_MAIL_FROM_ADDRESS='XXX'
1_MAIL_FROM_NAME='XXX'

# Second Company
4_MAIL_HOST=mail.XXX
4_MAIL_PORT=587
4_MAIL_USERNAME=no-reply@XXX2
4_MAIL_PASSWORD=XXX
4_MAIL_ENCRYPTION=STARTTLS
4_MAIL_FROM_ADDRESS='XXX'
4_MAIL_FROM_NAME='XXX'

# Third Company
7_MAIL_HOST=mail.XXX
7_MAIL_PORT=587
7_MAIL_USERNAME=no-reply@XXX3
7_MAIL_PASSWORD=XXX
7_MAIL_ENCRYPTION=STARTTLS
7_MAIL_FROM_ADDRESS='XXX'
7_MAIL_FROM_NAME='XXX'

# MySQL
MYSQL_ROOT_PASSWORD=XXX
MYSQL_USER=XXX
MYSQL_PASSWORD=XXX
MYSQL_DATABASE=ninja

# V4 env vars
# DB_STRICT=false
# APP_CIPHER=AES-256-CBC

And as i said, if I add a “normal” mail block as shown below, everything works (but all 3 companies are then on one smtp sender)

MAIL_HOST=mail.XXX
MAIL_PORT=587
MAIL_USERNAME=no-reply@XXX1
MAIL_PASSWORD=XXX
MAIL_ENCRYPTION=STARTTLS
MAIL_FROM_ADDRESS='XXX'
MAIL_FROM_NAME='XXX'

Many thanks and best regards
Noah

Hi,

If you think this is a bug please create an issue on GitHub.

Note: in the latest version of the app it’s now possible to configure the company SMTP credentials on Settings > Email Settings.

Hey Hillel

oh my god, this is exactly the feature I’ve always missed in Invoice Ninja! I’ll test it straight away and if it works, I won’t need to use the ENV (1_, 2_, …) at all. That would really be the highlight of the day!

I have now found the function in the settings. Can I now delete the 3 email blocks from my ENV file, or do I still need to have the original SMTP block in the ENV file?

Thank you very much for the quick feedback!

I believe you can remove the extra .env parameters.

i have now removed all email settings from the env file, recreated the docker and emptied the cache.
if i now enter the data in the smtp settings and send a test mail (using the test mail button), the mail is sent to my Invoice Ninja email user, but the sender is hello@example.com.

when i save the smtp settings and create an invoice and want to send it to an email address of mine, it does not arrive, although according to invoice ninja the mail has been sent. so i don’t see any errors, nothing is logged. i assume that the mail is sent as hello@example.com again and therefore does not end up in my mailbox.

now i have reinserted the standard smtp block in the env file and added my main data. but now all mails are sent via this data again, no matter what is set in the smtp settings of the companies. the mails are only sent via this address.

Am I doing something wrong? what do I have to set in the ENV file so that the SMTP settings of the Invoice Ninja application take effect?

@david any ideas where hello@example.com is coming from?

I am also getting the same issue. I also have my email defined in the docker env file. Used to work like a charm!

I went to settings in the IN app and set the mail provider back to Default which I think means it will pickup from the ENV file. I also added to the env 2_MAIL_EHLO_DOMAIN=‘all-tech-plus.com’ to match my email sending domain - I think that’s sorted it, I’ve just resent an invoice and it hasn’t bounced yet. My company ID is 2 btw. @eyer2003

FYI… in the latest version of the app you can configure per company SMTP settings on Settings > Email Settings.

@hillel I actually did that when I saw the setting was available and that’s probably when the issue started - I got the ‘example.com’ issue as reported by op @eyer2003 . I also was getting other delivery issues too, failed to send an old invoice (which shouldn’t be emailed anyhow as it was last year and is paid!).

@david I noticed hello@example.com is the default value if the .env value isn’t set.

1 Like

@hillel

Thats correct. the mailer will also fail completely with blank strings for these two particular values.

Hey!

Thank you for your input. I assume that you also use several companies and several mail servers / mail accounts (because of 2_MAIL…). Do the automatic invoices work normally for you, or are they also sent as hello@example.com? The same with user verification mails, for example.

I have also changed the mail settings in the IN app to default again and specified the mail data in the ENV file. I have also added the variable X_MAIL_EHLO_DOMAIN… but the mails that are sent directly from the system (automatic invoices, user verifications) are still sent from hello@example. I have emptied the cache.

That will probably be the problem. The array should also be able to respond to the individual companies (1_, 2_, …) so that the mails sent by the system are also sent by the correct sender.

Is there a way to do this? Could this code block perhaps be removed and it would work? Would probably be too easy, I guess :wink:

@hillel @eyer2003
Well, still not working, auto sending of invoices still fails with the hello@example.com issue.
I did also down and up the docker container after changes to make sure.

Are you configuring the SMTP values with the .env variables or using the app on Settings > Email Settings?

@hillel @eyer2003
I’ve just done some additional testing - in my settings I have set to ‘default’, the field reply-to-email is set to support@all-tech-plus.com though that used to make no difference since everything is set in the env file.
So I just tried defining MAIL_FROM_ADDRESS (set this slightly different to my normal one) and MAIL_FROM_NAME in the env file before my 2_ environment variables (I don’t have company ID 1 any more!). Sent an email manually and saw picking values from MAIL_FROM_ADDRESS and MAIL_FROM_NAME. Commented out MAIL_FROM_NAME and now I get e-mail from ‘Example’ but using the MAIL_FROM_ADDRESS. Seems to me like the n_ env settings getting ignored?

I suggest trying to configure the SMTP values in the app to see if it helps.

@david should we remove this section from the docs?

What do you use for your smtp server?

Enabling DEBUG mode and Expanded logging might help - Free Source Available Invoicing, Expenses & Time-Tracking | Invoice Ninja

Yes, we’ll need to update the setup page to insert the SMTP details into the company table rather than .env

After that we can update the docs to match