2013-10-08 43 views
0

我一直在使用PHP郵件表單一段時間,現在它不能在新網站上工作。PHP郵件導致錯誤:「header missing」

以下錯誤結果:

PHP Warning: mail() [function.mail]: "sendmail_from" not set in php.ini or custom "From:" header missing in E:\home\ -mypagefoldername- \Web\form.php on line 16

這是form.php的文件中的PHP代碼:

<? 
$mensagem = "Nome: ".$_POST['nome']." \n"; 
//email que o usuário preencheu 
$mensagem .= "Email: ".$_POST['email']." \n"; 
$mensagem .= "Assunto: ".$_POST['assunto']." \n"; 
$mensagem .= "Telefone: ".$_POST['fone']." \n"; 
$mensagem .= "Endereço: ". $_POST['mensagem']; 
$headers = ""; 
//email do seu domínio hospedado 
$emailsender = "[email protected]"; 
//email de quem vai receber 
$emaildestinatario = "[email protected]"; 
$assunto = "Mensagem do Parceiro Pipa"; 

// Envio dos dados do Formulário para seu e-mail: 
if(!mail($emaildestinatario, $assunto, $mensagem, $headers ,"-r".$emailsender)) 

{ 
// Se for Postfix - hospedagem linux 
$headers .= "Return-Path: " . $emailsender . $quebra_linha; 
} 
echo ' 
<script type="text/JavaScript"> 
alert("Email sent! Thank you!"); 
location.href="index.html" 
</script> 
'; 

?> 

很抱歉的葡萄牙語。

表單頁面是用下面的表單代碼一個簡單的HTML頁面:

<form method="post" action="form.php"> 

        <label>Nome</label> 
        <input name="nome" placeholder="Nome completo."> 

        <label>Telefone</label> 
        <input name="fone" placeholder="Número de telefone com DDD.">      

        <label>Assunto</label> 
        <input name="assunto" placeholder="Qual o motivo do seu contato?">      

        <label>Mensagem</label> 
        <textarea name="mensagem" placeholder="Escreva aqui sua mensagem."></textarea> 



        <label>Email</label> 
        <input name="email" type="email" placeholder="Email para contato."> 

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

</form> 

可能有人請幫助我嗎? =/

感謝您的耐心等待!

回答

0

如上所述,您可能會遺漏$headers變量中的From:聲明!

<?php 
    $From = "[email protected]"; 
    $headers .= "From: Your Name <". $From .">\n"; 
?> 

到您的$headers字符串的頂部附近。雖然爲了將來的參考,你可能想看看PHPMailer Class,一個很好的&安全類,可以有效地處理PHP mail

PHPMailer Github

+0

嘗試完全像這樣使用它,但仍會導致相同的錯誤。 –

+0

你試過把它放在頂部嗎?因爲否則你會覆蓋'$ headers'; o或者把'='改成'。=' – MackieeE

0

您沒有設置頭,這是造成這個問題,正如下面的文檔中說明。

Note:

When sending mail, the mail must contain a From header. This can be set with the additional_headers parameter, or a default can be set in php.ini.

Failing to do this will result in an error message similar to Warning: mail(): "sendmail_from" not set in php.ini or custom "From:" header missing. The From header sets also Return-Path under Windows.

http://php.net/manual/en/function.mail.php

在你的情況,你應該使用;

$headers = "From: [email protected]" . "\r\n"; 
+0

好吧,我試過這樣做。我注意到'$ emailsender'部分之前有一個'$ headers =「」',並且我編輯了它,就像你的例子。是它嗎?恐怕還是會出現同樣的錯誤。 =/ –

+0

我認爲你的「if(!mail())」行有錯誤,正確的用法是「mail($ to,$ subject,$ message,$ headers);」 – BrownEyes

0

請php.ini文件更改以下設置

啓用電子郵件您應該取消註釋以下行或在該行

;sendmail_from = 

的開頭刪除分號並更改該設置爲這個例子(如果本地主機)

sendmail_from = [email protected] 

如果你在一個產品您必須根據您的域更改默認地址,因此[email protected]

+0

我如何到達我的php.ini文件?我應該聯繫我的託管服務提供商嗎?還是應該自己去嘗試自己做?謝謝你的幫助! –