Search code examples
phpubuntuemailsmtpphpmailer

Long loading times when sending emails with PHPMailer in PHP 8.1 on Ubuntu 22.04


I am using PHPMailer in PHP 8.1 on Ubuntu 22.04 with Apache 2 to send emails through PHP web page scripts. The email sending works fine using the configured SMTP server, but all scripts that send an email often take a very long time (1-2 minutes) to load and finally send the mail. This is very problematic, as website users are confused and frequently reload the page after sending a form. When I run the same script locally on my computer with XAMPP, it responds immediately (using the same SMTP server). The same situation occurs using a completly different SMTP server. What could be causing this issue?

I am running Ubuntu in its current default configuration with ufw as the firewall (tutorial from DigitalOcean). Only a few mails should be send (up to two per hour).

I would appreciate any insights or suggestions on possible reasons for the long loading times when sending emails with PHPMailer in my specific setup. Thank you!


Solution

  • I was able to solve the problem with ideas from another forum: The DNS was to blame! The server had an IPv4 and an IPv6 address - and a hostname under which it communicates. But only the IPv4 was present in the DNS records. Since I removed the IPv6 completely, everything works immediately.