php - 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">×</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';
}
推荐阅读
- sql - Oracle SQL - 如何使用 JSP 调用 ODCI 流水线函数
- excel - 在用户窗体中复制文本框
- oracle - Oracle DB 未声明为“正确”
- vue.js - 在发出一个对象时,所有对象的所有项都被推入所有数组
- python - 用语言标记开头的```
- android-studio - 为什么Android Studio 3.2.1 向导创建的项目不需要导入info.dodata.clipboard.R?
- c# - 为数据创建新的控件集的应用程序
- node.js - Node.js TCP 套接字服务器数据缓冲区排序
- php - 在 Woocommerce 中的特定电子邮件通知上为特定产品添加自定义文本
- android - 如何使用 RecyclerView 适配器的数据并按参数将它们分成不同的组?