PHPMailer - 跳过向无效地址发送电子邮件 [英] PHPMailer - Skip sending emails to invalid adresses

查看:116
本文介绍了PHPMailer - 跳过向无效地址发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 PHPMailer 发送电子邮件,并且运行良好.只有一个问题,我不知道如何解决.由于域不存在,我可能需要尝试将电子邮件发送到无效地址.由于域不存在,这些电子邮件不会被发送,这很好.当我尝试时,我收到一条错误消息,并且 PHPMailer 停止并且也不会继续向其他(有效)地址发送电子邮件.有没有办法跳过那些无效的电子邮件并强制 PHPMailer 继续而不显示错误消息?

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?

错误消息:

致命错误:未捕获的 PHPMailer\PHPMailer\Exception:SMTP 错误:以下收件人失败:erika@dummyverein.de:域不存在:'dummyverein.de' in...

Fatal error: Uncaught PHPMailer\PHPMailer\Exception: SMTP Error: The following recipients failed: erika@dummyverein.de: Domain does not exist: 'dummyverein.de' in...

服务器 ->客户端:521 5.1.2 域不存在:'dummyverein.de'

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

SMTP 错误:RCPT TO 命令失败:521 5.1.2 域不存在:'dummyverein.de'

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

代码:

$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();

推荐答案

解决这个问题使用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}";
   }

这篇关于PHPMailer - 跳过向无效地址发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