为什么 php - 如果 ($mail->send()) 不能正常工作
Why php - if ($mail->send()) is not working properly
我已尝试将此 php 代码用于网站联系我们页面。字段验证的错误消息工作正常。但是,由于发送了错误的消息,它无法正确处理 $result 消息。
<?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>';
}
}
}
?>
你能找出这段代码的错误吗?
提前致谢。
您添加了错误的消息。
在 if ($mail->send()) {
上,您返回了错误消息。
将检查更改为 !$mail->send()
就是这样。
if (!$mail->send()) {
如果这不起作用,请尝试调试:
if(!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
else {
echo "Message has been sent successfully";
}
尝试使用 ErrorInfo
函数打印 error
if(!$mail->Send()) {// Send not send
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
我已尝试将此 php 代码用于网站联系我们页面。字段验证的错误消息工作正常。但是,由于发送了错误的消息,它无法正确处理 $result 消息。
<?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>';
}
}
}
?>
你能找出这段代码的错误吗?
提前致谢。
您添加了错误的消息。
在 if ($mail->send()) {
上,您返回了错误消息。
将检查更改为 !$mail->send()
就是这样。
if (!$mail->send()) {
如果这不起作用,请尝试调试:
if(!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
else {
echo "Message has been sent successfully";
}
尝试使用 ErrorInfo
函数打印 error
if(!$mail->Send()) {// Send not send
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}