PHPMailer - Skip sending emails to invalid adresses
Asked Answered
E

3

0

I'm trying to send emails via PHPMailer and it's working pretty fine. There's just one problem and I do not know how to fix it. There is the possibility, that I might need to try to send emails to an invalid address due to a non existing domain. It's fine that those emails won't be sent as the domain doesn't exist. When I try to, I get an error message and PHPMailer stops and will also not continue sending emails to other (valid) addresses. Is there any way to kind of skip those invalid emails and force PHPMailer to continue without showing error messages?

Error messages:

Fatal error: Uncaught PHPMailer\PHPMailer\Exception: SMTP Error: The following recipients failed: [email protected]: Domain does not exist: 'dummyverein.de' in...

SERVER -> CLIENT: 521 5.1.2 Domain does not exist: 'dummyverein.de'

SMTP ERROR: RCPT TO command failed: 521 5.1.2 Domain does not exist: 'dummyverein.de'

Code:

$mail = new PHPMailer(true);
$mail->CharSet = 'utf-8';  
$mail->isSMTP();
$mail->isHTML(true);
$mail->Host = 'smtp.strato.de';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = 'xxxx';
$mail->Password = 'xxxx';
$mail->SMTPSecure = 'tls';
$mail->SMTPDebug = 2;            // set to 2 to get error messages for now 
$mail->MailerDebug = false;
$mail->setFrom($absender, $name);
$mail->addAddress($to);
$mail->Subject = $subject;
         
$mail->Body = $message_other_player;

$mail->send();
Evette answered 16/5, 2020 at 2:12 Comment(10)
I'm not sure but try use @ before $mail->send();Jackleg
2nd; Another way is go to PHPMailer source and make some changeJackleg
so like @$mail->send();?Evette
Yes, @$mail->send();Jackleg
Okay, did not have any impact, same resultEvette
Try to comment this $mail->isSMTP();Allusion
@Allusion wow, that looks like it actually worked. I thought that would just move the problemEvette
@HenrikFiedler glad it worked for you.Allusion
Don’t do either of these things. They don’t solve the problem, they just hide it; it’s still happening.Inflexible
Before sending the mail You can check if the domain exist by using gethostbyname ( string $hostname ) : string. php.net/manual/de/function.gethostbyname.phpRancor
F
1

for solving this problem use try ... catch

$mail=new PHPMailer(true);
try {
$mail->CharSet = 'utf-8';  
$mail->isSMTP();
$mail->isHTML(true);
$mail->Host = 'smtp.strato.de';
$mail->Port = 587;
$mail->SMTPAuth = false;
$mail->Username = 'xxxx';
$mail->Password = 'xxxx';
$mail->SMTPSecure = 'tls';
$mail->SMTPDebug = 0;
$mail->MailerDebug = false;
$mail->setFrom($absender, $name);
$mail->addAddress($to);
$mail->Subject = $subject;
$mail->Body = $message_other_player;
}

$mail->send();
      // echo 'Message has been sent';
   } catch (Exception $e) {
     //  echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
   }
Fer answered 6/5, 2021 at 17:59 Comment(0)
U
0

try this:

$mail = new PHPMailer();
instead of :
$mail = new PHPMailer(true);
Unmannered answered 9/8, 2024 at 9:34 Comment(1)
"Try this" does not qualify for an answer. Could you explain why would the OP want to try this and why it would work?Bastian
E
-1

EDIT:

So I used an email validation function that checks if a domain exists from this StackOverflow question. It's working perfectly. Thanks a lot to @karim79

OLD ANSWER:

Warning: the following code is kind of working, but it was not a real solve and it's just moving the problem as SMTP is just ignored:

So thanks to @Hardood, it looks like this simple change actually solved it. I thought it would just move the problem, but it seems to work for now.

// $mail->isSMTP();

Simply commenting or deleting the "isSMTP()"

Evette answered 16/5, 2020 at 2:36 Comment(5)
This is not the solution you think it is. Your script will run, mail will be delivered to your local mail server, but then it will fail and you will have no idea that it has even happened. Hiding problems is not solving them.Inflexible
@Inflexible I had the same concerns but I just tried it. The emails are successfuly sent. To different mail servers like my Apple email too.Evette
In that case it’s likely that your hosting provider blocks outbound SMTP and routes it through their servers. This is likely to cause SPF failures, so check that.Inflexible
Okay, so I get what you're on about @Synchro. Do you maybe have any solving recommendations?Evette
PHPMailer has built-in email validation, as does PHP itself (using filter_var()), and there is a code example provided with PHPMailer showing how to use PHPMailer's SMTP class to check whether an address exists. As for the rest of the problem, investigate and see exactly how your email is being routed (look at the headers of a received message), do SPF checks (plenty of services can help you with that).Inflexible

© 2022 - 2025 — McMap. All rights reserved.