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

时间:2018-02-07 06:21:21

标签: php phpmailer

我使用phpMailer发送电子邮件。 电子邮件收件人作为数组传递,并通过循环遍历此数组来添加。例如:

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;
    }
}

地址设置为静态的收件人正在发送和接收电子邮件。例如。

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

问题是可能存在一些&#34;无效&#34;这些收件人之间的电子邮件。例如。 "rakesh@idontcare.com"

似乎每当phpMailer遇到这些类型的地址时,它都不会向其他任何地方发送电子邮件&#34;有效的&#34;地址也是如此。

我如何让PhpMailer工作,以便&#34;有效&#34;电子邮件会接收Gmail等电子邮件。

非常感谢任何帮助。感谢。

1 个答案:

答案 0 :(得分:0)

为了检查PHPMAILER是否出现错误。

你应该使用这两行,以便能够看到PHPMailer关于错误的结果。

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

如果您在阅读信息时遇到问题,可以通过输出,我会尝试帮助分析。