将完成的表单信息发送到电子邮件

时间:2013-10-12 18:36:55

标签: html php

嘿伙计们我刚刚开始学习一些PHP,我想知道如何将完整表格中的信息发送到电子邮件。我有我在底部列出的代码,但我知道它不对,任何帮助将不胜感激谢谢!

PHP:

<?php

  $email = $_REQUEST['clientEmail'] ;
  $subject = "New Order";
  $name = $_REQUEST['clientName'] ;
  $articleAmount = $_REQUEST['articleNum'];
  $wordAmount = $_REQUEST['wordNum'];
  $topic = $_REQUEST['topic'];
  $info = $_REQUEST['addInfo'];
  mail("kevin.duan996@gmail.com", $subject,
  "Name:" . $name . "<br/>"  . "Amount of Articles:" . $articleAmount . "<br/>" . "Amount of Words:" . $wordAmount . "<br/>" . "Topic:" . $topic . "<br/>" . "Additional Information:" . $info, "From:" . $email);
  echo "Thank you for ordering!";

?>

HTML:

<form action="order.php">

    <fieldset id="client" >

        <legend>Client Information</legend>
        <label for="clientName">Name:</label><input type="text" name="clientName" id="clientName" tabindex="1"/>
        <label for="clientEmail">Email:</label><input type="email" name="clientEmail" id="clientEmail" tabindex="2"/>

    </fieldset>

    <fieldset id="order">

        <legend>Order Information</legend>
        <label for="articleNum">Number of Articles</label><input type="text" name="articleNum" id="articleNum" tabindex="3"/>
        <label for="wordNum">Words per Article</label><input type="text" name="wordNum" id="wordNum" tabindex="4"/>
        <label for="topic">Topics</label><input type="text" name="topic" id="topic" tabindex="5"/>
        <label for="addInfo">Additional Info</label><input type="text" name="addInfo" id="addInfo" tabindex="6"/>

    </fieldset>

    <fieldset>

        <button type="submit">Submit!</button>

    </fieldset>
</form>

2 个答案:

答案 0 :(得分:0)

使用PHP mailPHPMailer(效果很好)?

答案 1 :(得分:0)

使用PHPMailer库:https://github.com/PHPMailer/PHPMailer


define('PROJECT_ROOT', '/path/to/your/root/'); //Different for different webhosts or self hosted environments

require (PROJECT_ROOT . 'PHPMailer-master/class.phpmailer.php');

$mail = new PHPMailer();
$mail->IsSMTP();
$to = $_POST['clientEmail']; //Email dervied from POST data.

$mail->Host       = "mail.example.com"; //If you haven't got an SMTP server, use Gmail's free one.
$mail->SMTPDebug  = 0; 
$mail->SMTPAuth   = true;
$mail->Port       = 25;
$mail->Username   = "someEmail@example.com";
$mail->Password   = "somePass";
$mail->From       = 'someEmail@example.com';
$mail->FromName   = 'My Website';
$mail->WordWrap   = 50;
$mail->isHTML(true); // Or false
$mail->addReplyTo('support@example.com', 'Support');

$mail->Subject = 'Message subject here';
$mail->addAddress($to);

$mail->Body = ""; // Message body using HTML here. (Remove if $mail->isHTML(); is false)
$mail->AltBody = "

Client: " . $_POST['clientName'] . " //Again: derived from POST data.
Email: " . $to . " //We defined this variable before as clientEmail

Number of Articles: " . $_POST['articleNum'] . "
Words per Article: " . $_POST['wordNum'] . "
Topics: " . $_POST['topic'] . "
Additional Info: " . $_POST['addInfo'] . "


";

    if(!$mail->send()) {
         echo 'Message could not be sent.';
         echo "Mailer Error: " . $mail->ErrorInfo;
         exit;
    } else {
            echo "Mail sent!";
        }

我注意到您的表单缺少一种发送用户输入的信息的方法。

<form action="order.php">

应该是:

<form action="order.php" method="POST">

希望你能让它运转起来!

相关问题