How to sendmail mail server set up with domain on ubuntu server 22.04

author

By Freecoderteam

May 01, 2024

298

image

Setting up a complete mail server with Sendmail or Postfix, including DKIM with OpenDKIM, DNS configurations, firewall settings, SSL/TLS, and integrating everything into a Laravel application involves a complex series of steps. Let's break down each component step-by-step for clarity.

1. Choose Your Mail Server: Sendmail or Postfix

Postfix is recommended due to its ease of configuration and better security compared to Sendmail. However, I'll provide brief instructions for both.

Installing Postfix:

sudo apt update
sudo apt install postfix

During installation, select "Internet Site" and input your domain name.

Installing Sendmail:

sudo apt update
sudo apt install sendmail

2. Install and Configure OpenDKIM

This will allow you to sign outgoing emails with DKIM.

sudo apt install opendkim opendkim-tools

Configure OpenDKIM

Edit /etc/opendkim.conf:

Syslog                  yes
UMask                   002
Socket                  inet:8891@localhost
PidFile                 /var/run/opendkim/opendkim.pid
Mode                    sv
UserID                  opendkim:opendkim
KeyTable                /etc/opendkim/KeyTable
SigningTable            /etc/opendkim/SigningTable
ExternalIgnoreList      /etc/opendkim/TrustedHosts
InternalHosts           /etc/opendkim/TrustedHosts

Set up KeyTable and SigningTable

Create or edit /etc/opendkim/KeyTable:

mail._domainkey.yourdomain.com yourdomain.com:mail:/etc/opendkim/keys/yourdomain.com/mail.private

Create or edit /etc/opendkim/SigningTable:

*@yourdomain.com mail._domainkey.yourdomain.com

Generate Keys

opendkim-genkey -t -s mail -d yourdomain.com -v
sudo mkdir -p /etc/opendkim/keys/yourdomain.com
sudo mv mail.private mail.txt /etc/opendkim/keys/yourdomain.com/

3. Update DNS Settings

Add the DKIM record (from mail.txt) and SPF record to your domain's DNS:

  • DKIM: mail._domainkey IN TXT "v=DKIM1; k=rsa; p=MIGfMA0G..."
  • SPF: v=spf1 ip4:<Your_Server_IP> -all

4. Configure SSL/TLS for Secure Email Transmission

Install certificates (you can use Let's Encrypt for free certificates):

sudo apt install certbot
sudo certbot certonly --standalone -d yourdomain.com

Configure Postfix to use SSL:

Edit /etc/postfix/main.cf:

smtpd_tls_cert_file=/etc/letsencrypt/live/yourdomain.com/fullchain.pem
smtpd_tls_key_file=/etc/letsencrypt/live/yourdomain.com/privkey.pem
smtpd_use_tls=yes

Restart Postfix:

sudo systemctl restart postfix

5. Firewall Configuration

Allow SMTP and HTTPS traffic:

sudo ufw allow 25
sudo ufw allow 443

6. Laravel Integration

Install the necessary package for mail handling in Laravel:

composer require guzzlehttp/guzzle

Configure .env in Laravel for SMTP settings:

MAIL_MAILER=smtp
MAIL_HOST=yourdomain.com
MAIL_PORT=587
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS='example@yourdomain.com'
MAIL_FROM_NAME="${APP_NAME}"

7. Test Your Configuration

Send a test email from Laravel using a simple route or command to ensure everything is configured correctly.

Final Notes

This guide covers a very complex setup, and each step should be tested thoroughly before proceeding to the next. Adjustments may be needed based on your specific server environment, domain settings, or Laravel version. If you run into specific errors during setup, each error must be addressed individually.

Share this post :

Subscribe to Receive Future Updates

Stay informed about our latest updates, services, and special offers. Subscribe now to receive valuable insights and news directly to your inbox.

No spam guaranteed, So please don’t send any spam mail.