douzangdang2225 2018-02-07 06:21
浏览 84

将电子邮件发送到phpMailer中的有效电子邮件

I'm using phpMailer to send emails. Email recipients are passed as an array, and are added by looping through this array. Example:

public static function mailTo($recipients)
{
    $f3 = \Base::instance();
    $user = AclHelper::getCurrentUser();
    $template= new \Template;
    $mailBody= $template->render('leave/emailTemp.html');

    // When true, PHPMailer returns exceptions
    $mail = new PHPMailer(true);
    try {
        $mail->isSMTP();                                      // Set mailer to use SMTP
        $mail->isHTML(true);

        $mail->addAddress($user['email']);
        $mail->addAddress("abhshrestha@growbydata.com");
        $mail->addAddress("rmali@growbydata.com");

        foreach($recipients as $recipient){
            $mail->addAddress($recipient);
        }

        $mail->SMTPAuth = true; // authentication enabled
        $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
        $mail->Host = "smtp.gmail.com";
        $mail->Port = 465; // or 587
        $mail->Username = "growbydata.np@gmail.com";
        $mail->Password = "abcd";

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

        $userFullName = trim(ucfirst($user['firstname'])) . " " . trim(ucfirst($user['lastname']));
        $mail->FromName = $userFullName;
        $mail->Body =  $f3->get('message');
        $mail->Body .="<br>". $mailBody;
        $mail->Subject = 'Updates on leave date applied';

        $mailStatus = (boolean)$mail->send();

        if ($mailStatus === true) {
            return $mail;
        }
    } catch (phpmailerException $e) {
        $response = array(
            'status'=>'error',
            'message'=>'Got some error while sending emails',
            'exceptions'=>$e->getMessage()
        );
        return $response;

    } catch (Exception $e) {
        $response = array(
            'status'=>'error',
            'message'=>'Got some error while sending emails',
            'exceptions'=>$e->getMessage()
        );

        return $response;
    }
}

Emails are being sent to and received by recipients whose addresses are set statically. eg.

$mail->addAddress("abhshrestha@growbydata.com");
    $mail->addAddress("rmali@growbydata.com");

The problem is that there might exist some "invalid" emails among these recipients. For eg. "rakesh@idontcare.com".

It seems that whenever phpMailer encounters these type of addresses, it doesn't send emails to any other "valid" addresses as well.

How can I make PhpMailer work such that "valid" emails receive emails like in Gmail.

Any help is very much appreciated. Thanks.

  • 写回答

1条回答 默认 最新

  • dongxuying7583 2018-02-09 09:08
    关注

    In order to check if the PHPMAILER is getting some error.

    You should use this two lines in order to be able to see the result of PHPMailer about errors.

    mail->SMTPDebug = SMTP::DEBUG_SERVER;
    $mail->SMTPDebug = 2; //Alternative to above constant
    

    If you get some problem to read the information you can pass the output were and I try to help to analyse.

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100