PHP Mail:邮件的页面内容

时间:2011-11-14 05:48:56

标签: php email

我有一个表单,客户填写个人数据,并对服务做出选择。因此,使用$ _GET函数,我检索该表单的信息,进行一些基本的数学运算并显示所有信息,以便客户可以预览订单,然后点击“发送”以确认作业。

我需要通过邮件向我发送预览订单,但我不知道如何发送所有这些。我知道如何从表单中检索数据然后发送它,但不知道如何发送这些变量。

FORM

// I am omitting elements to make this shorter    
<select name="Amount">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
     </select>

预览订单

  $price = 5;  

    // I am omitting elements to make this shorter 
    // here I do some math     
    $Amount = $_GET['Amount']; 
        // check value and select appropriate item 
        if ($Amount== "1") { 
            $extra = "1"; 
            } 
        elseif ($Amount == "2") { 
            $extra = "2"; 
            } 
        elseif ($Amount == "3") { 
            $extra = "3";


// here is the order preview and this is what I need to email to myself
// the customer should look this preview and then HIT a confirm buttom to get this sent

<?php echo $Name;?><br>
<?php echo $Address;?><br>
<?php echo $E-mail;?><br>
<?php echo $Phone;?><br>
Your order: <?php echo $extra . " " . "products, for a total of" . " " . ($price * $extra); ?>

--------------------最终版本---------------感谢所有人!

我将只使用2个字段来缩小代码

// FORM - 用户输入他的数据

<form method="get" id="order" action="order-info.php">
<h1>Personal Info</h1>
<p>name: <input name="name" type="text" /></p>
<p>email: <input name="surname" type="text" /></p>
<INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Continue">
</form>

// ORDER预览 - 在此确认订单之前,用户可以预览他的所有数据和产品相关选项

<?php session_start(); ?>
<?php
// get info personal
$Name = $_GET['name'];
$Email = $_GET['email'];
?>
// Now I echo the info
<h2><?php echo $Name . " " . $Email ; ?><br></h2>
<?php
  // here's where the magic is done thanks to Sheldon Ferns!
  $_SESSION['customerInfo']['name'] = $Name;
  $_SESSION['customerInfo']['email'] = $Email;

?>
// having stored all the info in a session I proceed to send it to the email function. That weird name is because I read you should avoid naming your email process file with predictable names like mail.php, this increases protection against spammers. 
<form  method ="POST" action = "xljkadf.php">
<INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Confirm Order">

// MAIL PROCESS -

<?php  session_start();
 # Anti-header-injection - Use before mail() 
# By Victor Benincasa <vbenincasa(AT)gmail.com> 
foreach($_REQUEST as $fields => $value) if(eregi("TO:", $value) || eregi("CC:", $value) || eregi("CCO:", $value) || eregi("Content-Type", $value)) exit("ERROR: Code injection attempt denied! Please don't use the following sequences in your message: 'TO:', 'CC:', 'CCO:' or 'Content-Type'."); 

  $headers = 'From: Your Site <noreply@yoursite.com>' . "\n".
// in the next line what I do is to send a BCC to me as I want the customer to get a copy of the order without knowing my address  
$headers .= 'Bcc: Your site <yourmail@xxxx.com>' . "\r\n";



  $mailBody = "Order details: \n". 
                "Name: ".$_SESSION['customerInfo']['name'] . "\n".
                "Email: " .$_SESSION['customerInfo']['email'] . "\n";




// Next the mail function. The first arguments is the customer e-mail so he gets a copy of his order.
   mail($_SESSION['customerInfo']['Email'], "Order Info ", $mailBody, $headers);

   ?>
 // A redirect to a thank you page once the e-mail is sent.
<script language="JavaScript" type="text/JavaScript">
<!--
window.location.href = "http://www.yoursite.com/thank-you.html";
//-->
</script>

4 个答案:

答案 0 :(得分:0)

如果我正确理解您的问题,您需要知道如何使用php在电子邮件中发送一些内容。 请参阅php中的mail函数。文档heremessage参数可以包含您的内容。另外,通过文档中的示例,您将了解实现。

答案 1 :(得分:0)

制作一个这样的字符串(我省略了换行符和额外的格式):

$mystring = 'Name: '.$Name.', Address: '.$Address.', E-mail: '.$E-mail.', $Phone: '.$Phone;

然后通过mail()发送:

mail('myaddress@php.com', 'My Subject', $mystring);

答案 2 :(得分:0)

$mailBody将捕获您在ob_start()ob_get_clean()之间回应的所有内容。 mail()功能会将电子邮件发送给您。

<?php ob_start(); ?>
<?php echo $Name;?><br>
<?php echo $Address;?><br>
<?php echo $E-mail;?><br>
<?php echo $Phone;?><br>
Your order: <?php echo $extra . " " . "products, for a total of" . " " . ($price * $extra); ?>
<?php $mailBody = ob_get_clean(); 

mail('your@mail.com', 'Mail Subject', $mailBody);

?>

答案 3 :(得分:0)

您希望在用户点击预览页面中的发送按钮时发送电子邮件。但是你不知道如何在PHP中获取客户正在预览的数据;因为发送不提交表格。
这就是你的问题,这就是你能做的事情

当客户提交您的,在previewOrder php文件中,您可以将信息保存在会话变量中。例如

<?php
  session_start(); ?>
  //The php code you have already written.

  //Saving data in session
  <?php
  $_SESSION['customerInfo']['Name'] = $Name;
  $_SESSION['customerInfo']['Address'] = $Address;
  $_SESSION['customerInfo']['Email'] = $Email;
  $_SESSION['customerInfo']['Phone'] = $Phone;
  $_SESSION['customerInfo']['Extra'] = $Extra;
?>

请注意,session_start()必须位于文件的开头。

现在,当用户单击SEND按钮时,从预览页面,你必须将页面指向sendOrder.php这样的文件,它应该是这样的

<?php
  session_start();
  $mailBody = "Order Info: \n". 
              "Name: ".$_SESSION['customerInfo']['Name'] . "\n".
              "Address: " .$_SESSION['customerInfo']['Address'] . "\n".
              "Email: " .$_SESSION['customerInfo']['Email'] . "\n".
              "Phone: " .$_SESSION['customerInfo']['Email'] . "\n".
              "Price: " .$_SESSION['customerInfo']['Price'] . "\n".
              "Extra: " .$_SESSION['customerInfo']['Extra'] . "\n";

   mail('your@mail.com', 'Order Information', $mailBody);

?>

您在此处所做的是阅读预览中会话中保存的值。 SESSIONS是一种可以跨页面提供变量的方法。

希望这有帮助,

PS。我没有测试代码的语法错误。如果有的话,请更正它们。)