user7589192
user7589192

Reputation:

The following recipients failed error with PHPMailer

Maybe I'm a bad Googler but I cannot find the answer for my current PHPMailer issue. I've never personally used PHPMailer, so this is my first project with it.

When I'm submitting my form, I'm getting this information which also includes an error but first things first:

2017-11-26 02:46:39 SMTP INBOUND: "235 Authentication succeeded" 
2017-11-26 02:46:39 SERVER -> CLIENT: 235 Authentication succeeded

With authentication done well, good job me, I'm connecting to the smtp server i'm using (not gmail, my web hosting email servers)

This is the error I'm getting:

SMTP Error: The following recipients failed: [email protected]: sender IP address may not use this server as asmarthost. Use the webserver/localhost SMTP server instead

My PHP code is this:

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'assets/vendor/autoload.php';

/**
 * This example shows how to handle a simple contact form.
 */
$msg = '';

if (array_key_exists('email', $_POST)) {
    date_default_timezone_set('Etc/UTC');

    $mail = new PHPMailer;

    $mail->SMTPDebug = 4;
    $mail->isSMTP();
    $mail->Host = 'my.smtp.host';
    $mail->SMTPAuth = true;
    $mail->Username = 'username';
    $mail->Password = 'password';
    $mail->SMTPSecure = 'tls';                           
    $mail->Port = 587;

    $mail->setFrom('[email protected]');

    $mail->addAddress('[email protected]');

    if ($mail->addReplyTo($_POST['email'], $_POST['name'])) {
        $mail->Subject = 'PHPMailer contact form';

        $mail->isHTML(false);

        $mail->Body = <<<EOT
Email: {$_POST['email']}
Name: {$_POST['name']}
Message: {$_POST['message']}
EOT;

        if (!$mail->send()) {

            $msg = 'Sorry, something went wrong. Please try again later.';
        } else {
            $msg = 'Message sent! Thanks for contacting us.';
        }
    } else {
        $msg = 'Invalid email address, message ignored.';
    }
}
?>

And I presume this is the right recipent code:

$mail->addAddress('[email protected]');

I might be dumb, but I cant figure out the issue here. I've changing some info, such as editing this

$mail->Host = 'my.smtp.host';

to this

$mail->Host = 'localhost';

But no luck. Any thoughts on this? All help appreciated. Thank you, thank you.

This PHPMailer code is copied from one of the examples. I've added the SMTP information myself.

Upvotes: 1

Views: 4459

Answers (1)

user7589192
user7589192

Reputation:

The reason why I got this error was simply because my hosting provider did not allow sending emails via the servers, only for personal use. Thank you @ManuelOtto who replied to my question above.

So if anyone else is getting the same error, check if your host allows sending emails via the hosted server(s). If this is not the case, contact your hosting provider, because this is most likely a problem at webhoster's end.

I could still use the contact form without using SMTP, by simply removing all the SMTP information I listed in my PHP code.

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'assets/vendor/autoload.php';

/**
 * This example shows how to handle a simple contact form.
 */
$msg = '';

if (array_key_exists('email', $_POST)) {
    date_default_timezone_set('Etc/UTC');

    $mail = new PHPMailer;

    $mail->setFrom('[email protected]');

    $mail->addAddress('[email protected]');

    if ($mail->addReplyTo($_POST['email'], $_POST['name'])) {
        $mail->Subject = 'PHPMailer contact form';

        $mail->isHTML(false);

        $mail->Body = <<<EOT
Email: {$_POST['email']}
Name: {$_POST['name']}
Message: {$_POST['message']}
EOT;

        if (!$mail->send()) {

            $msg = 'Sorry, something went wrong. Please try again later.';
        } else {
            $msg = 'Message sent! Thanks for contacting us.';
        }
    } else {
        $msg = 'Invalid email address, message ignored.';
    }
}
?>

Good luck.

Upvotes: 1

Related Questions