SMTP mail server setup in Ubuntu Server

SMTP mail server setup in Ubuntu Server

Introduction:

In this article, we can discuss how we can set up an SMTP server inside the ubuntu server. The server can be on prime or cloud

step to follow

step first: mail and ssmtp package install

we need to install mail-utils and SMTP packages in the ubuntu server. for that run the below command.

sudo apt-get update -y
sudo apt-get install mailutils
sudo apt-get install --reinstall ssmtp

step second: gmail security setting

for mail services, we are using a Gmail server. so some security settings we need to

1st we need to enable 2-step authentication inside the security setting.

2nd create an app password for access the mail server inside the ubuntu server

an app password is needed to access Gmail from the ubuntu server and its different from the Gmail password that we use to access Gmail to send mail

Go to security setting >> click on app password >>click on other > give name and generate password

save this password in a secure point that we needed to access Gmail in the ubuntu server.

Go to the Gmail setting of your Gmail account click on forwarding and POP/IMAP
check the IMAP setting if its default is disabled. click on enable

step third: Do change the configuration in /etc/ssmtp/ssmtp.conf file

Note:- while doing these changes you need to work as the root user.

cd /etc/ssmtp
vim ssmtp.conffile

>>>>provide your Gmail ID to register.
mailhub=smtp.gmail.com:465 >>> give mail hub details as smtp.gmail.com and add port 465 with it. also, add this port to the security group of your ec2 instance.
if port 465 is not work in your case we can add port 587

AuthUser=test@gmail.com >>>provide your Gmail ID
Authpass=jhwfcffmyhtcxrei >>> give the app password that you created.
UseTLS=YES >>> add this point
UseSTARTLS=Yes \>>> add this point
rewriteDomain=gmail.com >>>> set rewrite domain name as gmail.com
hostname=devloper.northamerica-northeast1-a.c.potent-arcade-379712.internal >\> do not change this this line remain as it is.

#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
root=postmaster
SERVER=test@gmail.com
# The place where the mail goes. The actual machine name is required no 
# MX records are consulted. Commonly mailhosts are named mail.domain.com
mailhub=smtp.gmail.com:465
AuthUser=test@gmail.com
Authpass=jhwfcffmyhtcxrei
UseTLS=YES
UseSTARTLS=Yes

# Where will the mail seem to come from?
#rewriteDomain=
rewriteDomain=gmail.com

# The full hostname
hostname=devloper.northamerica-northeast1-a.c.potent-arcade-379712.internal

# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address

step forth:- To test mail is successfully delivered or not run the below command

run the below syntax to send mail
🚀echo "This is a test email" >>> this stand for mail contents that we want to send.

🚀mail -s "Test Email Subject"

-s stand for subject
>>> stands for person mail id that we wont to send.

echo "This is a test email" | mail -s "Test Email Subject" ram1258@gmail.com