2013-10-12 46 views
-2

嘿傢伙我剛剛開始學習一些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("[email protected]", $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

使用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 = "[email protected]"; 
$mail->Password = "somePass"; 
$mail->From  = '[email protected]'; 
$mail->FromName = 'My Website'; 
$mail->WordWrap = 50; 
$mail->isHTML(true); // Or false 
$mail->addReplyTo('[email protected]', '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"> 

希望你得到它的工作!

+0

感謝您使用此方法嘗試的幫助,但它顯示所有代碼作爲網站上的文本。有任何想法嗎? – kduan

+0

是的。你必須使用<?php開始標記=) –