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

PHP联系表单突然无法发邮件,代码未改动需排查

Fixing Your Broken PHP Contact Form

Hey Sergi, let's break down why your once-working contact form stopped sending emails—since you're new to PHP, I'll keep this clear and actionable.

The Main Culprit: Deprecated PHP Variables

Your code uses $HTTP_POST_VARS, which is a super old, deprecated PHP variable—it was removed way back in PHP 5.4 (released in 2012!). Modern PHP uses the $_POST superglobal instead to access form data. This is almost certainly why your form stopped working when your server updated PHP.

Other Hidden Issues

There are a couple more bugs in your mail_cast.php that would break functionality even if $HTTP_POST_VARS still worked:

  • You reference a $mail variable in the Reply-To header, but it's never defined (you need to grab the user's email from the form).
  • Email headers require \r\n line breaks (not just newlines) to be compliant—using regular newlines can cause mail servers to reject the message.
  • Your loop runs 4 times, but your form only has 3 fields (Nombre, Email, Mensaje) plus the submit button—this will cause an undefined index error.

Fixed Code for mail_cast.php

Here's the fully updated, working version with comments explaining changes:

<?php
// Initialize message content
$msg = "Ha recibido un comentario en la web\n\n";

// Use modern $_POST superglobal instead of deprecated $HTTP_POST_VARS
foreach ($_POST as $field_name => $value) {
    // Skip the submit button field
    if ($field_name !== 'submit') {
        // Clean input to prevent unwanted HTML
        $msg .= ucfirst($field_name) . ": " . strip_tags($value) . "\n\n";
    }
}

// Recipient email (double-check this is correct for your server)
$recipient = "contacturl@mail";
// Email subject
$subject = "formulario web";

// Get user's email from form (fixes undefined $mail variable)
$user_email = isset($_POST['Email']) ? $_POST['Email'] : '';

// Properly formatted email headers with compliant line breaks
$mailheaders = "From: X <noreply@yourdomain.com>\r\n"; // Use a valid sender email, not a URL
$mailheaders .= "Reply-To: " . $user_email . "\r\n";
$mailheaders .= "Content-Type: text/plain; charset=UTF-8\r\n"; // Add charset for special characters

// Send the email and check if it succeeded
$mail_sent = mail($recipient, $subject, $msg, $mailheaders);

// Redirect to thank you page (only if email was sent, better UX)
if ($mail_sent) {
    header('Location: http://www.url.com/cast/gracias.html');
    exit;
} else {
    // Optional: Show error message if mail fails
    echo "Lo sentimos, hubo un error al enviar tu mensaje. Por favor intenta nuevamente.";
}
?>

Quick HTML Form Improvements

Your HTML is mostly fine, but adding required attributes to mandatory fields helps users fill them out correctly:

<form action="http://url/mail_cast.php" method="post" id="contacto">
    <br />
    <table width="208" align="center">
        <tr>
            <td width="44" valign="top" class="Estilo24">Nombre</td>
            <td width="147" class="Estilo72"><input name='Nombre' type='text' class="Estilo73" size="22" required /></td>
            <td width="1" rowspan="4" class="Estilo72">&nbsp;</td>
        </tr>
        <tr>
            <td valign="top" class="Estilo24">Email</td>
            <td height="5" valign="top" class="Estilo72"><input name='Email' type='email' class="Estilo73" size="22" required /></td>
        </tr>
        <tr>
            <td valign="top" class="Estilo24">Mensaje</td>
            <td colspan="2" class="Estilo72"><textarea name='Mensaje' cols="17" rows="7" class="Estilo73" required></textarea></td>
        </tr>
        <tr>
            <td colspan="3" align="center" class="Estilo72">
                <input name="submit" type='submit' class="Estilo73" value='Enviar mensaje' />
            </td>
        </tr>
    </table>
    <br />
    Los datos facilitados al enviar al rellenar el formulario formarán parte de la Base de Datos de la Clínica, para más información consultar el <a href="../cat/aviso.html">Aviso legal</a> de nuestra web.
</form>

Debugging Tips If It Still Doesn't Work

  • Check your server's PHP error log (ask your hosting provider if you don't know where it is)—it will show exactly what's broken.
  • Test if the mail() function works on your server: create a simple test script with just mail('your-email@example.com', 'Test', 'Hello') and see if you get the email.
  • Add var_dump($_POST); at the top of mail_cast.php to confirm form data is being sent correctly.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 06:57:50