<toc></toc> I put together the following python script in order to verify that mail is being delivered through a server and can be retrieved by IMAP-SSL on the next Nagios test cycle.
#!/usr/bin/python # $Id: check_delivery_imap.py,v 1.3 2015/05/16 19:48:11 root Exp root $ import sys import re import imaplib import smtplib from email.Utils import formatdate date = formatdate(localtime=True) host = sys.argv[1] recipient = sys.argv[2] password = sys.argv[3] warning = 0 # IMAP existing messages try: imapserver = imaplib.IMAP4_SSL(host) # imapserver.debug=4 imapserver.login(recipient,password) resp, messagecount = imapserver.select() if int(messagecount[0]) < 1: print "WARNING - no message in INBOX" warning = 1 resp, ids = imapserver.search(None, 'ALL') for id in ids[0].split(): imapserver.store(id, '+FLAGS', '\\Deleted') imapserver.expunge() imapserver.close() imapserver.logout() except: print "CRITIAL - IMAP server error" sys.exit(2) # Use SMTP to send new message try: testmessage = '''Date: ''' + date + ''' Subject: Nagios Delivery test message From: ''' + recipient + ''' To: ''' + recipient + ''' Nagios Delivery test message: ''' + date smtpserver = smtplib.SMTP(host,25) # smtpserver.set_debuglevel(1) smtpserver.starttls() smtpserver.sendmail(sys.argv[2], sys.argv[2], testmessage) smtpserver.quit() except: print "CRITIAL - SMTP server error" sys.exit(2) if warning == 1: sys.exit(warning) else: print "Mail Delivery OK"