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

为什么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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