Web表单发送电子邮件

Web表单发送电子邮件

本文介绍了PHP Web表单发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用PHP设置了一个电子邮件Web表单.该表格可以正常工作并发送电子邮件.但不是重定向,而是出现以下错误:error;您需要提交表格!警告:无法修改标头信息-行70上/home/con17120/public_html/form-to-email.php中已经发送过的标头(输出始于/home/con17120/public_html/form-to-email.php:5). ",尽管该表单仍然可以正常工作,但它不会重定向.

I have set up an email web form in PHP. The form works correctly and sends the email. but rather than redirecting it comes up with this error: error; you need to submit the form!Warning: Cannot modify header information - headers already sent by (output started at /home/con17120/public_html/form-to-email.php:5) in /home/con17120/public_html/form-to-email.php on line 70" although the form still works fine it just won't redirect.

...谢谢HTML:

.....ThanksHTML:

     <form method="post" name="myemailform" action="form-to-email.php">

    <h1>June/July Crops: </h1>
    <h2><input type="checkbox" class="form cushycms" value="yes" name="check1">Pink Lady Apples  2kg <b>$7.00</b></h2>
    <h2><input type="checkbox" class="form cushycms" value="yes" name="check2">Seedless Imperials Mandarines 1kg <b>$4.00</b></h2>
    <h2><input type="checkbox" class="form cushycms" value="yes" name="check3">Bananas 1kg <b>$4.00</b></h2>
    <h2><input type="checkbox" class="form cushycms" value="yes" name="check4">Seedless Thompson Grapes 1kg <b>$6.00</b></h2>
    <h2><input type="checkbox" class="form cushycms" value="yes" name="check5">Strawberries <b> TBA</b></h2>
    <h2><input type="checkbox" class="form cushycms" value="yes" name="check6">Cherries <b> TBA</b></h2>
    <h2><input type="checkbox" class="form cushycms" value="yes" name="check7">Pistachio Nuts 1/4kg <b>$5.00</b> </h2>
    <h2><input type="checkbox" class="form cushycms" value="yes" name="check8">Cashew Nuts 1/4kg <b>$5.00</b></h2>
    <h2><input type="checkbox" class="form cushycms" value="yes" name="check9">Tassie Dutch Cream Potatoes 2kg <b>$7.00</b></h2>
    <h2><input type="checkbox" class="form cushycms" value="yes" name="check10">Avocado's 4 for <b>$7.00</b></h2>
    <h2><input type="checkbox" class="form cushycms" value="yes" name="check11">Tomatoes 1kg <b>$4.00</b></h2>
    <h2><input type="checkbox" class="form cushycms" value="yes" name="check12">Cold Pressed Honey 1 kg <b>$11.00</b></h2>
    <h2><input type="checkbox" class="form cushycms" value="yes" name="check13">Cage Free Eggs 1 Doz <b>$4.00</b></h2>
    <h2><input type="checkbox" class="form cushycms" value="yes" name="check14">Soup Packs <br>(Carrot, parsley, onion, parsnip, celeriac) <b>$4.00</b></h2>
<input class="send" type="submit" value="Submit">
                    <input class="send" type="reset" value="Reset">
    </section>



    <section class="form">
    <h1>Order:</h1>
    <h2 style="margin-top:-15px;">To avoid delays pre-order here. Simply choose your crops, fill in your details below and click submit. </h2><br>

            Full Name:<br>
                <input class="name-email" type="text" name="name" placeholder="John Smith"><br>
            Business:<br>
                <input class="name-email" type="text" name="business" placeholder="Business Inc." ><br>
            Email:<br>
                <input class="name-email" type="text" name="email" placeholder="[email protected]"><br>
            Message<br>
                <input class="name-email" type="text" name="message" placeholder="Extra Comments" ><br>


        </form>

PHP:

PHP:

//Validate first
if(empty($name)||empty($visitor_email))
{
    echo "Name and email are mandatory!";
    exit;
}

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

$email_from = '';//<== update the email address
$email_subject = "New Order submission";
$email_body = "You have received a new order from: $name.\n".
    "Email:$email_from: /n"
    "Business Name:  $business.\n".
    "Here is the message: $message\n".
    "PinkLadyApples2kg:$check1\n".
    "Mandarines1kg:$check2\n".
    "Bananas1kg:$check3\n".
    "Grapes1kg:$check4\n".
    "Strawberries:$check5\n".
    "Cherries:$check6\n".
    "PistachioNuts1.4kg:$check7\n".
    "CashewNuts1/4kg:$check8\n".
    "TassieDutchPotatoes2kg:$check9\n".
    "Avocados4:$check10\n".
    "Tomatoes1kg:$check11\n".
    "Honey1doz:$check12\n".
    "Eggs1doz:$check13\n".
    "Soup:$check14\n".

$to = "";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";

//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header( 'Location:/thank-you.html' );


// 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;
  }
}

?>

推荐答案

我认为您的代码中包含header('Location:....'),但是在此之前您已经回显了,或者您在<?php之前有一些东西或<?.在输出某些内容之前,可以发送标题.您可以通过删除回声或使用缓冲区来修复它.

I think that you have header('Location: ....') in your code but you have echo before that or you have something before <?php or <?. Headers can be send before you output something. You can fix it by removing the echo or using the buffer.

<?php
ob_start();

<YOUR CODE>

ob_end_flush();

?>

这篇关于PHP Web表单发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 14:50