繁体   English   中英

HTML / PHP联系人表单不会将数据发送到我的gmail

[英]HTML / PHP contact form doesn't send data to my gmail

我一直在尝试使用在这里和其他论坛中找到的解决方案来解决此问题,但似乎没有任何效果。 我对PHP真的很陌生。 我不知道此信息是否相关,但我在byethost.com上托管了我的项目,他们说它支持PHP。

我已经在HTML联系人页面中创建了联系表单,并创建了一个PHP文件来处理数据并将其发送到我的gmail地址。 它说消息已成功发送,但数据从未到达我在gmail上的收件箱。 我尝试过以其他方式更改PHP代码(使用复制粘贴解决方案),但是没有运气。 我也尝试过将其发送到我的hotmail帐户,但是它也不起作用。 谁能帮我?

这是HTML联系人表格:

     <div id="form_wrap">
        <form method="post" action="form_process.php" id="contact_form" class="contact">
                <label>
                    <span>NOME*</span>
                    <input name="nome" type="text" tabindex="1" required>
                </label>

                <label>
                    <span>E-MAIL*</span>
                    <input name="email" type="email" tabindex="2" required>
                </label>

                <label>
                    <span>TELEFONE*</span>
                    <input name="phone" type="tel" tabindex="3" required>
                </label>

                <label>
                    <span>WEBSITE</span>
                    <input name="website" placeholder="http://" type="url" tabindex="4">
                </label>

                <label>
                    <span>MOTIVO DE CONTACTO*</span>
                    <select class="escolha" name="motivo" size="1" tabindex="5" required>
                    <option>Contratá-lo</option>
                    <option>Fazer uma pergunta</option>
                    <option>Dizer olá ou agradecer</option>
                    </select>
                </label>

                <div>
                <label>
                    <span>MENSAGEM*</span>
                    <textarea name="message" tabindex="6" required></textarea>
                </label>
                </div>

                <div>
                <input name="submit" type="submit" value="Enviar" id="submit">
                </div>

        </form>
            <a class="subtop" href="#subtop">&#8211; Voltar ao topo</a> 
        </div>

这是我的form_process.php:

    <?php
if(isset($_POST['submit'])) {

$nome = $_POST['nome'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$website = $_POST['website'];
$motivo = $_POST['motivo'];
$message = $_POST['message'];

$to = "mygmailadress@gmail.com";
$subject = "Site contact form";

$header = "From: ".$fromText."\r\n";
$header .= "Cc: ".$ccText."\n";
$header .= "Reply-To : ".$fromText."\r\n";
$header .= "Return-Path : ".$fromText."\r\n";
$header .= "X-Mailer: PHP\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: text/plain; charset=iso-8859-1\r\n";

mail($to, $subject, $header, $message, "From: " . $nome);
}
if(@mail($emailRecipient, $subject, $message, $headers))
{
  echo "Mail Sent Successfully";
}else{
  echo "Mail Not Sent";
}
?>

为什么要两次发邮件功能?

  1. mail有4个参数,您提供5个参数(从第4份开始,应该在$ header中)
  2. 您的邮件功能中使用的变量$emailRecipient不存在。
  3. “发件人”标头中的内容应该是电子邮件地址(和名称),而不仅仅是非字符串

电子邮件永远不会到达有许多可能的原因。

它可能陷入了垃圾邮件中,被主机阻止,或者您的php.ini文件中的邮件功能可能未正确设置。

但是,从您的代码看来,您似乎错误地使用了mail()函数。

您试图通过两次调用该函数来发送两次电子邮件。 只需调用一次即可:

  if(mail(...)) {
    echo 'good times';
    } else {
    echo 'boo, no email was sent';
    }

其次,您使用的功能不正确。 根据此处的文档,mail函数采用以下五个参数:

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

$additional_headers$additional_parameters是可选的,如[方括号]所示。 $to$subject$message依次排列。

第三,我强烈建议不要使用内置的mail()函数。

我建议使用SwiftMailer 这是一个功能全面的PHP库,它将照顾您。

您尝试使用错误的变量名两次发送邮件。

该代码对我有用。

<?php
    if(isset($_POST['submit'])) {

    $nome = $_POST['nome'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $website = $_POST['website'];
    $motivo = $_POST['motivo'];
    $message = $_POST['message'];

    $to = "asdf@gmail.com";
    $subject = "Site contact form";

    $header = "From: ".$email."\r\n";
    $header .= "Cc: ".$email."\n";
    $header .= "Reply-To : ".$email."\r\n";
    $header .= "Return-Path : ".$email."\r\n";
    $header .= "X-Mailer: PHP\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: text/plain; charset=iso-8859-1\r\n";

    if(mail($to, $subject, $message, $header))
    {
      echo "Mail Sent Successfully";
    }else{
      echo "Mail Not Sent";
    }

    }
    ?>

如果您使用的是免费托管,则它们可能会限制您发送电子邮件的能力。 这样的事情正在发生:

https://www.freehosting.com/client/knowledgebase.php?action=displayarticle&id=25

PHP邮件发送功能仅限于免费帐户,以防止滥用。 为了使其正常工作,应将您的邮件脚本配置为使用SMTP服务器“ cpanel.freehosting.com”,并使用在cpanel中设置的电子邮件帐户的凭据对其进行身份验证。 付费帐户可以不受限制地访问PHP邮件功能。

您可以在以下链接中找到有关在PHP脚本中设置电子邮件身份验证的更多信息: http : //email.about.com/od/emailprogrammingtips/qt/PHP_Email_SMTP_Authentication.htm

(可选)可以通过在此处购买相应的插件来启用PHP mail()。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM