如何为自定义选择表单制作自动回复电子邮件

时间:2015-04-02 19:50:21

标签: php html forms email

我在“我的首页”上使用自定义optin表单

我想添加一封自动回复确认电子邮件,发送给提交表单的所有人。这是目前在mail-script.php中使用的代码。

<?php
/* Set e-mail recipient */

$myemail  = "email@gmail.com";

/* Check all form inputs using check_input function */
$fname    = check_input($_POST['fname'], "Enter your first name");
$lname    = check_input($_POST['lname'], "Enter your last name");
$email    = check_input($_POST['email']);
$phone    = check_input($_POST['phone']);
$altphone = check_input($_POST['altphone']);
$resort   = check_input($_POST['resort']);
$amount   = check_input($_POST['amount']);
$call     = check_input($_POST['call']);

/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
    show_error("E-mail address not valid");
}

/* If URL is not valid set $website to empty */
if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i", $website))
{
    $website = '';
}
/* Lets Save in txt file */
$text = "$fname\n$lname\n$email\n$phone\n$resort\n$amount\n$call\n\n";
file_put_contents('secretfilenae.txt', $text, FILE_APPEND);
/* Let save in cvs file */
$fields = array($fname,$lname,$email,$phone,$resort,$amount,$call);

$fp = fopen('secretfilenae.csv', 'a');
fputcsv($fp, $fields);
fclose($fp);
/* Let's prepare the message for the e-mail */
$message = "Hello!

Your contact form has been submitted by:


First Name : $fname
Last Name : $lname
E-mail: $email
Phone : $phone
Alt Phone : $altphone
Resort: $resort
Amount: $amount
Call  : $call

End of message
";

/* Send the message using mail() function */
mail($myemail, $subject, $message);

/* Redirect visitor to the thank you page */
header('Location: index.html');
exit();

/* Functions we used */
function check_input($data, $problem='')
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
        show_error($problem);
    }
    return $data;
}

function show_error($myError)
{
?>
if (strtolower($_POST['code']) != 'mycode') {die('Wrong access code');}
    <html>
    <body>

    <b>Please correct the following error:</b><br />
    <?php echo $myError; ?>

    </body>
    </html>
<?php
exit();
}
?>

1 个答案:

答案 0 :(得分:0)

使用php mail()功能:

mail($myEmail, $headerForEmail, $message);