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

PHP联系表单转邮件代码无法发送邮件,请求技术排查

Hey there, let's troubleshoot this PHP mail issue step by step—since your script runs but no emails go through, it's usually one of these common culprits:

1. Verify the mail() function's parameters and basic setup

First, double-check that your mail() call uses valid, host-compliant values:

  • The recipient must be your domain-based email (e.g., support@yourdomain.com), as most shared hosts block emails sent to external addresses unless configured properly.
  • The From address must match your domain too—random or external senders will get flagged or blocked.
  • Add error checking to confirm if the mail() function itself is failing:
    $to = "your@yourdomain.com";
    $subject = "Contact Form Submission";
    $message = "Test message from contact form";
    $headers = "From: contact@yourdomain.com\r\n";
    $headers .= "Reply-To: " . $_POST['email'] . "\r\n";
    $headers .= "Content-Type: text/html; charset=UTF-8\r\n";
    
    if(mail($to, $subject, $message, $headers)) {
        echo "Email sent successfully!";
    } else {
        echo "Mail delivery failed at the function level!";
    }
    

This will tell you if the issue is with the mail() call itself, not just silent failures downstream.

2. Check your host's email restrictions

Most shared hosting providers either disable the native mail() function entirely or require SMTP authentication instead. Here's what to do:

  • Log into your host's control panel (e.g., cPanel) and check the email settings—look for mentions of "SMTP requirements" or "mail function restrictions".
  • If SMTP is required, use a reliable library like PHPMailer instead of mail()—it's more robust and handles authentication properly. Example setup:
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;
    
    require 'vendor/autoload.php'; // Make sure you've installed PHPMailer via Composer
    
    $mail = new PHPMailer(true);
    try {
        // SMTP Server Settings (get these from your host's email docs)
        $mail->isSMTP();
        $mail->Host = 'mail.yourdomain.com';
        $mail->SMTPAuth = true;
        $mail->Username = 'your@yourdomain.com'; // Your domain email address
        $mail->Password = 'your-email-password'; // Your email password
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
        $mail->Port = 465;
    
        // Recipients
        $mail->setFrom('contact@yourdomain.com', 'Website Contact Form');
        $mail->addAddress('your@yourdomain.com', 'Your Name');
    
        // Content
        $mail->isHTML(true);
        $mail->Subject = 'New Contact Form Submission';
        $mail->Body = "Message from: " . $_POST['name'] . "<br>Email: " . $_POST['email'] . "<br>Message: " . $_POST['message'];
    
        $mail->send();
        echo 'Your message has been sent successfully!';
    } catch (Exception $e) {
        echo "Oops, something went wrong. Error: {$mail->ErrorInfo}";
    }
    

PHPMailer will give you specific error messages if authentication fails or the SMTP server rejects the connection.

3. Check if emails are landing in spam

Sometimes emails do send successfully, but get filtered into spam folders. To fix this:

  • Add proper email headers (like Reply-To and Content-Type) as shown in the first section—this helps email providers recognize the message as legitimate.
  • Avoid generic subject lines or overly short messages, which trigger spam filters.
  • Check your domain email's spam folder first—you might find the test submissions there.
4. Review PHP error logs

Your host's PHP error logs will have detailed clues about why emails aren't sending. To access them:

  • In cPanel, go to the "Error Log" section under "Metrics".
  • Or, enable error logging in your script temporarily to capture issues:
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    ini_set('log_errors', 1);
    ini_set('error_log', './php_errors.log'); // Make sure this file is writable
    

Run your form again, then check the log file for entries related to mail() or SMTP connections.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 06:58:46