Wiki: Postfix as a Gmail SMTP Relay for Email Filter Events

Forum for questions and support relating to the 1.24.x releases only.
Locked
jbmia
Posts: 50
Joined: Sat Jul 30, 2011 1:56 pm

Wiki: Postfix as a Gmail SMTP Relay for Email Filter Events

Post by jbmia »

For those that aren't so mechanically inclined, I put together this install & configure tutorial to get Postfix up and running for use as an smtp relay for gmail. It's pretty rough formatting wise, but after spending many many hours chasing down a solution to getting event alerts via email, I felt compelled share a what ended up being a relatively simple solution.

http://www.zoneminder.com/wiki/index.ph ... ter_events.

jbmia
Last edited by jbmia on Sun Aug 07, 2011 1:05 pm, edited 1 time in total.
kaneco
Posts: 2
Joined: Wed Nov 11, 2009 5:49 pm

Re: Wiki: Postfix as a Gmail SMTP Relay for Email Filter Ev

Post by kaneco »

Hello,

Can you please send me the tutorial to set-up the email from gmail to receive the alarms from zoneminder in the gmail box. the link is broken.

Best regards
tijuca
Posts: 9
Joined: Sun Jul 31, 2011 10:01 am

Re: Wiki: Postfix as a Gmail SMTP Relay for Email Filter Ev

Post by tijuca »

http://www.zoneminder.com/wiki/index.ph ... ter_events.

You can also click on left side "Recent Changes" and you will find modified pages. :wink:
jbmia
Posts: 50
Joined: Sat Jul 30, 2011 1:56 pm

Re: Wiki: Postfix as a Gmail SMTP Relay for Email Filter Ev

Post by jbmia »

Hmmm.. Neither of those links works.. and when I used search for "postfix" and "gmail" nothing comes up.. not very helpful. Not sure why these don't work but, maybe they can be fixed by a moderator or something...

Anywho, for those that stumble across this, here's how to get there.. from the top of the home page...

Click: Wiki, then "contents" (in the paragraph that pops up, then "How To" at the bottom of the page, then look to the bottom of the page where the "Other Stuff" is located.. It's at the bottom right now.. Not the most prominent place, but based on the heading descriptions this looked to me like where it should go.. If someone more knowledgeable deems otherwise, please move it as appropriate.

BTW, thanks tijuca for the formatting revision. Looks cleaner.

Also, I made a small correction.. In the postmap hash config section, I changed:
"postmap -q smtpout.secureserver.net /etc/postfix/sasl_passwd"
to:
"postmap -q smtp.gmail.com:587 /etc/postfix/sasl_passwd"

The older one was from when I was trying to get a godaddy account going and I pasted it into these instructions in error.

Good luck!

jbmia
User avatar
virtualgibe
Posts: 3
Joined: Sun Aug 07, 2011 12:22 pm

Re: Wiki: Postfix as a Gmail SMTP Relay for Email Filter Ev

Post by virtualgibe »

I followed your guide wiki.
I well configured Postfix. And Postfix work correctly.
I can send emails with the test commands:

Code: Select all

$ Postfix reload
$ Sendmail sendmail@example,com
Gmail thru Relay Test
.
ZoneMinder I configured it this way:

Code: Select all

OPT_EMAIL: yes
EMAIL_ADDRESS: sendmail@example,com
OPT_MESSAGE: no
MESSAGE_ADDRESS: sendmail@example,com
EMAIL_HOST: localhost
FROM_EMAIL: mymail@gmail,com
Where can I check that my configuration is correct?
ZoneMinder does not send mail. I have configured mail filters that work with flags.

Code: Select all

Email details of all matches: yes
Where is my error?

I have not modified: zmfilter.pl
jbmia
Posts: 50
Joined: Sat Jul 30, 2011 1:56 pm

Re: Wiki: Postfix as a Gmail SMTP Relay for Email Filter Ev

Post by jbmia »

My suggestion would be to have several ssh terminal as follows:
1. tail -f /var/log/messages
2. tail -f /var/log/zm/zm*.log
3. tail -f /var/log/maillog

Double check those log files and adjust paths if they are different...

From there I would generate an event and watch those logs closely.. make sure postfix is running.. make sure you don't have any permissions issues... Zma or zmd should show the alert as soon as it detects it.. Watch that zmfilter.log shows the event filter getting picked up and processed... Then look for zmfilter to create the email and then maillog to actually send it... From their Something will come up in one of those logs that will clue you in... Double check all your settings.. this morning I was testing filter for a specific monitor and it didn't generate because I had "less than" instead of "equal to".. a simple mistake, but they can be frustrating..

Let us know how it goes..

jbmia
pp1981
Posts: 17
Joined: Sun Jul 10, 2011 3:29 pm

Re: Wiki: Postfix as a Gmail SMTP Relay for Email Filter Ev

Post by pp1981 »

Hi,

I made the procedure posted by jbmia.

When I digit
tail -f /var/log/mail.log
the monitor shows this error
Aug 10 22:40:29 **** postfix/smtp[15222]: 284833057BD: to=<myusername@gmail,com>, relay=none, delay=1169, delays=1169/0.15/0/0, dsn=4.4.3, status=deferred (Host or domain name not found. Name service error for name=smtp,gmail,com type=MX: Host not found, try again).

What does it mean?

Thank you
pp1981
User avatar
virtualgibe
Posts: 3
Joined: Sun Aug 07, 2011 12:22 pm

Re: Wiki: Postfix as a Gmail SMTP Relay for Email Filter Ev

Post by virtualgibe »

EUREKA!!!
I have reinstalled the operating system and everything went well this time.
I have Ubuntu 11.04 - Natty Narwhal

The installation procedure is different in some points to CentOS
I followed these steps:
If you want to place the power of wiki, I can do it myself?

Email Setup Ubuntu 11.04 - Natty Narwhal / postfix / Gmail / Zoneminder / Event Filter

A1. First, Install postfix

set root user

Code: Select all

$ sudo -i
Install postfix

Code: Select all

root@***:/# apt-get install postfix
select OK
select no configuration

Code: Select all

root@***:/# whereis -b postfix
postfix: /usr/sbin/postfix /etc/postfix /usr/lib/postfix /usr/share/postfix

Code: Select all

root@***:/# ldd /usr/sbin/postfix
libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0x00359000)
A2. Install CA root certificate bundle

