I finally set up my own mail server using a couple guides. Just putting this here for my memory. Thank you to the great people who write amazing blogs.


How to set up the server


When put behind cloudflare you need to add the bellow to /etc/nginx/nginx.conf in the http { location

    
    # CF set real ip
    set_real_ip_from 103.21.244.0/22;
    set_real_ip_from 103.22.200.0/22;
    set_real_ip_from 103.31.4.0/22;
    set_real_ip_from 104.16.0.0/12;
    set_real_ip_from 108.162.192.0/18;
    set_real_ip_from 131.0.72.0/22;
    set_real_ip_from 141.101.64.0/18;
    set_real_ip_from 162.158.0.0/15;
    set_real_ip_from 172.64.0.0/13;
    set_real_ip_from 173.245.48.0/20;
    set_real_ip_from 188.114.96.0/20;
    set_real_ip_from 190.93.240.0/20;
    set_real_ip_from 197.234.240.0/22;
    set_real_ip_from 198.41.128.0/17;
    set_real_ip_from 2400:cb00::/32;
    set_real_ip_from 2606:4700::/32;
    set_real_ip_from 2803:f800::/32;
    set_real_ip_from 2405:b500::/32;
    set_real_ip_from 2405:8100::/32;
    set_real_ip_from 2c0f:f248::/32;
    set_real_ip_from 2a06:98c0::/29;

    # use any of the following two
    real_ip_header CF-Connecting-IP;
    #real_ip_header X-Forwarded-For

Enable PHP Sockets


How to install and set up roundcube


Allow roundcube and dovecot to see real IP. This also works when it’s behind cloudflare.
https://github.com/roundcube/roundcubemail/issues/5336


Default to html editor

Edit /usr/share/nginx/html/dir/config/defaults.inc.php

edit $config[‘htmleditor’] = 0; to $config[‘htmleditor’] = 4;


This is only needed if redis won’t install using a package manager

How to install and set up redis


CLI administration

CLI administration (My Fork)

I have a new fork on my own gitea server, but I need to clean it up so I can share it.


Multi Accounts

Then you have to run the mysql init in the sql folder

mysql -u dbUser -ppassword -D dbName < plugins/ident_switch/sql/mysql.initial.sql


Couldn’t get working

Now it’s time to set up z-push. This will make dovecot work with ActiveSync for quicker updates.
https://wiki.z-hub.io/display/ZP/Installation


Add Keep me logged in button

THIS DOESN’T SEEM TO WORK WELL. I’VE STARTED USING RAINLOOP INSTEAD

https://github.com/mfreiholz/persistent_login

Now we need to create the database

mysql -u dbUser -ppassword -D dbName < plugins/persistent_login/sql/mysql.sql

Now we need to enable the sql usage. Change $rcmail_config[‘ifpl_use_auth_tokens’] = false; to $rcmail_config[‘ifpl_use_auth_tokens’] = true;

nano plugins/persistent_login/config.inc.php.dist


Add mobile interface

https://github.com/messagerie-melanie2/Roundcube-Plugin-Mobile


INSTALL RAINLOOP INSTEAD. It’s pretty easy

Install Rainloop


By default I noticed that postfix doesn’t save sent emails in the sent folder if sent with postfix. This is the mail reason I set up a mail server so all of my app could send out. I really wanted a way to keep track of what was sent out. The below is how I got it to work. The only downside is since roundcube/IMAP clients auto save email in the sent folder so if you send something from roundcube it will be in the sent folder twice. 

Add the below to the end of the file /etc/postfix/main.cf
nano /etc/postfix/main.cf

sender_bcc_maps = mysql:/etc/postfix/sql/mysql-virtual-bcc-maps.cf

nano /etc/postfix/sql/mysql-virtual-bcc-maps.cf

user = vmail
password = password
hosts = 127.0.0.1
dbname = vmail
query = SELECT CONCAT_WS('[email protected]', username, domain) AS destination from accounts where username = '%u' and domain = '%d'

nano /var/vmail/sieve/global/bcc_to_sent.sieve

require ["fileinto", "mailbox", "envelope", "subaddress","imap4flags"];
if envelope :detail "to" "smtp_sent" {
addflag "\\Seen";
fileinto :create "Sent";
stop;
}

Then had to add the below to /etc/dovecot/dovecot.conf in the plug-ins section after the line that starts with sieve_before
nano /etc/dovecot/dovecot.conf

sieve_before2 = file:/var/vmail/sieve/global/bcc_to_sent.sieve

Now we want to add an additional column in the table that way we can disable auto save to sent if we know we’re going to use a normal IMAP client. This will default to zero. 
mysql -u root -p
use vmail
ALTER TABLE accounts ADD COLUMN autosent TINYINT NOT NULL DEFAULT '0';

If you want to make it a one so it will save it all you have to do is run the below sql. I’m working on changing the bash script that I use to create accounts and add the ability to edit this column. THE BASH SCRIPT HAS BEEN UPDATED TO SUPPORT THIS, BUT THE INIT IN THE SCRIPT DOESN’T INIT THE COLUMN SO YOU HAVE TO DO IT MANUALLY.
UPDATE accounts SET autosent=1 WHERE username = ‘username’ AND domain = ‘domain’;


Now it’s time to add filters for auto mail filtering

nano /usr/share/nginx/html/site.com/config/config.inc.php

add ‘managesieve’, to the end of plugins in the config file. Nothing else should need to be configured it should just work.


Time to relay thru AmazonSES if you want

go to console.aws.amazon.com and create an account

Search for Simple Email Service

Go to domains

Then “Verify New Domain” YOU WILL NEED DNS ACCESS FOR EACH DOMAIN YOU WANT TO ADD

Then update your DNS with the info they provide

Now we need to edit the main config file

nano /etc/postfix/main.cf

Add the following to the above file

[email protected] be replaced with what you chose and replace password with the password

relayhost = email-smtp.yourRegion.amazonaws.com:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = static:USER:PASSWORD
smtp_sasl_security_options = noanonymous

Now restart postfix with systemctl restart postfix

Now test send an email you should be able to see that it was sent by mailgun or mailgun.site.com

It looks like with this config postfix will still deliver local email using LTMP.