Repository: inventree/InvenTree

Issue:
Email settings not configured
### Deployment Method

- [ ] Installer
- [ ] Docker Development
- [X] Docker Production
- [ ] Bare metal Development
- [ ] Bare metal Production
- [ ] Digital Ocean image
- [ ] Other (please provide a link `Steps to Reproduce`

### Describe the problem*

Hello,

I am having issues configuring the email settings to connect to our on-prem exchange server. 

I have configured the .env file with the following variables (Note - exchange does not require authentication internally)

INVENTREE_EMAIL_HOST
INVENTREE_EMAIL_PORT
INVENTREE_EMAIL_SENDER

However, when running the docker container, the 'System Information' screen reports that 'Email settings not configured. Under 'Global Settings' > 'Login Settings' a prompt shows at the top saying 'Outgoing email has not been configured. Some login and sign-up features may not work correctly!'

Apologies if I have missed something obvious in the documentation but I seem to be going round in circles currently. Any help that you could offer would be greatly appreciated. Thank you

### Steps to Reproduce

1. Stop the contain with `docker compose down` 
2. Edit .env file to include the email variables (as per https://docs.inventree.org/en/0.10.0/start/config/#email-settings)
3. Starting the container with `docker compose up -d`

Further steps (Apologies, these might be all guesses at this stage)

1.  I have tried setting these within the configuration file (I believe saved here: `volume/inventree-data/config.yaml` to include host, port, sender, tls and ssl variables, however with no change.
2. Feeling like I am missing a step, I am running `docker compose run inventree-server invoke update`
3. I am now running 0.10.1 but with the same issue.



### Relevant log output

_No response_


Discussion:
hi there @phil-ittech !
Have you checked if you can ping the Exchange server from within the docker container? Sometimes DNS resolving does not work with windows networks + docker.
From past experience with Exchange, it might be required to pass an empty username/password combination and set email_sender.
Also, NTML/Kerberos authentication will not work as your container is (probably) not joined to your domain.
@phil-ittech as per this function:

https://github.com/inventree/InvenTree/blob/93a2d612e2d2f8581e083f62cc84fe8b65f0c275/InvenTree/InvenTree/status.py#L44

You will need to also specify the username `EMAIL_HOST_USER` and password `EMAIL_HOST_PASSWORD`
Hello @matmair. Thank you for your reply, really appreciate it.

I have checked and I can ping the exchange server from the docker host and from the container directly. I can also telnet to the server on port 25.

I have added the following variables to the .env file, restarted the containers but with no luck.

INVENTREE_EMAIL_USERNAME
INVENTREE_EMAIL_PASSWORD
INVENTREE_EMAIL_TLS

A few of questons if I may:

- Should I be adding the email settings within the .env or the config.yaml file? 
- Does one of these files take priority?
- Using the .env, is there any specific formatting required e.g. INVENTREE_EMAIL_HOST=hostname.domain.com Or is this better to put within quotes of apostrophes?
- How should I apply these settings - is saving the file, taking the container down and back up again enough?
-  I found the status.py script within the console of the docker container (as per this discussion https://github.com/inventree/InvenTree/discussions/2823) and as just suggested by @SchrodingersGat (Thank you for talking the time to reply) However running the command `python3 status.py` from within the container returns:

`root@f348c05495ce:/home/inventree/InvenTree/InvenTree# python3 status.py
Traceback (most recent call last):
  File "/home/inventree/InvenTree/InvenTree/status.py", line 11, in <module>
    from django_q.models import Success
  File "/root/.local/lib/python3.9/site-packages/django_q/models.py", line 16, in <module>
    from django_q.conf import croniter
  File "/root/.local/lib/python3.9/site-packages/django_q/conf.py", line 31, in <module>
    class Conf:
  File "/root/.local/lib/python3.9/site-packages/django_q/conf.py", line 37, in Conf
    conf = settings.Q_CLUSTER
  File "/root/.local/lib/python3.9/site-packages/django/conf/__init__.py", line 82, in __getattr__
    self._setup(name)
  File "/root/.local/lib/python3.9/site-packages/django/conf/__init__.py", line 63, in _setup
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting Q_CLUSTER, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
`
 
 Does the above give any clues please?
 
@SchrodingersGat  - With regards to 'EMAIL_HOST_USER' and  'EMAIL_HOST_PASSWORD'  - is this in addition to INVENTREE_EMAIL_USERNAME and INVENTREE_EMAIL_PASSWORD please?

Many Thanks in advance.
You have probably mis-configured your settings file, based on that error. Can you share a sanitized version here? Remove or redact any sensitive info.
The order is enviroment variable -> .env -> config.yaml -> database
Thank you both for your replies and help. 

@SchrodingersGat - here are my .env and config.yaml files

[InvenTree env file](https://github.com/inventree/InvenTree/files/10929528/InvenTree.env.file.txt)

[InvenTree config yaml file.txt](https://github.com/inventree/InvenTree/files/10929592/InvenTree.config.yaml.file.txt)

Thank you in advance

Comprehensive docs for email setup are here - https://docs.inventree.org/en/latest/start/config/#email-settings

Unless you have removed the username and password values, they are empty in both your `.env` and `config.yaml` files (which is a problem)
Hi @SchrodingersGat. Thanks the reply.

Both username and password values have been left blank on purpose as InvenTree will be connecting to our exchange server on an internal receive connector as 'Anonymous users' (as this is internal use we don't require authentication). 

Are you saying however that these variables NEED to be populated please? 

I will populate these now and report back.

Thank you
Yes as per my link above in the `is_email_configured` method, these fields must be filled.. Might be a bug, as it doesn't allow for anon logins
I have now authenticated with a domain user account and the 'Email Settings not configured' message has now gone from 'System Information'. Perfect.

However, I am now seeing SMTP authentication Server Error with the following data output

```
Traceback (most recent call last):

File "/home/inventree/InvenTree/InvenTree/forms.py", line 277, in send_mail
result = super().send_mail(template_prefix, email, context)

File "/root/.local/lib/python3.9/site-packages/allauth/account/adapter.py", line 140, in send_mail
msg.send()

File "/root/.local/lib/python3.9/site-packages/django/core/mail/message.py", line 284, in send
return self.get_connection(fail_silently).send_messages([self])

File "/root/.local/lib/python3.9/site-packages/django/core/mail/backends/smtp.py", line 102, in send_messages
new_conn_created = self.open()

File "/root/.local/lib/python3.9/site-packages/django/core/mail/backends/smtp.py", line 69, in open
self.connection.login(self.username, self.password)

File "/usr/local/lib/python3.9/smtplib.py", line 750, in login
raise last_exception

File "/usr/local/lib/python3.9/smtplib.py", line 739, in login
(code, resp) = self.auth(

File "/usr/local/lib/python3.9/smtplib.py", line 662, in auth
raise SMTPAuthenticationError(code, resp)

smtplib.SMTPAuthenticationError: (535, b'5.7.3 Authentication unsuccessful')
```


I will go off and troubleshoot this issue (as its likely to be something on my email server side) and report back as soon as I can to confirm what is going on here.

Thanks for your help with this.

I never even thought of anon mail servers, I have not had contact with non-cloud mail servers for years. @SchrodingersGat it might make sense to allow that. If I make a PR do you think we should merge?
To confirm, the SMTP authentication error was related to an incorrect receive connector on the exchange server that was not correctly setup to accept from 'Exchange Users' (i.e. authenticated users) from an internal source.

Email is all working now from Inventree.

Thanks to both of you @SchrodingersGat  @matmair for your swift replies and help.

Looking forward to using this system. Great work guys!
Great to hear, can we close this?
Yes of course. Thanks!
> I never even thought of anon mail servers, I have not had contact with non-cloud mail servers for years. @SchrodingersGat it might make sense to allow that. If I make a PR do you think we should merge?

I don't think it hurts to allow for this option. I'd be happy to merge

Resolving patch:
diff --git a/InvenTree/InvenTree/status.py b/InvenTree/InvenTree/status.py
--- a/InvenTree/InvenTree/status.py
+++ b/InvenTree/InvenTree/status.py
@@ -61,19 +61,13 @@ def is_email_configured():
         if not settings.TESTING:  # pragma: no cover
             logger.debug("EMAIL_HOST is not configured")
 
-    if not settings.EMAIL_HOST_USER:
-        configured = False
-
-        # Display warning unless in test mode
-        if not settings.TESTING:  # pragma: no cover
-            logger.debug("EMAIL_HOST_USER is not configured")
+    # Display warning unless in test mode
+    if not settings.TESTING:  # pragma: no cover
+        logger.debug("EMAIL_HOST_USER is not configured")
 
-    if not settings.EMAIL_HOST_PASSWORD:
-        configured = False
-
-        # Display warning unless in test mode
-        if not settings.TESTING:  # pragma: no cover
-            logger.debug("EMAIL_HOST_PASSWORD is not configured")
+    # Display warning unless in test mode
+    if not settings.TESTING:  # pragma: no cover
+        logger.debug("EMAIL_HOST_PASSWORD is not configured")
 
     return configured
 


Test patch: