繁体   English   中英

PHP联系表单发送但不接收电子邮件

[英]PHP contact form sending but not receiving emails

我已经使用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>

您要将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';       
}

您的服务器很可能没有安装或安装了邮件传输代理(MTA),但未正确配置。 适用于Linux的MTA示例:sendmail,postfix,exim4,qmail。

安装和配置MTA可能会变得很复杂。

您可以从同一服务器上的命令行发送电子邮件吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM