簡體   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