axllent / mailpit

An email and SMTP testing tool with API for developers
https://mailpit.axllent.org
MIT License
6.19k stars 153 forks source link

How to disable reverse DNS lookup? #230

Closed atesca09 closed 10 months ago

atesca09 commented 11 months ago

Mailpit uses smtpd as smtp server

smtpd tries to do a reverse DNS lookup of the IP sending the mail, see https://github.com/mhale/smtpd/blob/cfd012220c479797ab15cee042831c95ea95dd1e/smtpd.go#L244

docker compose generates a domain name based on -.

if I run the Mailpit as part of a docker compose project the generated name might get too long and the reverse DNS lookup return bad rdata

dockerd[1528]: level=error msg="[resolver] failed to write response" error="dns: bad rdata"

with that bad rdata the lookup times out after 10 seconds and the sender is resolved as unknown

But the 10 second timeout is too long for the Symfony 4.4 Mailer which uses a timeout of 5 seconds, see https://github.com/symfony/mailer/blob/554b8c0dc2db9d74e760fd6b726f527364f03302/Transport/Smtp/Stream/SocketStream.php#L29

Even with newer versions of Symfony, waiting 10 seconds for the timeout to occur seems wrong

Steps to reproduce:

Create a directory really-long-project-name-for-testing-dns-rdata

add a docker-compose.yaml with:

version: '3.9'

services:
    mailpit:
      image: axllent/mailpit
      ports:
        - '8025:8025'

    curl:
      image: curlimages/curl
      volumes:
        - ./email.txt:/email.txt:ro
      depends_on:
        - mailpit

add a demo email.txt

From: sender@example.com
To: recipient@example.com
Subject: Email Subject

This is the body of the email.
It can contain multiple lines of text.

send the email with docker compose run --rm curl smtp://mailpit:1025 --mail-from sender@example.com --mail-rcpt recipient@example.com --upload-file /email.txt

It will send the email with an "unknown" as the hostname of the sending IP, but it'll take 10 seconds which is too long to send emails from a Symfony 4.4 project

axllent commented 11 months ago

@atesca09 Yes I can confirm this problem. There are two issues here:

  1. You are using an invalid (auto-generated) hostname for your docker environment which is longer than RFC1034 allows (max 63 characters). The solution (work-around) for you is to specify a --name <shorter-name> to your docker compose command and provide a valid hostname. There is an open issue for Docker relating to this.
  2. As you've discovered, this DNS 10s timeout in Mailpit is originating from the mhale/smtpd package, not Mailpit itself. The 10s timeout is however a hardcoded DNS lookup timeout in Go itself, so that timeout can't be changed in smtpd. Please refer to the open issue on smtpd where there is a request to optionally allow disabling the DNS lookup entirely (which I could then make configurable with a flag in Mailpit). Whilst this does not change the Docker bug (names longer than 63 chars), there are several other reasons one may want to disable DNS lookups in smtpd. Please feel free to add to that smtpd issue so the author understands the need and can then maybe implement the option to disable it sooner.

In the meantime there is nothing I can do here unfortunately.

atesca09 commented 11 months ago

@axllent yeah I thought so, I was just checking in, in case I missed something.

Unfortunately the workaround specifying a shorter name isn't an option for me in the current setup.

I guess we'll have to wait for smtpd to provide the option to skip the reverse DNS lookup

axllent commented 11 months ago

@atesca09 I will probably submit a pull request to smtpd later today if get around to it. Out of curiosity, are you using Gitlab CI (in relation to it not being an option for you)?

atesca09 commented 11 months ago

@axllent No the issue for my use-case isn't in GitLab. Besides the workaround with emptying /etc/resolv.conf is unacceptable for the use-case. It is more a problem with the amount of projects that are affected (~100+) and the amount of changes that have to be made just to replace the old mailhog with mailpit

axllent commented 10 months ago

@atesca09 This new feature has been released in v1.13.0 and should solve your issue completely. Starting Mailpit with either the --smtp-disable-rdns flag or setting MP_SMTP_DISABLE_RDNS=true in your environment (Docker) disables the reverse DNS entirely. Obviously you need to pull the latest axllent/mailpit image first :)

version: '3.9'

services:
  mailpit:
    image: axllent/mailpit
    environment:
      - MP_SMTP_DISABLE_RDNS=true
    ports:
      - '8025:8025'

  curl:
    image: curlimages/curl
    volumes:
      - ./email.txt:/email.txt:ro
    depends_on:
      - mailpit
time docker compose run --rm  curl smtp://mailpit:1025 --mail-from sender@example.com --mail-rcpt recipient@example.com --upload-file /email.txt
[+] Creating 1/0
 ✔ Container really-long-project-name-for-testing-dns-rdata-mailpit-1  Running                                                                                                 0.0s 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   145    0     0  100   145      0  11276 --:--:-- --:--:-- --:--:-- 12083

real    0m0.878s
user    0m0.057s
sys     0m0.020s

Please confirm this resolves your issue? Thanks.

atesca09 commented 10 months ago

Confirmed, this works for me. Thanks a lot @axllent