You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP联系表单故障求助:多年正常运行,现显示发送成功却未收到邮件

Hey there, let's dig into why your PHP contact form is showing the Danish "mail sent" message but you're not actually receiving anything. Since it worked flawlessly for years, this is almost always a server-side change rather than a code issue—but let's cover all bases step by step:

1. First, Verify if PHP's mail() Function Actually Works

Hosts often tweak email services over time—they might disable unauthenticated mail() calls, or change their mail server settings entirely. Let's test with a simple standalone script to rule this out:

<?php
// Replace these with your actual email and domain details
$to = "your-personal-email@example.com";
$subject = "PHP Mail Test";
$message = "If you get this, the mail() function is working!";
$headers = "From: noreply@your-domain.dk" . "\r\n" .
           "Reply-To: noreply@your-domain.dk" . "\r\n" .
           "X-Mailer: PHP/" . phpversion();

// Enable error reporting temporarily to catch issues
error_reporting(E_ALL);
ini_set('display_errors', 1);

if(mail($to, $subject, $message, $headers)) {
    echo "Test email sent successfully!";
} else {
    echo "Test email failed to send. Check error messages above.";
}
?>

Upload this to your server and run it. If you don't get the test email, or if errors pop up, your server's native mail() function is broken—this is the most likely culprit.

2. Check if Your Form Code Ignores mail()'s Return Value

It sounds like your current code just shows the "success" message no matter what, even if mail() fails. Look at your kontakt.php handling code—does it look something like this?

// Example of bad (common) code
if(isset($_POST['navn'])) {
    // Collect form data...
    mail($to, $subject, $message, $headers); // No check for success!
    echo "邮件已发送"; // Your Danish success message
}

If so, you're blindly showing success even when the mail fails. Fix this by checking the return value of mail():

$mailSent = mail($to, $subject, $message, $headers);
if($mailSent) {
    echo "邮件已发送";
} else {
    echo "Beklager, der opstod en fejl. Prøv igen senere."; // Danish error message
    // Log the error for debugging
    error_log("Contact form mail failed: " . print_r(error_get_last(), true));
}

This will at least tell you if the function is failing, instead of giving a false success.

3. Check Spam Folders (Yes, Really)

Sometimes the email does get sent, but your inbox's spam filter flags it. Double-check your spam/junk folder for messages from your form's sender address. To reduce this risk, always use a sender email tied to your domain (e.g., noreply@your-domain.dk) instead of a free Gmail/Outlook address—those are way more likely to get filtered.

4. Switch to SMTP (The Reliable Long-Term Fix)

Most modern hosts now block unauthenticated mail() calls to cut down on spam. Using SMTP with a library like PHPMailer is way more reliable. Here's a quick example:

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

// Include PHPMailer files (download and place them in your site directory first)
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

try {
    $mail = new PHPMailer(true);

    // SMTP Server Settings (get these from your host's control panel)
    $mail->isSMTP();
    $mail->Host = 'smtp.your-domain.dk';
    $mail->SMTPAuth = true;
    $mail->Username = 'noreply@your-domain.dk'; // Your domain email
    $mail->Password = 'your-email-password'; // Your email's password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
    $mail->Port = 465;

    // Recipient & Sender
    $mail->setFrom('noreply@your-domain.dk', 'Kontakt Formular');
    $mail->addAddress('your-personal-email@example.com', 'Dit Navn');

    // Email Content
    $mail->isHTML(true);
    $mail->Subject = 'Ny Besked Fra Kontakt Formular';
    $mail->Body = "Navn: {$_POST['navn']}<br>Email: {$_POST['email']}<br>Besked: {$_POST['besked']}";
    $mail->AltBody = "Navn: {$_POST['navn']}\nEmail: {$_POST['email']}\nBesked: {$_POST['besked']}";

    $mail->send();
    echo "邮件已发送";
} catch (Exception $e) {
    echo "Beklager, besked kunne ikke sendes: {$mail->ErrorInfo}";
    error_log("PHPMailer Error: {$mail->ErrorInfo}");
}
?>

You'll need to download PHPMailer's latest files and place them in your site directory—this eliminates the risks of unauthenticated mail() calls and gives you detailed error messages if something goes wrong.

5. Contact Your Host Provider

If none of the above works, reach out to your host's support team. Tell them your PHP contact form stopped sending emails recently, even though it worked for years. Ask them to check the server's mail logs—they'll be able to see if emails are being blocked, rejected, or stuck in a queue.

内容的提问来源于stack exchange,提问作者LoveCod

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:34:23