douguaidian8021 2016-08-11 11:39 采纳率: 100%
浏览 267
已采纳

为什么php - if($ mail-> send())无法正常工作

I have tried to use this php code for a web site contact us page. Error messages for field validation are working fine. However its not working the $result messages properly as its sending the wrong message.

<?php
    if (isset($_POST["submit"]) && $_POST["submit"] == 'Send')
    {

        $name = isset($_POST['name']) ? $_POST['name'] : '';
        $email = isset($_POST['email']) ? $_POST['email'] : '';
        $subject = isset($_POST['subject']) ? $_POST['subject'] : '';
        $message = isset($_POST['message']) ? $_POST['message'] : '';
        $human = isset($_POST['human']) ? intval($_POST['human']) : '';
        $error = array();

        // Check if name has been entered
        if (empty($name)) {
            $error['name'] = 'Please enter your name';
        }

        // Check if email has been entered and is valid
        if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $error['email'] = 'Please enter a valid email address';
        }

        //Check if message has been entered
        if (empty($message)) {
            $error['message'] = 'Please enter your message';
        }

        //Check if simple anti-bot test is correct
        if ($human !== 5) {
            $error['human'] = 'Your anti-spam is incorrect';
        }

        // If there are no errors, send the email
        if (empty($error)) {
            $mail = new PHPMailer;

            $mail->isSMTP();
            $mail->SMTPDebug = 3;
            $mail->setFrom('from@example.com', 'Mailer');
            $mail->addAddress('example@example.net', 'User');


            $mail->Subject = $_POST['subject'];
            $mail->Body    = $_POST['message'];

            // From here the code is not working properly
            if ($mail->send()) {
                $result = '<div class="alert alert-success">Sorry there is a problem by sending your message. Please try again later.</div>';
            } else {
                //echo 'Mailer Error: ' . $mail->ErrorInfo;exit;
                $result = '<div class="alert alert-success">Thank you for your message and we will get back to you as soon as possible.</div>';
            }  
        }
    }
?>

Can you identify the error of this code?

Thanks in advance.

  • 写回答

2条回答 默认 最新

  • doulei1965 2016-08-11 11:40
    关注

    you have added incorrect message. on if ($mail->send()) { you have returned error message.

    change the check to !$mail->send() that's it.

    if (!$mail->send()) {
    

    If that doesn't work, try this to debug:

        if(!$mail->send()) {
            echo "Mailer Error: " . $mail->ErrorInfo;
        } 
        else {
            echo "Message has been sent successfully";
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求MCSCANX 帮助
  • ¥15 机器学习训练相关模型
  • ¥15 Todesk 远程写代码 anaconda jupyter python3
  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开