The simplest SMTP server for development

    For those who are lazy and do not want to bother, blow dust off a tambourine by setting up Exim4 / postfix / sendmail, a simple mail collector written in Python is proposed [1] .

    His job is to collect all the mail sent to this fake smtp server and put it in the specified folder / tmp / mails in the form of message files nobody@mail.local.1 with a serial number.


    Step # 1: installing nullmailer


    In standard turnips, there should be a nullmailer. If not, then download , and then install:
    sudo apt-get install nullmailer


    Go to / etc / nullmailer and add in the remotes file:
    localhost smtp


    Restart:
    service nullmailer restart


    In Debian-like Linux, the message queue is located in / var / spool / nullmailer / queue /

    Step # 2: installing fakemail


    Download the fakemail script , unpack it. If standard, then run
    python setup.py install
    or copy the file fakemail.py where necessary.

    Now I would like to make it run as a service, and not just hanging in the console.
    To do this, create the file /etc/init.d/fakemail and paste the following code into it:
    Hidden text
    #!/bin/sh
    set -e
    NAME=fakemail
    DAEMON=/usr/local/bin/$NAME.py
    DOPTIONS="--path=/tmp/mails"
    test -x "$DAEMON" || exit 0
    PIDDIR=/var/run
    PIDFILE=$PIDDIR/$NAME.pid
    LANG=C
    export LANG
    . /lib/lsb/init-functions
    start_fakemail()
    {
        start-stop-daemon --start --quiet --pidfile $PIDFILE --make-pidfile --exec $DAEMON -- $DOPTIONS >/dev/null 2>&1 &
    }
    stop_fakemail()
    {
        if [ -f "$PIDFILE" ]; then
            PID=`cat $PIDFILE`
            kill -2 $PID
            rm -f "$PIDFILE"
        fi
    }
    case "$1" in
        start)
            log_daemon_msg "Starting MTA"
            start_fakemail
            ;;
        stop)
            log_daemon_msg "Stopping MTA"
            stop_fakemail
            ;;
        restart)
            stop_fakemail
            start_fakemail
            ;;
    esac
    exit 0
    



    If you manually copied the file, then perhaps you should edit the file and in line 6, where DAEMON, replace the path to our Python file.

    Make it run:
    sudo chmod +x /etc/init.d/fakemail


    Now you need to create (under root) the mails folder in / tmp [2]
    sudo mkdir /tmp/mails


    And add links to the service during system shutdown:
    sudo ln -s /etc/init.d/fakemail /etc/rc0.d/K20fakemail
    sudo ln -s /etc/init.d/fakemail /etc/rc1.d/K20fakemail
    sudo ln -s /etc/init.d/fakemail /etc/rc6.d/K20fakemail


    If you need it to start automatically as well, add the following:
    code to add to autorun
    sudo ln -s /etc/init.d/fakemail /etc/rc2.d/S20fakemail
    sudo ln -s /etc/init.d/fakemail /etc/rc3.d/S20fakemail
    sudo ln -s /etc/init.d/fakemail /etc/rc4.d/S20fakemail
    sudo ln -s /etc/init.d/fakemail /etc/rc5.d/S20fakemail
    


    We start the service:
    service fakemail start


    [1] the main material was found by reference , I just added the service.

    [2] any other folder is also possible, but then it is necessary inside /etc/init.d/fakemail where DOPTIONS = change your path --path = / path / to / folder

    Also popular now: