PHP邮件无法正常工作

时间:2012-02-17 05:47:30

标签: php html css email

在我的业余时间为当地的身体修改客厅做一些快速的网络开发工作;我正在尝试设置一个联系我们表单,除非我点击提交,它只返回die();方法。我不太确定我做错了什么,因为我习惯于编写Java,我只是得到了错误的方法。哈哈。如果有人能够帮助我,我的代码如下所示,问题区域在“联系”案例中。

<html>
<head>
<title>Darklite Tattoo & Piercing</title>
<link href="src/style.css" type="text/css" rel="stylesheet" /></link>
</head>
<body bgcolor="000048" />
<?php include("src/config.php"); ?>
  <div align="center" /><img src="src/img/darklite_banner.jpg" /><br />
    <div id="main" />
        <?php echo("<a href=\"index.php\" />Home</a> &middot; <a href=\"index.php?x=profiles\" />Profiles</a> &middot; <a href=\"index.php?x=services\" />Services</a> &middot; <a href=\"index.php?x=contact\" />Contact Us</a>"); ?>
            <hr>
            <?php
                $x = $_GET['x'];
                switch($x) {
                    case "profiles":
                        echo("Le profiles.");
                    break; case "services":
                        echo("Body piercings and tattoo's.");
                    break; case "contact":
                        if ($_POST["email"]<>'') { 
                            $ToEmail = 'ihatejava@hotmail.com'; 
                            $EmailSubject = 'Site contact form '; 
                            $mailheader = "From: ".$_POST["email"]."\r\n"; 
                            $mailheader .= "Reply-To: ".$_POST["email"]."\r\n"; 
                            $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
                            $MESSAGE_BODY = "Name: ".$_POST["name"]."<br>"; 
                            $MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>"; 
                            $MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"])."<br>"; 
                            mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure"); 
                        ?> 
                        Your message was sent
                        <?php 
                        } else { 
                        ?> 
                        <form action="index.php?x=contact" method="post">
                        <table width="400" border="0" cellspacing="2" cellpadding="0">
                        <tr>
                        <td width="29%" class="bodytext">Your name:</td>
                        <td width="71%"><input name="name" type="text" id="name" size="32"></td>
                        </tr>
                        <tr>
                        <td class="bodytext">Email address:</td>
                        <td><input name="email" type="text" id="email" size="32"></td>
                        </tr>
                        <tr>
                        <td class="bodytext">Comment:</td>
                        <td><textarea name="comment" cols="45" rows="6" id="comment" class="bodytext"></textarea></td>
                        </tr>
                        <tr>
                        <td class="bodytext">&nbsp;</td>
                        <td align="left" valign="top"><input type="submit" name="Submit" value="Send"></td>
                        </tr>
                        </table>
                        </form> 
                        <?php 
                        }; 
                    break; default:
                        echo("Welcome to Darklite Tattoo & Piercing!");
                    break;
                }
            ?>
    </div>
  </div>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

要通过php发送邮件,您需要一台smtp服务器。您可以在php_ini配置文件中设置它,也可以使用ini_set函数调用mail beofre:

ini_set('sendmail_from', '$from');
ini_set('SMTP', '$smtp_server_address');
ini_set('smtp_port', $smtp_port);
mail(...);

答案 1 :(得分:1)

你是否在localhost上测试它,如果是,那么你必须在localhost中设置一个邮件服务器,一些邮件服务器是sendmailpostfix你必须将其中一个配置到你的本地系统