表格提交后发送电子邮件

时间:2015-01-28 23:53:26

标签: php html mysql forms email

我正在制作一个脚本,用于在启动页面上进行简单的邮件列表注册,并且我在向表单中输入的电子邮件地址发送欢迎/感谢电子邮件时遇到了一些问题。

我的代码:PHP

<?php
    if($_POST['formSubmit'] == "Submit")
    {    
        //email variables
        $subject = "Indie Rally - Welcome & Thanks!";
        $email_message = "Welcome and thanks for joining the Indie Rally Mailing List!\n\n";
        $email_message .= "Your Email: ".clean_string($first_name)." ";
        $email_message .= "Has been added to our mailing list and will be used to give development, site feature updates, and news.\n\n";
        $email_message .= "If at any point you have feedback, ideas, suggestions for features of Indie Rally, feel free to email us any time at admin@indierally.com\n";

        // create email headers
        $headers = 'From: admin@indierally.com' . "\r\n" .
                    'Reply-To: admin@indierally.com' . "\r\n" .
                    'X-Mailer: PHP/' . phpversion(); 

        //Database Connection
        mysql_connect ("localhost", "USERNAME", "PASS") or die ('Error: ' . mysql_error());
        mysql_select_db("DBNAME") or die ('Data error:' . mysql_error());

        $email = PrepSQL($_POST['formEmail']); 
        $query = "INSERT INTO mailinglist (email) VALUES (". $email . ")";

        mysql_query($query) or die ('Error updating database' . mysql_error());
        //success - email & forward
        mail($email, $subject, $email_message, $headers); 
        header("Location: submitted.html");
    }

    // function: PrepSQL()
    // use stripslashes and mysql_real_escape_string PHP functions
    // to sanitize a string for use in an SQL query
    //
    // also puts single quotes around the string
    //
    function PrepSQL($value)
    {
        // Stripslashes
        if(get_magic_quotes_gpc()) 
        {
            $value = stripslashes($value);
        }

        // Quote
        $value = "'" . mysql_real_escape_string($value) . "'";

        return($value);
    }
    function clean_string($string) {

      $bad = array("content-type","bcc:","to:","cc:","href");

      return str_replace($bad,"",$string);

    }
?>

我的代码:表格

    <div class="maillist">
            <?php
                    if(!empty($errorMessage)) 
                    {
                            echo("<p>There was an error with your form:</p>\n");
                            echo("<ul>" . $errorMessage . "</ul>\n");
                    }
            ?>
            <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
                    <p>
                    <input type="text" name="formEmail" maxlength="50" value="<?=$varEmail;?>" placeholder="Email Me Updates!" />
                    <input type="submit" name="formSubmit" value="Submit" />
                    </p>
            </form>
    </div>

当我提交电子邮件地址时,它会提交到数据库,但电子邮件永远不会发送给收件人。

请参阅http://www.indierally.com,了解我正在谈论的内容

1 个答案:

答案 0 :(得分:2)

您的$to变量未设置,因此PHP不知道将电子邮件发送到何处。如果您将代码更改为:

if( !mail($to, $subject, $email_message, $headers) ) {
    echo 'error here...';
} else {
    header("Location: submitted.html");
}

您会看到失败的消息。也许您应该将$to替换为$email