Code: Select all

root@***:/# apt-get install nmap
A3. Now we need to find your server's CA root certificate bundle

Code: Select all

root@***:~# updatedb

Code: Select all

root@***:/# locate ca-bundle.crt
/usr/share/ncat/ca-bundle.crt
B1. Edit /etc/postfix/main.cf with the following values

Code: Select all

root@***:/# gedit /etc/postfix/main.cf
Copy/paste the following into a blank "/etc/postfix/main.cf" file

Code: Select all

relayhost = smtp.gmail,com:587
mydomain = local.domain
myhostname = host.local.domain
myorigin = $myhostname
smtpd_sasl_path = smtpd
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_type = cyrus
smtp_sasl_auth_enable = yes
smtp_sasl_security_options = noanonymous
smtp_sasl_tls_security_options = noanonymous
smtp_use_tls  = yes
smtp_tls_CAfile = /usr/share/ncat/ca-bundle.crt
smtp_sasl_tls_security_options = noanonymous
Save File and Close

B2. Copy/paste the following into a blank "/etc/postfix/sasl_passwd" file.

Code: Select all

root@***:/# gedit /etc/postfix/sasl_passwd
Copy/paste the following into a blank "/etc/postfix/sasl_passwd" file
Change username & password to your gmail username and password of course.

Code: Select all

# The server info must exactly match the value
# for "relayhost" in /etc/postfix/main.cf
smtp.gmail,com:587 yourusername:yourpassword
Save File and Close

B3. Generate a postfix lookup table from the previous file

Code: Select all

root@***:/# postmap hash:/etc/postfix/sasl_passwd
Make sure that the hash "took" by checking it with the following command.
The response should be the user:pass that you defined for the file.
Basically, we're confirming that Postfix has the ability to pull up the user:pass when it needs it and you didn't hose
the command with the wrong path or file name

Code: Select all

root@***:/# postmap -q smtp.gmail,com:587 /etc/postfix/sasl_passwd
B4. Get rid of the clear text password file (I'd really do this at the end, once it's confirmed functional, but don't forget)

Code: Select all

root@***:/# rm /etc/postfix/sasl_passwd
C1. Restart postfix and send a test email

Code: Select all

root@***:~# postfix start

Code: Select all

root@***:/# postfix reload

Code: Select all

root@***:/# sendmail yourmail@gmail,com
Test relay thru Gmail
.
C2. Go check your gmail account you sent your test email to.

D. Configure ZoneMinder:
Options -> Email

OPT_EMAIL >CHECK<
EMAIL_ADDRESS yourmail@gmail,com
MESSAGE_ADDRESS yourmail@gmail,com
EMAIL_HOST localhost
FROM_EMAIL yourmail@gmail,com
abandoning the other aspects parameters

E. Set a valid Event Filter
Email details of all matches >CHECK<
GOOD WORK! :D
jbmia
Posts: 50
Joined: Sat Jul 30, 2011 1:56 pm

Re: Wiki: Postfix as a Gmail SMTP Relay for Email Filter Ev

Post by jbmia »

Congratulations virtualgibe!!

pp1981,

Look at this part: Host or domain name not found. Name service error for name=smtp,gmail,com

That's telling you that the name server cannot translate smtp,gmail,com (but shouldn't it be0 "smtp.gmail.com"). I know in Europe they sometimes invert .'s and ,'s, but do they do that for ip addresses to ?? Double check that.. the issue might be as simple as that..

