📜 ⬆️ ⬇️

Mail setup for debugging mail functions of developed applications on Ubuntu

Hello!

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

In order to work sending mail from PHP and everything else, you need an MTA. I use Synaptic to install most of the programs, and in it Postfix is ​​marked with the Ubuntu logo (which means that it is the default MTA), so, without much hesitation, I installed Postfix. When installing using a synaptic, the Postfix configuration window pops up, I selected localhost only.
')
When I got to the point of testing a single function in a PHP application related to sending mail, there was a curiosity. I'm testing with a copy of the live database, and, after sending the letter, it was naturally sent to MTA for its intended purpose, which was already lost under Windows, where I used the Courier Mail Server (by the way, very small and convenient). Of course, I immediately apologized to the person who left the letter.

In this connection, the question arose: how to configure the mail so that it does not go beyond the localhost, or go 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.

It is configured this way:
A line is added to the /etc/postfix/main.cf file.
header_checks = regexp:/etc/postfix/header_checks
the file / etc / postfix / header_checks is created, the following 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 changes are accepted by postfix, all incoming mail will be put on the hold queue, and will not be sent anywhere.

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

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

The second method is to send all mail to a local mailbox. It is convenient to view mail from a local mailbox by setting up an email client (which usually always works) on POP3 localhost.

This is done like this:
With the help of a synaptic (or apt-get, to whom, as it is convenient), the POP3 daemon dovecot 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 by putting # at the beginning of the line). Execute commands
postmap /etc/postfix/header_checks
postfix reload

The mail client is configured on POP3 localhost with the login and password of the account 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 be in your inbox almost immediately.

+ 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 else in this life). It is good that you can change the treatment 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.

Source: https://habr.com/ru/post/22805/


All Articles