PHP Mailer发送的数据错误不被接受

时间:2019-02-20 12:33:31

标签: php smtp phpmailer

PHPMailer中出现以下错误:

  

SMTP错误:不接受数据。SMTP服务器错误:DATA END命令   失败的详细信息:   STOREDRV.Submission.Exception:SendAsDeniedException.MapiExceptionSendAsDenied;   由于消息的永久异常,无法处理消息   无法提交消息。

我正在使用以下内容:

public function sendEmail($conn, $user, $to_email, $subject, $messageToSent)
    {
        //Check if user exists
        $exist = $this->checkIfUserExist($conn, $user);
        $from = $this->getUserEmail($conn, $user);
        if($exist['exist'])
        {
            // Please specify your Mail Server - Example: mail.example.com.
            //$mail = new PHPMailer\PHPMailer();
            $mail = new PHPMailer\PHPMailer\PHPMailer;                // Passing `true` enables exceptions
            $message = "success";
            try {
                //Server settings
                $mail->SMTPDebug = 2;                                 // set it to 2 to Enable verbose debug output
                $mail->isSMTP();                                      // Set mailer to use SMTP
                $mail->Host = 'smtp.office365.com';                   // Specify main and backup SMTP servers
                $mail->SMTPAuth = true;                               // Enable SMTP authentication
                if($from=='' || $from==null || $from=="NULL")
                {

                    $mail->setFrom('i@abc.org');
                }
                if($from!='')
                {

                    $mail->setFrom($from);
                }
                $mail->Username = 'i@abc.org';     // SMTP username
                $mail->Password = 'xyz';                         // SMTP password
                $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
                $mail->Port = 587;                                    // TCP port to connect to
                //$mail->AuthType = 'PLAIN';
                //Recipients
                //$mail->setFrom($user.'@abc.com');
                //$mail->setFrom($from);
                $mail->addAddress('i@abc.org');     // Add a recipient
                //$mail->addAddress('ellen@example.com');               // Name is optional
                $mail->addReplyTo('info@example.com', $subject);
                //$mail->addCC('cc@example.com');
                //$mail->addBCC('bcc@example.com');

                //Attachments
                //$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
                //$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

                //Content
                $mail->isHTML(true);                                  // Set email format to HTML
                $mail->Subject = $subject;
                if($from=="i@abc.org")
                {
                    $mail->Body = $messageToSent. '<p>The user asking for password recovery does not have a valid email. Thus, the sender will be shown as sent from the admin email. The user have the following ID: </p><h3>'.$exist['user_id'].'</h3>';
                }
                else
                {
                    $mail->Body = $messageToSent. '<p>The user have the following ID: </p><h3>'.$exist['user_id'].'</h3>';

                }
                $mail->AltBody = 'Please take actions according to needs.';

                if($mail->send())
                {
                    echo json_encode($message);
                }
                else
                {
                    echo json_encode($mail->ErrorInfo);
                }

            } catch (Exception $e) {
                echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
            }
        }
        else
        {
            echo json_encode("UserDoesntExist"); 
        }

    }

我已经读过here$mail->Username$mail->setFrom应该相同,但是通过这种方式,我们应该为每封更改$mail->Password的电子邮件获取密码。

1 个答案:

答案 0 :(得分:2)

线索以异常的名义出现:SendAsDenied;这表示您不能将用户名以外的其他任何内容用作发件人地址,尤其是不能使用任意(伪造)地址。

如果要避免伪造问题,请从您的管理员地址发送,但将用户的地址设置为答复地址。这样一来,您就不会虚伪,回复将到达正确的位置。