jbmia
pp1981
Posts: 17
Joined: Sun Jul 10, 2011 3:29 pm

Re: Wiki: Postfix as a Gmail SMTP Relay for Email Filter Ev

Post by pp1981 »

Thank you jbmia but that's not the problem...

I only wrote "," and not "." here in the post bcause it was red like a spam.

Problem solved: that's only need to restart the PC :-)
pp1981
Posts: 17
Joined: Sun Jul 10, 2011 3:29 pm

Re: Wiki: Postfix as a Gmail SMTP Relay for Email Filter Ev

Post by pp1981 »

Many congratulations VIRTUALGIBE!!! ;-)
hmjgriffon
Posts: 7
Joined: Thu Feb 06, 2014 8:48 pm

Re: Wiki: Postfix as a Gmail SMTP Relay for Email Filter Ev

Post by hmjgriffon »

Can someone give me the postfix config that works with ubuntu? This didn't match for me http://www.zoneminder.com/wiki/index.ph ... ter_events.
hmjgriffon
Posts: 7
Joined: Thu Feb 06, 2014 8:48 pm

Re: Wiki: Postfix as a Gmail SMTP Relay for Email Filter Ev

Post by hmjgriffon »

I FOUND IT, holy crap so easy, maybe that's why it's hard to find anything on this because all of the linux nerds are laughing going oh you don't know how to set up postfix with gmail, buhahahahahaha RTFM, well whatever, I just followed this and it worked perfectly.

f you want to use a Gmail account as a free SMTP server on your Ubuntu-Linux server, you will find this article useful. This guide is tested with Ubuntu 12.04. If you face any issue, feel free to use comments-section below.

Relaying Postfix mails via smtp.gmail.com:

First, install all necessary packages:

sudo apt-get install postfix mailutils libsasl2-2 ca-certificates libsasl2-modules
If you do not have postfix installed before, postfix configuration wizard will ask you some questions. Just select your server as Internet Site and for FQDN use something like mail.example.com

Then open your postfix config file:

vim /etc/postfix/main.cf
and following lines to it:

relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_CAfile = /etc/postfix/cacert.pem
smtp_use_tls = yes
You might have noticed that we haven’t specified our Gmail username and password in above lines. They will go into a different file. Open/Create

vim /etc/postfix/sasl_passwd
And add following line:

[smtp.gmail.com]:587 USERNAME@gmail.com:PASSWORD
If you want to use your Google App’s domain, please replace @gmail.com with your @domain.com

Fix permission and update postfix config to use sasl_passwd file:

sudo chmod 400 /etc/postfix/sasl_passwd
sudo postmap /etc/postfix/sasl_passwd
Next, validate certificates to avoid running into error. Just run following command:

cat /etc/ssl/certs/Thawte_Premium_Server_CA.pem | sudo tee -a /etc/postfix/cacert.pem
Finally, reload postfix config for changes to take effect:

sudo /etc/init.d/postfix reload
Testing if mails are sent via Gmail SMTP server:
If you have configured everything correctly, following command should generate a test mail from your server to your mailbox.

echo "Test mail from postfix" | mail -s "Test Postfix" you@example.com
To further verify, if mail sent from above command is actually sent via Gmail’s SMTP server, you can log into Gmail account USERNAME@gmail.com with PASSWORD and check “Sent Mail” folder in that Gmail account. By default, Gmail always keeps a copy of mail being sent through its web-interface as well as SMTP server. This logging is one strong reason that we often use Gmail when mail delivery is critical.

Once configured, all emails from your server will be sent via Gmail. This method will be useful if you have many sites on your server and want them all to send emails via Gmail’s SMTP server.
ac7ss
Posts: 4
Joined: Wed Mar 05, 2014 3:55 pm

Re: Wiki: Postfix as a Gmail SMTP Relay for Email Filter Ev

Post by ac7ss »

Notice: If you are getting errors in your logs with the line

Code: Select all

zmfilter.pl exited abnormally, exit status 9
try installing:

Code: Select all

apt-get install libmime-tools-perl
Fixed it for me. (Now to work on writing the correct filter! my mail got flooded.)
Locked