Using 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.
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:
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
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.
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:
- "firstname.lastname@example.org" is your Gmail account
- "password" is password for your Gmail account
Then reboot system:
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:
Where: "email@example.com" is an email address of server admin (perhaps yours).
Create a forwarding database from /etc/aliases by command:
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: "firstname.lastname@example.org".
Test email for sending
Create a txt file "youtestmessage.txt" with the following text:
To: email@example.com From: firstname.lastname@example.org Subject: Testmessage This is a test for sending
Where "email@example.com" is your current or second email to get test email and make sure that Postfix sends the email. And run command:
sendmail firstname.lastname@example.org < youtestmessage.txt
After this you have to get email on email@example.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 "firstname.lastname@example.org".
Gmail has one specific thing which has been described in previous article.