首页 > 解决方案 > PHP联系表单发送但不接收电子邮件

问题描述

我创建了一个使用 PHP 发送数据的联系表单。该表格似乎可以正常发送,因为没有错误并且出现成功消息,但是我没有将电子邮件接收到指定的收件箱中。

我正在使用 PHPMailer,因为我最初尝试只使用 php 'mail' 命令,我现在明白这有点命中注定。

我不明白为什么我不会收到这些电子邮件,所以我将非常感谢可以提供的任何帮助。

我对 PHP 还很陌生,所以请耐心等待 :)

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

$msg = "";

if (isset($_POST['submit'])) {
    require 'phpmailer/src/Exception.php';
    require 'phpmailer/src/PHPMailer.php';
    require 'phpmailer/src/SMTP.php';

    function sendemail ($to, $from, $body) {
        $mail = new PHPMailer(true);
        $mail->setFrom($from);
        $mail->addAddress($to);
        $mail->Subject = 'Contact Form - Email';
        $mail->Body = $body;
        $mail->IsHTML(false);

        return $mail->send();
    }

    $email = $_POST['email'];
    $subject = $_POST['subject'];
    $body = $_POST['body'];

    if (sendemail('gareth.langley1@gmail.com', $email, $subject, $body)) {
        $msg = 'Email has been sent, thank you!';
    } else 
        $msg = 'Email failed, please try again later';       

}

?>

<title>Test Contact Form Using PHPMailer</title>

<body>

    <div id="contactInnerWrapper">
        <a href="#"><span id="close">&times;</span></a>
        <h1 id="h1contactForm">Get in touch</h1>
            <form method="post" action="index.php">
                <label for="email">Email address:</label><br>
                <input  type="email" name="email"  placeholder="Enter email" id="email">
                <label for="subject">Subject:</label>
                <input  type="text" name="subject" id="subject"><br>
                <label for="body">What would you like to ask us?</label><br>
                <textarea  type="text" name="body" rows="7" id="content"></textarea>
                <button type="submit" name="submit" id="submit">Submit</button>
            </form>
            <br><br>
            <?php echo $msg; ?>
    </div>


</body>

标签: phphtml

解决方案


您将 4 个参数传递给只需要 3 个的函数。

function sendemail ($to, $from, $body)

sendemail('gareth.langley1@gmail.com', $email, $subject, $body)

尝试像这样编辑:

if (sendemail('gareth.langley1@gmail.com', $email, $body)) {
    $msg = 'Email has been sent, thank you!';
} else 
    $msg = 'Email failed, please try again later';       
}

推荐阅读