我已经使用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>
答案 0 :(得分:0)
您要将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';
}
答案 1 :(得分:0)
您的服务器很可能没有安装或安装了邮件传输代理(MTA),但未正确配置。适用于Linux的MTA示例:sendmail,postfix,exim4,qmail。
安装和配置MTA可能会变得很复杂。
您可以从同一服务器上的命令行发送电子邮件吗?