Search code examples
phpphpmailer

PHP Mailer e-mail sent but not received


I'm using PHP mailer to send an e-mail.

PHP mailer debug is telling me this:

2020-04-08 10:16:59 Connection: opening to 192.***.**.**:25, timeout=300, options=array()
2020-04-08 10:16:59 Connection: opened
2020-04-08 10:16:59 SERVER -> CLIENT: 220 SRVEX01.***.local Microsoft ESMTP MAIL Service ready at Wed, 8 Apr 2020 12:16:59 +0200
2020-04-08 10:16:59 CLIENT -> SERVER: EHLO ncrapp.***.nl
2020-04-08 10:16:59 SERVER -> CLIENT: 250-SRVEX01.***.local Hello [192.168.50.31]250-SIZE 104857600250-PIPELINING250-DSN250-ENHANCEDSTATUSCODES250-X-ANONYMOUSTLS250-AUTH NTLM250-X-EXPS GSSAPI NTLM250-8BITMIME250-BINARYMIME250-CHUNKING250 XRDST
2020-04-08 10:16:59 CLIENT -> SERVER: MAIL FROM:<Flow.Info@***.nl>
2020-04-08 10:16:59 SERVER -> CLIENT: 250 2.1.0 Sender OK
2020-04-08 10:16:59 CLIENT -> SERVER: RCPT TO:<rob@***.nl>
2020-04-08 10:16:59 SERVER -> CLIENT: 250 2.1.5 Recipient OK
2020-04-08 10:16:59 CLIENT -> SERVER: RCPT TO:<ronnie@***.nl>
2020-04-08 10:16:59 SERVER -> CLIENT: 250 2.1.5 Recipient OK
2020-04-08 10:16:59 CLIENT -> SERVER: RCPT TO:<koen@***.nl>
2020-04-08 10:16:59 SERVER -> CLIENT: 250 2.1.5 Recipient OK
2020-04-08 10:16:59 CLIENT -> SERVER: DATA
2020-04-08 10:16:59 SERVER -> CLIENT: 354 Start mail input; end with <CRLF>.<CRLF>
2020-04-08 10:16:59 CLIENT -> SERVER: Date: Wed, 8 Apr 2020 12:16:59 +0200
2020-04-08 10:16:59 CLIENT -> SERVER: To: rob@***.nl, ronnie@***.nl, koen@***.nl
2020-04-08 10:16:59 CLIENT -> SERVER: From: Flow.Info@***.nl
2020-04-08 10:16:59 CLIENT -> SERVER: Subject: Test Mail
2020-04-08 10:16:59 CLIENT -> SERVER: Message-ID: <mUgoSkvhJrgVz1iGjf0wTNZ0RPaIWMxpxujmhFRzdw@ncrapp.***.nl>
2020-04-08 10:16:59 CLIENT -> SERVER: X-Mailer: PHPMailer 6.0.1 (https://github.com/PHPMailer/PHPMailer)
2020-04-08 10:16:59 CLIENT -> SERVER: MIME-Version: 1.0
2020-04-08 10:16:59 CLIENT -> SERVER: Content-Type: text/html; charset=iso-8859-1
2020-04-08 10:16:59 CLIENT -> SERVER:
2020-04-08 10:16:59 CLIENT -> SERVER: Test Mail
2020-04-08 10:16:59 CLIENT -> SERVER:
2020-04-08 10:16:59 CLIENT -> SERVER: .
2020-04-08 10:16:59 SERVER -> CLIENT: 250 2.6.0 <mUgoSkvhJrgVz1iGjf0wTNZ0RPaIWMxpxujmhFRzdw@ncrapp.***.nl> [InternalId=108795816574987, Hostname=SRVEX01.***.local] 1719 bytes in 0.096, 17,471 KB/sec Queued mail for delivery
2020-04-08 10:16:59 CLIENT -> SERVER: QUIT
2020-04-08 10:16:59 SERVER -> CLIENT: 221 2.0.0 Service closing transmission channel
2020-04-08 10:16:59 Connection: closed

I've multiple e-mail addresses where I sent the e-mail to. Everything seems to be okay, but some mail addresses never received the e-mail.

Me (Rob) received the e-mail but the other 2 employees did not receive an e-mail. The company where I'm a trainee for has around 100 employees, 10 of them can not receive an e-mail from my application.

Is this a programming problem? Or a mail server problem?

My code:

use PHPMailer\PHPMailer\PHPMailer;
include_once "PHPMailer/PHPMailer.php";
include_once "PHPMailer/Exception.php";
include_once "PHPMailer/SMTP.php";

$mail = new PHPMailer();

$mail->SMTPDebug = 3;

$mail->Host = "192.***.**.**";
$mail->isSMTP();
$mail->SMTPAuth = false;
$mail->Username = "***";
$mail->Password = "***";
$mail->Port = 25;

$mail->addAddress("rob@***.nl");
$mail->addAddress("ronnie@***.nl");
$mail->addAddress("koen@***.nl");

$mail->setFrom('Flow.Info@***.nl');

$mail->Subject = "Test Mail";
$mail->isHTML(true);

$mail->Body = "Test Mail";

$mail->send();

Solution

  • Debugging

    Enabling less secure apps to access Gmail

    Open your Google Admin console (admin.google.com).

    Click Security > Basic settings .

    Under Less secure apps, select Go to settings for less secure apps .

    In the subwindow, select the Enforce access to less secure apps for all users radio button. ...

    Click the Save button.

    USING LATEST PHPMAILER CODE

    // Import PHPMailer classes into the global namespace
    // These must be at the top of your script, not inside a function
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\SMTP;
    use PHPMailer\PHPMailer\Exception;
    
    // Load Composer's autoloader
    require 'vendor/autoload.php';
    
    // Instantiation and passing `true` enables exceptions
    $mail = new PHPMailer(true);
    
    try {
        //Server settings
        $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      // Enable verbose debug output
        $mail->isSMTP();                                            // Send using SMTP
        $mail->Host       = 'smtp1.example.com';                    // Set the SMTP server to send through
        $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
        $mail->Username   = 'user@example.com';                     // SMTP username
        $mail->Password   = 'secret';                               // SMTP password
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
        $mail->Port       = 587;                                    // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
    
        //Recipients
        $mail->setFrom('from@example.com', 'Mailer');
        $mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
        $mail->addAddress('ellen@example.com');               // Name is optional
        $mail->addReplyTo('info@example.com', 'Information');
        $mail->addCC('cc@example.com');
        $mail->addBCC('bcc@example.com');
    
        // Attachments
        $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
        $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
    
        // Content
        $mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject = 'Here is the subject';
        $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
        $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
    
        $mail->send();
        echo 'Message has been sent';
    } catch (Exception $e) {
        echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
    }```