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.
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.
For ubuntu/debian based systems
sudo apt-get install 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
$ sudo dpkg-reconfigure postfix
It will repeat the above steps for reconfiguration.
sudo systemctl status postfix.service
sudo service postfix restart/stop/start
To check Postfix verision
postcat -q MAIL_ID # here change Mail_id to id comes by mailq command
postsuper -d ALL deferred
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
SERVER = "localhost"
FROM = "firstname.lastname@example.org"
TO = ["email@example.com"] # must be a list
SUBJECT = "Hello!"
TEXT = "This message was sent with Python's smtplib using postfix"
# Prepare actual message
message = """\
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
# Send the mail
server = smtplib.SMTP(SERVER)
server.sendmail(FROM, TO, message)
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 !!!