首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么php - if ($mail->send())不能正常工作

为什么php - if ($mail->send())不能正常工作
EN

Stack Overflow用户
提问于 2016-08-11 19:39:12
回答 2查看 5.7K关注 0票数 1

我曾尝试使用此php代码的网站联系我们的网页。字段验证的错误消息工作正常。然而,它不能正确地工作$result消息,因为它发送了错误的消息。

代码语言:javascript
复制
<?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>';
            }  
        }
    }
?>

你能找出这个代码的错误吗?

提前谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-08-11 19:40:26

您添加了不正确的消息。在if ($mail->send()) {上,您返回了错误消息。

将检查更改为!$mail->send(),就是这样。

代码语言:javascript
复制
if (!$mail->send()) {

如果这样做不起作用,可以尝试这样调试:

代码语言:javascript
复制
    if(!$mail->send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    } 
    else {
        echo "Message has been sent successfully";
    }
票数 3
EN

Stack Overflow用户

发布于 2016-08-11 19:44:30

尝试使用ErrorInfo函数打印error

代码语言:javascript
复制
if(!$mail->Send()) {// Send not send
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38895332

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档