Skip to content
Snippets Groups Projects
test_mail.py 2.25 KiB
Newer Older
Stéphane Diemer's avatar
Stéphane Diemer committed
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
Check that emails can be sent.
Stéphane Diemer's avatar
Stéphane Diemer committed
'''
import os
import subprocess
import sys
Stéphane Diemer's avatar
Stéphane Diemer committed

GREEN = '\033[92m'
RED = '\033[91m'
DEF = '\033[0m'

Florent Thiery's avatar
Florent Thiery committed
os.chdir(os.path.dirname(__file__))
if not os.path.isfile('../utils.py'):
    print('conf.sh not found')
    sys.exit(1)

Stéphane Diemer's avatar
Stéphane Diemer committed

def print_color(txt, col):
    print('%s%s%s' % (col, txt, DEF))

Stéphane Diemer's avatar
Stéphane Diemer committed

def print_red(txt):
    print_color(txt, RED)

Stéphane Diemer's avatar
Stéphane Diemer committed

def print_green(txt):
    print_color(txt, GREEN)

Stéphane Diemer's avatar
Stéphane Diemer committed

Stéphane Diemer's avatar
Stéphane Diemer committed

def get_configured_relay():
    status, out = subprocess.getstatusoutput('grep relayhost /etc/postfix/main.cf')
    if status == 0:
        return out.split('relayhost = ')[1]
Stéphane Diemer's avatar
Stéphane Diemer committed

Florent Thiery's avatar
Florent Thiery committed
    global conf
    configured_relay = get_configured_relay()
    print('Checking if SMTP relay conforms to conf')
Florent Thiery's avatar
Florent Thiery committed
    es_utils = imp.load_source('es_utils', '../utils.py')
    conf = es_utils.load_conf()
    conf_relay = conf.get('EMAIL_SMTP_SERVER')
    if conf_relay != configured_relay:
        print_red('Configured STMP relay (%s) does not match the expected value (%s)' % (configured_relay, conf_relay))
        all_ok = False
    else:
        print_green('STMP relay is properly set')
Stéphane Diemer's avatar
Stéphane Diemer committed

    print('Sending test email')
    global all_ok
    email = "noreply+%s@ubicast.eu" % random.randint(0, 1000)
    cmd = 'echo "This is a test email" | mail -s "Test email from `cat /etc/hostname`" %s' % email
    status, out = subprocess.getstatusoutput(cmd)
    time.sleep(5)
    status, out = subprocess.getstatusoutput('grep %s /var/log/mail.log | grep bounced' % email)
    if status == 0:
        print_red('Sending mail failed')
        all_ok = False
    else:
        print_green('Email sent')
Stéphane Diemer's avatar
Stéphane Diemer committed

Stéphane Diemer's avatar
Stéphane Diemer committed

Stéphane Diemer's avatar
Stéphane Diemer committed
if not os.path.exists('/etc/postfix'):
    print_red('Postfix dir does not exists, please install postfix.')
    all_ok = False
Stéphane Diemer's avatar
Stéphane Diemer committed
else:
    # check that postfix listens the port 25 correctly
Florent Thiery's avatar
Florent Thiery committed
    status, out = subprocess.getstatusoutput('netstat -pant | grep master | grep 127.0.0.1:25')
    if status != 0:
        print_red('The port 25 is not listened by any process.')
        all_ok = False
    else:
        print_green('Postfix listening port: OK.')
        check_relay()
        send_test_email()

sys.exit(int(not all_ok))