Mail setup for debugging mail-functions of developed applications on Ubuntu

    Hello!

    Recently I bought a new laptop (the old one became quite old). I installed Ubuntu on it (why I deleted another OS, first described it, but then erased it so that there was no flame and holivar).

    For sending mail from PHP and everything else to work, you need an MTA. I use Synaptic to install most programs, and Postfix is ​​marked with the Ubuntu logo in it (which means that it is the MTA by default), so without any hesitation, I installed Postfix. When installing using the synaptic, the Postfix configuration window pops up, I chose the localhost only option.

    When it came to testing one function in a PHP application related to sending mail, a funny thing happened. I’m testing with a copy of the live database, and, after sending the letter, it, of course, was sent to MTA for its intended purpose, from which it had already lost the habit of using Windows, where it used Courier Mail Server (by the way, it’s very small and convenient). Of course, he immediately apologized to the man who had left the letter.

    In this connection, the question arose: how to configure mail so that it does not go beyond localhost, or goes to a specific local mailbox?

    Break the network, I realized that the easiest option is to redirect all incoming mail to a special queue called hold. Mail in this queue can be any number; resources, except for disk space, does not consume.

    This is set up like this:
    A line
    header_checks = regexp:/etc/postfix/header_checks
    is added to the /etc/postfix/main.cf file, the / etc / postfix / header_checks file is created, this line is added to it:
    /^Received:/ HOLD
    after which the command is executed
    postmap /etc/postfix/header_checks
    and postfix is ​​“refreshed” by the command
    postfix reload
    As soon as the changes are accepted by the postfix, all incoming mail will get into the hold queue, and will not go anywhere.

    Mail in this queue can be viewed in several ways:
    1. Any text watcher, for example, built into mc. All messages in the queue are located in the / var / spool / postfix / hold directory, in separate files, so you can view any message.
    2. Using the pfqueue program (I set as a synaptic).
    pfqueue should be run like this:
    pfqueue -q 4
    the q parameter sets the queue number, the queue hold has number 4.

    + headers are saved unchanged
    - pfqueue does not show the message body (at least for me, the message is in 1251) The

    second method is to forward all mail to the local mailbox. It is convenient to view mail from the local mailbox by configuring the mail client (which usually always works) on POP3 localhost.

    This is done like this:
    with the help of a synaptic (or apt-get, anyone can use it) a POP3 dovecot daemon is installed (for POP3 to work) a
    line is added to the / etc / postfix / header_checks file
    /^Received:/ REDIRECT your_login@localhost
    (if you already have a line with HOLD, you need to comment it out, putting at the beginning of the line #). Commands are executed
    postmap /etc/postfix/header_checks
    postfix reload

    The mail client is configured on POP3 localhost with the login and password of the account that is specified in REDIRECT.

    After that, if the client is configured to check mail with a short period of time, all messages sent by any program on the computer will appear in your inbox almost immediately.

    + it’s convenient to watch mail
    - the To: headers are overwritten, they are replaced with the address in REDIRECT.

    So, each option has its advantages and disadvantages (like everything in this life). It’s good that you can change the processing option at any time. If there is any more convenient way to intercept all mail in Ubuntu, I will be glad to know about it in the comments.

    Also popular now: