How to make mail server in ubuntu

Setting up a mail server in Ubuntu can be a bit complex, but I'll guide you through the process. We'll use Postfix, Dovecot, and MySQL to create a simple mail server.

Prerequisites:

  1. Ubuntu 18.04 or later (other versions may work, but this guide is specific to 18.04)
  2. A static IP address or a dynamic DNS (DDNS) service
  3. A domain name (e.g., example.com)
  4. A MySQL database (we'll use the default mysql package)

Step 1: Install required packages

Open a terminal and run the following commands:

sudo apt update
sudo apt install postfix dovecot-imapd dovecot-pop3d mysql-server

Step 2: Configure Postfix

Edit the Postfix configuration file:

sudo nano /etc/postfix/main.cf

Add the following lines at the end of the file:

mydestination = example.com
myhostname = mail.example.com
myorigin = $myhostname

Replace example.com with your domain name.

Step 3: Configure Dovecot

Edit the Dovecot configuration file:

sudo nano /etc/dovecot/dovecot.conf

Add the following lines at the end of the file:

mail_location = maildir:~/Maildir
first_valid_uid = 1000
first_valid_gid = 1000

Step 4: Create a MySQL database and user

Create a new MySQL database and user:

sudo mysql -u root -p

Create a new database:

CREATE DATABASE mail;

Create a new user:

CREATE USER 'mailuser'@'localhost' IDENTIFIED BY 'password';

Grant privileges to the user:

GRANT ALL PRIVILEGES ON mail.* TO 'mailuser'@'localhost';

Step 5: Configure Postfix to use MySQL

Edit the Postfix configuration file:

sudo nano /etc/postfix/mysql.cf

Add the following lines:

hosts = localhost
user = mailuser
password = password
dbname = mail

Step 6: Restart Postfix and Dovecot services

Restart the Postfix and Dovecot services:

sudo service postfix restart
sudo service dovecot restart

Step 7: Configure DNS

You need to configure your DNS settings to point to your mail server. You can do this by creating a TXT record for your domain:

sudo nano /etc/postfix/main.cf

Add the following line:

mx_record = yes

Then, create a TXT record for your domain:

sudo nano /etc/bind/named.conf.local

Add the following lines:

zone "example.com" {
  type master;
  file "/etc/bind/db.example.com";
};

zone "example.com" {
  type slave;
  masters { 8.8.8.8; };
};

Replace example.com with your domain name.

Step 8: Test your mail server

You can test your mail server by sending an email to your domain:

sudo mail -s "Test email" [email protected]

Replace user with the username you want to send the email to.

That's it! You now have a basic mail server set up in Ubuntu. You can use tools like telnet or msmtp to test your mail server.

Note: This is a basic setup, and you may want to add additional security measures, such as SSL/TLS encryption, to your mail server.