Postfix Set up on ubuntu to send emails

This article explains, how to set up postfix to send emails on ubuntu system

Hello all .Today we will see how to setup postfix for email sending on ubuntu.

What is Postfix?

    Postfix is a free and open source Mail Transfer Agent(MTA) which routes and delivers electronic mail. In short which handles delivery of emails and also routing of emails.

Prerequisites:

  • A valid domain name (you can purchase from godaddy, bigrock, etc)
  • Instance (server from aws(ec2) or Digital Ocean or any other services)
  • See my post about domain name here

How to Install ?

For ubuntu/debian based systems 

sudo apt-get install postfix

Steps or important things to configure postfix

General type of mail configuration: Internet Site

System mail name:
mydomain.com # use your domain name here

Root and postmaster mail recipient:
bala # give your system user name so that it will receive mails

Other destinations to accept mail for (blank for none):
$myhostname, mydomain.com, mail.mydomain.com,

Force synchronous updates on mail queue? No

Local networks: 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128

# Postfix listens on local host so that outside system can't access. very important for avoiding others spamming our system

Mailbox size limit (bytes): 0 # No limit for Mailbox size

Local address extension character: +

Internet protocols to use: all

Making changes to config or Reconfigure by

 $ sudo dpkg-reconfigure postfix

It will repeat the above steps for reconfiguration.

Most Important things for Inbox delivery of emails

  • Valid `SPF` certficate for your domain
  • Valid `DKIM` certificate for your domain
  • DMARC` Certificate for your domain
  • Server `Ip` should be `whitlisted` if it's blacklisted

Reference Links for SPF,DKIM:

  • For SPF refer here 
  • For DKIM you can refer here 
  • Other useful link

Few Postfix Commands

Check postfix service running/stopped by

sudo systemctl status postfix.service

Start or restart Service by

sudo service postfix restart/stop/start

Check mail queue length by

mailq

postqueue -p

To check Postfix verision  postconf mail_version 

Process the queue by 

postqueue -f 

Read Mail from queue by

postcat -q MAIL_ID # here change Mail_id to id comes by mailq command

To remove all mails in the deferred queue

postsuper -d ALL deferred

Check the Postfix logs by 

tail -f /var/log/mail.log

Now, we have postfix configured and necessary things done for email setup. 

Let's see how we can send email using python's smtplib (in built module).

save below code as  send_email.py

import smtplib

SERVER = "localhost"
FROM = "root@mydomain.com"
TO = ["user@anotherdomain.com"] # must be a list

SUBJECT = "Hello!"

TEXT = "This message was sent with Python's smtplib using postfix"

# Prepare actual message
message = """\
From: %s
To: %s
Subject: %s
%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)

# Send the mail

server = smtplib.SMTP(SERVER)
server.sendmail(FROM, TO, message)
server.quit()

Run this by python send_email.py

Note:  change your from and to email address in script . Assuming postfix runs on your same system as script.

That's it. Check the inbox or spam for our email . Hope this helps.

Thanks for reading. Happy coding !!!

Leave a comment

(Note: Comments are moderated)