I finally set up my own mail server using a couple guides. Just putting this here for my memory


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)


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

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


By defaut 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
sender_bcc_maps = mysql:/etc/postfix/sql/mysql-virtual-bcc-maps.cf

/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’

/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
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.
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 mailgun if you want

go to mailgun.com and create an account

Hit sending on the left hand side

then hit new domain

Do something like mailgun.site.com

Then add 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 = smtp.mailgun.org
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = static:[email protected]: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.