This post was made to illustrate howto send emails via postfix mail server using Gmail. Previously I was using sendmail with gmail in combination but few times sendmail made troubles in new installation, therefore i switched to postfix which is quite simple as compared to sendmail complexity. I have tested it at various networks and so far found it reliable.
It can be used by other mail applications like mail utility, radius manager web bulk mail function, or any other you like. We will use GMAIL as relay to send our emails using our gmail account. You need functional gmail account for this purpose, and make sure ‘allow less secure application’ is turned on to availe this function.
TIP:
We can also use this as centralized email server gateway so that all of our devices on the LAN like Mikrotik Router, Cisco Switches, Mobile Devices and others can send via this email gateway so that we can get rid of configuring email services at each system separately.
That’s why in some specific situation, I say “Work Smarter, Not Harder” / Za!b
Software Used:
OS : Ubuntu 12.4 / 32bit
Email Server : POSTFIX 2.9.6
Let’s Start …
First we need to update ubuntu apt-get and then install the postfix mail server application
Step#1
apt-get update && apt-get -y install postfix mailutils libsasl2-modules
- When prompted for “General type of mail configuration” choose Internet Site.
- When prompted for a “Mail name,” you can use default name.
As showed in the images below …
Once above installation is done,
Create and edit new file which will store the Gmail ID and Password
touch /etc/mailname # Change the radius.local to match your hostname FQDN name echo "pcname.local" > /etc/mailname # create File which will contain user id and password touch /etc/postfix/sasl_passwd nano /etc/postfix/sasl_passwd
and paste following [Make sure you replace YOURGMAILID:YOURPASS with valid gmail credentials.
[smtp.gmail.com]:587 YOURGMAILID@gmail.com:YOURPASS
Save & Exit.
Step#2
Now Make it accessible for root
chmod 600 /etc/postfix/sasl_passwd
Step#3
Edit postfix main configuration File by
nano /etc/postfix/main.cf
Remove all previous lines and paste following ….
#Postfix main configuration file / Syed Jahanzaib / aacable at hotmail dot com / http:// aacable . wordpress . com smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu) biff = no append_dot_mydomain = no readme_directory = no smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key smtpd_use_tls=yes smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache alias_maps = hash:/etc/aliases alias_database = hash:/etc/aliases myorigin = /etc/mailname #################################################### # MAKE SURE YOU CHANGE THIS SECTION TO YOUR LOCAL myhostname = PCNAME #radius.localhost # change following with your local PC NAME mydestination = PCNAME #pcname.localhost, localhost.localhost, , localhost # Relay hosts entries as below, ADD YOUR IP SERIES HERE mynetworks = 127.0.0.0/8 10.0.0.0/8 192.168.0.0/16 ####################################################### mailbox_command = procmail -a "$EXTENSION" mailbox_size_limit = 0 recipient_delimiter = + inet_interfaces = all inet_protocols = all relayhost = [smtp.gmail.com]:587 smtp_use_tls = yes smtp_sasl_auth_enable = yes smtp_sasl_security_options = smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
Save & Exit.
Step#4
Use postmap command to compile and hash the contents of sasl_passwd. The results will be stored in your Postfix configuration directory in the file sasl_passwd.db.
postmap /etc/postfix/sasl_passwd
Step#5
Create folder to hold mails
mkfifo /var/spool/postfix/public/pickup
: If above folder already exists, ignore and continue
Step#6
Change the FROM address. It will be displayed in user inbox from section.
chfn -f 'YOUR COMPANY NAME' root
Step#7
Make sure you have Enable “Less Secure Apps” In Gmail
https://www.google.com/settings/security/lesssecureapps
Step#8 [ required only if you have SENDMAIL installed previously, probably you dont need this to do in new installation. This was a point in which I got stuck for hour]
IF sendmail was previously installed, then remove it and stop its service
apt-get remove sendmail service sendmail stop
Step#9
Finally Restart POSTFIX service
sudo /etc/init.d/postfix restart
FINAL Step#10 / TESTiNG the Ride !
Now try to send email by using command in the terminal, change the email address to your email address
mail -s "Test subject from postfix by Syed.Jahanzaib" aacable@hotmail.com
After this it will ask cc: just press enter
it will show blank cursor where you can type the email body, type it any text or leave it blank , then press press CTRL+D to finally send the email
Now at the same time in other window, Check mail log for any error
tail -f /var/log/mail.log
Result:
TIPS:
DISABLE IPV6 BEFORE PROCEEDING
I had some bad experience with the IPV6 enabled in my Ubuntu box. So if you are having trouble, try to disable IPV6 and remove its entries from hosts files as well.
Blacklist, to block sending email to specific domain/users
http://www.linuxmail.info/postfix-restrict-sender-recipient/
OR DO FOLLOWING
Error:
Jun 7 11:04:39 radius postfix/smtp[652]: connect to smtp.gmail.com[2a00:1450:400c:c07::6d]:587: Network is unreachable
This means your MTA is trying to connect to the Google SMTP via IPv6. and if your network doesnt support ipv6, you will get error.
To get rid of the error message, you can configure Postfix not to use IPv6 by editing your /etc/postfix/main.cf
with this directive in the end:
inet_protocols = ipv4
Afterwards you’ll have to restart postfix:
/etc/init.d/postfix restart
STOP CRON related mail notificaitons
add this in your cronjob
> /dev/null 2>&1
Example: if you have some script running every few minutes use following
# Monitor Data Center Temperature every minute and send email/sms alerts */1 * * * * /temp/noctempr.sh > /dev/null 2>&1
Regards
Syed Jahanzaib