Sunday, March 24, 2013

Using Gmail SMTP to send email in FreeBSD by Postfix

gmail_logo-e1354684621248Using Gmail SMTP to send email in FreeBSD by Postfix

This is the best solution for servers where their configuration does not allow you to send emails directly. For example it can be a development server where you develop applications and this server only has Internet connection without white IP-address

Install Postfix
cd /usr/ports/mail/postfix && make install

Then a config menu appears where you need to choose the following options: PCRE, TLS, SASL2. For dependent packages you may leave default config options.

But, be attentive at the end of installation, the installer will ask you:
Would you like to activate Postfix in /etc/mail/mailer.conf [n]

You have to answer "y".

To enable postfix startup script please add this line to /etc/rc.conf:
postfix_enable="YES"


Disable default sendmail


Then turn off sendmail by adding lines in /etc/rc.conf:
sendmail_enable="NO"
sendmail_submit_enable="NO"
sendmail_outbound_enable="NO"
sendmail_msp_queue_enable="NO"

And you should also disable some sendmail specific daily maintenance routines in your
/etc/periodic.conf file:
daily_clean_hoststat_enable="NO"
daily_status_mail_rejects_enable="NO"
daily_status_include_submit_mailq="NO"
daily_submit_queuerun="NO"

If /etc/periodic.conf does not exist please create it and add those values.


Configure Postfix


Add the following lines to the end of /usr/local/etc/postfix/main.cf file:
relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/usr/local/etc/postfix/sasl_passwd
smtp_sasl_security_options =
smtp_use_tls = yes

Create a file /usr/local/etc/postfix/sasl_passwd by command:
cd /usr/local/etc/postfix
touch sasl_passwd
chmod 400 sasl_passwd

Add the following line to the end of /usr/local/etc/postfix/sasl_passwd file:
[smtp.gmail.com]:587 yourmail@gmail.com:password

Where:

  • "yourmail@gmail.com" is your Gmail account

  • "password" is password for your Gmail account


Then reboot system:
reboot

Now, hash the password by the following commands:
cd /usr/local/etc/postfix
postmap sasl_passwd

Move all root's emails to email address, add this line to file /etc/mail/aliases:
root:    adminmail@gmail.com

Where: "adminmail@gmail.com" is an email address of server admin (perhaps yours).

Create a forwarding database from /etc/aliases by command:
newaliases

To check if a redirect works, run command:
postmap -q root hash:/etc/aliases

And then you have to see the email address of server admin like this: "adminmail@gmail.com".


Test email for sending


Create a txt file "youtestmessage.txt" with the following text:
To: yourmail@gmail.com
From: yourmail@gmail.com
Subject: Testmessage
This is a test for sending

Where "yourmail@gmail.com" is your current or second email to get test email and make sure that Postfix sends the email. And run command:
sendmail yourmail@gmail.com < youtestmessage.txt

After this you have to get email on yourmail@gmail.com, and it means that it work correctly.

The last step is the sending emails for root. This is very important because FreeBSD daily sends reports about system to root's email. So run, the following command to check it:
sendmail -bv root

After this you have to get the email on "adminmail@gmail.com".


In addition


Gmail has one specific thing which has been described in previous article.

No comments:

Post a Comment