简体   繁体   English

提交表单后发送电子邮件

[英]send email after form submission

I am working on a script for a simple mailing list signup on a splash page and I am having some issues sending a welcome/thanks email to the email address entered in the form. 我正在为启动页面上的简单邮件列表注册编写脚本,并且在向表中输入的电子邮件地址发送欢迎/感谢电子邮件时遇到了一些问题。

My code: PHP 我的代码: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);

    }
?>

My Code: the form 我的代码:表格

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

When I submit an email address, it submits to the database, however the email is never sent to the recipient. 当我提交电子邮件地址时,它会提交到数据库,但是电子邮件永远不会发送给收件人。

See http://www.indierally.com to see exactly what I am talking about 访问http://www.indierally.com,以准确了解我在说什么

Your $to variable isn't getting set thus PHP doesn't know where to send the email. 您的$to变量未设置,因此PHP不知道将电子邮件发送到何处。 If you changed your code to be: 如果您将代码更改为:

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

You would see the failed message. 您将看到失败的消息。 Perhaps you should replace $to with $email ? 也许您应该将$to替换$to $email

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM