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


Send completed form information to an email

嘿伙计们,我刚刚开始学习一些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>

使用 PHP 邮件或 PHPMailer(效果很好)?

使用 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">

希望你让它工作!