We've embarked on a project to develop modules for managing emails in Odoo. It's a complex task, but we're confident it's achievable. Our motivation stems from the desire to gain more control over our email system and keep costs to a minimum. As such, we've decided to transition some of our email addresses from Gmail to Mailcow. This move requires significant effort but offers numerous benefits:
- We can manage blacklists in Odoo, adding spammers to our Mailcow blacklist.
- New email addresses in Mailcow are automatically created when a new user is added in Odoo.
- Creating aliases in Odoo also creates them in Mailcow.
However, we're currently unable to automate the addition of new emails to the list of emails relayed to Mailcow. So, even if we don't create a Google Workspace user immediately, we'll need to manually add the email to the list of emails routed out of Gmail.
This process involves using external email servers, which can be challenging to set up in a staging environment. The external server continues to receive emails, causing a conflict between the staging and production environments. To resolve this, we've developed a module that checks if we're on the production server and blocks the cron job that retrieves emails. This solution effectively ends the email conflict between the two servers. However, it raises a new question: how do we now test our email module?
One option is to add features to the module that allow it to process mail. However, my preferred approach is to set up a new server for testing and then simply remove the mail fail-safe module. This method is straightforward, but setting up a test server on a test domain is not. It involves significant work on the DNS side of your domain. I'll outline how to do this with my preferred mail server, Mailcow (dockerized).
I already have a Mailcow server running on a Linux VM launched on my Proxmox server. You can host your VM anywhere (OVH, Google, AWS, Azure), or you can use this guide for other mail servers. To proceed, you'll need a Staging or Dev Odoo and an external mail server.
Creating a subdomain and setting up DNS
You'll need to create several records on your DNS server (or your domain registrar's server if you haven't changed it). In this example, we'll create a subdomain (https://testmail.bemade.org%29./ We'll start with an MX record for the subdomain, using the same server as our main domain, i.e., https://mail.bemade.org/ (you may need to create an A record for https://mail.bemade.org/ if you haven't already).
MX testmail https://mail.bemade.org/ 10
This is sufficient for receiving emails. However, to send emails, we need to add more records to authenticate our emails correctly and prevent them from being marked as spam or disappearing. Next, we need a Sender Policy Frame TXT record, which will usually have the same value as your main domain. Be aware that you'll need to adjust this record if you plan to use other servers to send mail from your domain name.
TXT testmail v=spf1 mx https://ip4:WWW.XXX.YYY.ZZZ/ ~all
Replace https://www.xxx.yyy.zzz/ with your mail server IP address. If your server signs all outgoing emails with DKIM, as Mailcow does, you'll also need to add a TXT record with DKIM keys. Your mail server will provide the value for this record.
TXT dkim._domainkey.testmail v=DKIM1;k=rsa;t=s;s=email;p=......
These are the minimum records required for secure and serious use. If you're using Mailcow, you'll find all this information (and more record recommendations) in the web interface under domain configuration. We added https://testmail.bemade.org/ to the domains under Email configuration in Mailcow. We only filled in the domain name field, saved it, and followed the DNS buttons to the right of the newly created domain.
Now that we have a domain, we need to create our first mailbox. In the interface, switch to Email Configuration / Mailbox and add one by filling in the left part of the email (named odoo in my case), select the domain, add a full name, and enter a password twice. To use it as the email used by Odoo to send all messages, you need to change the Allow From field to deactivate sender validation for all domains or just your test domain. Then press the create button.
Next, we'll test it from the SoGo interface (add /sogo to the URL of your Mailcow server or follow one of the links in the administration interface or on the main login screen of Mailcow, click on Webmail). On the SoGo login, enter the newly created email with the matching password. First, we'll test sending using https://mail-tester.com/ (a freemium service that evaluates email deliverability). Navigate to their website, which will provide an email address to write to and then provide a complete report. So, in SoGo, send an email to that address. Then go back to https://mail-tester.com%2C/ read the report, and make changes accordingly. If you want a chance to score 10/10 and not lose points in SpamAssassin, paste a real email into the content of the test email. If you score 8 or above without content, everything should be okay, not just for testing, but for production purposes.
Next, from another server, send an email to your newly created email address and wait a minute or two before checking in SoGo for the email in your inbox. If your MX record is okay and the created domain and email address match, everything should work out of the box.
Configuring Odoo to the mail server
If you've set up Google or Microsoft on your Odoo as an external email server, then you'll need to add a module, which I won't cover here. But except for two-factor authentication, the rest of the configuration should be similar. In the Odoo configuration setup, check Custom Email Servers and enter your test domain in the Alias Domain field (https://testmail.bemade.org%29./ Then click on Incoming Email Servers and create a new one.
The name of this email server is just for information, so use whatever you want. Even though Mailcow supports both POP and IMAP, we'll select IMAP as the Server type. Enter your mail server address in the Server Name fields, check SSL/TLS, and set the port to 993. Then enter the email address created earlier as the Username with the corresponding Password, then click confirm and save.
For the Outgoing Server, do the same, setting Connection Security to TLS (STARTTLS) and Authenticate with user. Be sure to set the port to 587, with the same server name, username, and password from the previous steps. Test the connection, then save.
Congratulations, you're done! You now have a subdomain (or a full domain) ready to use with an external server, allowing you to test and develop mail-related modules in Odoo.