2012-04-05 99 views
1

我有一个表单,我想用它来收集用户信息。我有一个PHP获取信息和存储在变量中。当用户点击提交按钮表单提交,并成功地重定向到谢谢页面。 Bu出于某种原因,我没有按预期收到电子邮件。有人可以解释我做错事的地方吗?使用php和html格式发送电子邮件

下面是我的HTML和PHP文件。我将在稍后添加JavaScript验证。在这一刻,我只是想让表单提交工作。

PHP

<?php 

//This page should not be accessed directly. Need to submit the form. 
if(!isset($_POST['submit'])){ 
echo "ERROR. This page cannot be accessed directly"; 
} 

//Variables from the form 
$name    = $_POST['fullname']; 
$ageGroup   = $_POST['ageGroup']; 
$gender    = $_POST['gender']; 
$visit    = $_POST['visit']; 
$purposeOfVisit  = $_POST['purposeOfVisit']; 
$otherReferrer  = $_POST['otherReferrer']; 
$member    = $_POST['member']; 
$socialMedia  = $_POST['socialMedia']; 
$difficulties  = $_POST['difficulties']; 
$easeOfUse   = $_POST['easeOfUse']; 
$whatDifficulties = $_POST['whatDifficulties']; 
$specialCondition = $_POST['specialCondition']; 
$browser   = $_POST['browser']; 
$preferredFormat = $_POST['preferredFormat']; 
$contentsForWebsite = $_POST['contentsForWebsite']; 
$oldContents  = $_POST['oldContents']; 
$expectations  = $_POST['expectations']; 

$message = "Fullname:"; // will compose later 

// Compose email 
$email_from = "[email protected]"; 
$email_subject = "Infinity User Questionnaire"; 
$email_body = "You have received a new message from the user $name.\n". 
"Here is the message:\n $message". 

$to = "[email protected]"; 

$headers = "From: $email_from \r\n"; 

//Send the email! 
mail($to,$email_subject,$email_body,$headers); 

//done. redirect to thank-you page. 
header('Location: thankyou.html'); 


if(IsInjected($visitor_email)) 
{ 
    echo "Bad email value!"; 
    exit; 
} 

// Function to validate against any email injection attempts 
function IsInjected($str) 
{ 
    $injections = array('(\n+)', 
      '(\r+)', 
      '(\t+)', 
      '(%0A+)', 
      '(%0D+)', 
      '(%08+)', 
      '(%09+)' 
     ); 
    $inject = join('|', $injections); 
    $inject = "/$inject/i"; 
    if(preg_match($inject,$str)) 
    { 
    return true; 
    } 
    else 
    { 
    return false; 
    } 
} 
?>  

,如果你愿意,你可以下载的index.php here

非常感谢。

+1

是在网站上或您的本地网站上的网站? – Kristian 2012-04-05 23:29:46

+0

确定'$ _POST'中没有任何内容后,您不会阻止加载代码。您只能添加警告消息。始终启用error_reporting。 – PeeHaa 2012-04-05 23:35:00

+0

它在我的本地主机上。 – Subash 2012-04-05 23:36:50

回答

0

的问题是与我的托管服务。我不得不给他们一个电话,他们为我修好了。

+0

请[将此答案标记为接受的答案](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work ),这样这个问题就不会显示为“未答复”。 – Xenon 2012-04-25 10:24:25

0

我建议你使用的PHPMailer从http://phpmailer.worxware.com/发送邮件。使用PHPMailer,您可以跟踪错误,以HTML和TEXT发送邮件,更改编码和管理单词warpers。

而且,有人说,你的脚本并不妨碍它被直接执行,我认为这头必须用刚换行字符完成“\ n”,而不是回到马车“\ r”。

+1

虽然PHPMailer很好,但它并没有回答原来的问题。此外,PHPMailer仍然使用'mail()'... – Xenon 2012-04-05 23:52:00

3

由于这是在本地机器上,你需要在php.ini首先配置mail()。例如,你可以将其配置为使用ISP的SMTP服务器:

[mail function] 
SMTP = smtp.isp.net 
smtp_port = 25 
sendmail_from = [email protected] 

欲了解更多信息,请参阅PHP Documentation

编辑:我只是测试你对我的一个服务器代码,并如预期发送的电子邮件。这进一步导致我相信你如何配置mail()函数有什么问题。