Checking why PHP Mail isn't working

The Problem

The problem: Emails aren't being sent out. In this specific case it was the forum software "MyBB" not sending registration emails to users.

Test Script

The first thing to do is: Test if it actually sends the email.
For this I recommend using a non-gmail email like one from "temp-mail.org" or similar.
Simply copy and paste this PHP file to a browsable location in your web folder.

IMPORTANT: Change user@example.com to the email you want your server to send it from, for example noreply@yourdomain.net. Change me@hotmail.com to your own email address.

<?php
error_reporting(-1);
ini_set('display_errors', 'On');
set_error_handler("var_dump");
echo "Running: ";
$headers = 'From: user@example.com' . "\r\n" .
'Reply-To: user@example.com';
$success = mail('me@hotmail.com', 'The papers for your report', "Hope you can use them! BR", $headers);
if (!$success) {
    echo "ERROR: '";
    $errorMessage = error_get_last()['message'];
    echo $errorMessage;
    echo "'";
}else{
  echo " Successfull!";
}
?>
If you now visit the website and it says "Running: Successfull!" then PHP mail is working fine.
If it says "ERROR: " then try to install sendmail. It mostly fails to send if you don't have sendmail installed, so install it and try the script again.(Command: sudo apt-get install sendmail)

If it says "Successfull!" now but the email is still not arriving to your users then head over to the next section.

Checking Sendmail

If Emails are still not sending you can check the sendmail logs and mail spools.

To check if a mail was actually send you can use the following command:

sudo less /var/log/mail.log
This contains information from the journal which tells you if mails were accepted for delivery.

If they were accepted for delivery then check the mail spool of www-data for returning mails.
sudo less /var/spool/mail/www-data
In here are emails you get back from successfully sending one with the php mail function.
In my case there were a lot of emails from gmail returning to me, because gmail could not verify my domain. The information why mails aren't sending are contained in the body of the returning mail.
My example:
Subject: Returned mail: see transcript for details
Auto-Submitted: auto-generated (failure)

This is a MIME-encapsulated message

--X.X/vX.luckysrv.de

The original message was received at Sat, 27 Feb 2021 23:25:34 +0100
from localhost [127.0.0.1]

   ----- The following addresses had permanent fatal errors -----
<userX@gmail.com>
    (reason: 550-5.7.1 [3b01:4000:4d:f7f:284a:1aff:febc:44D2] Our system has detected that)

   ----- Transcript of session follows -----
... while talking to gmail-smtp-in.l.google.com.:
>>> DATA
<<< 550-5.7.1 [3b01:4000:4d:f7f:284a:1aff:febc:44D2] Our system has detected that
<<< 550-5.7.1 this message does not meet IPv6 sending guidelines regarding PTR
<<< 550-5.7.1 records and authentication. Please review
<<< 550-5.7.1  https://support.google.com/mail/?p=IPv6AuthError for more information
<<< 550 5.7.1 . m4si12345803wrp.16 - gsmtp
554 5.0.0 Service unavailable
This error message means the following:
The Server this email was sent from is missing an IPv6 entry that redirects to the paired domain. This means that the domain example.com correctly redirects to the server's IP 1.2.3.4, but the IPv6 address of the Server is forgotten and does not reverse-lookup to the (correct) example.com domain.

To fix this simply add an A record to your domain configuration that points to the IPv6 address of your server. To see the IP addresses of your server you can use the ip a command.