簡體   English   中英

在電子郵件PHP中復制HTML表單

[英]Copy HTML form in email PHP

我正在嘗試將HTML表單復制到電子郵件正文中。 下面是其中表單已經存在於php文件中的代碼,但是我想如果用戶提交html表單文件,則所有未隱藏的表單數據都將被復制(作為禁用元素)在電子郵件正文中。 另外,如果有任何文件附件字段,則應將其附加到電子郵件中。

<?php
// multiple recipients
$to  = 'demo@localhost.com' . ', '; // note the comma
$to .= 'demo@localhost.com';

// subject
$subject = 'Birthday Reminders for August';

// message
$message = '
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />


<title>Untitled Document</title>
</head>

<body style="padding:3px; margin:0px;" bgcolor="#FFFFFF">
<table cellpadding="0" cellspacing="0" border="0" width="440">

    <tr><td style="height:10px"></td></tr>
    <tr>
      <td colspan="2" style="text-align:justify; line-height:15px;" class="body">

      <form name="frm" method="POST" action="careersuccess.php" enctype="multipart/form-data">
      <table cellpadding="0" cellspacing="0" border="0" width="100%">
          <tr>
            <td width="23%" class="body"> Name</td>
            <td width="3%" class="body">:</td>
            <td width="74%"><input type="text" name="strname" class="textfield"></td>
        </tr>
        <tr><td style="height:3px"></td></tr>
        <tr>
            <td width="23%" class="body"> Address</td>
            <td width="3%" class="body">:</td>
            <td width="74%"><textarea cols="16" name="straddress"></textarea></td>
        </tr>
        <tr><td style="height:3px"></td></tr>
        <tr>
            <td width="23%" class="body"> City</td>
            <td width="3%" class="body">:</td>
            <td width="74%"><input type="text" name="strcity" class="textfield"></td>
        </tr>
        <tr><td style="height:3px"></td></tr>
        <tr>
            <td width="23%" class="body"> State</td>
            <td width="3%" class="body">:</td>
            <td width="74%"><input type="text" name="strstate" class="textfield"></td>
        </tr>
        <tr><td style="height:3px"></td></tr>
        <tr>
            <td width="23%" class="body"> Contact No</td>
            <td width="3%" class="body">:</td>
            <td width="74%"><input type="text" name="strno" class="textfield"></td>
        </tr>
        <tr><td style="height:3px"></td></tr>
        <tr>
            <td width="23%" class="body"> Email</td>
            <td width="3%" class="body">:</td>
            <td width="74%"><input type="text" name="stremail" class="textfield"></td>
        </tr>
        <tr><td style="height:3px"></td></tr>
        <tr>
            <td width="23%" class="body"> Comments</td>
            <td width="3%" class="body">:</td>
            <td width="74%"><textarea cols="16" name="strcomments"></textarea></td>
        </tr>
        <tr><td style="height:3px"></td></tr>
        <tr>
            <td width="23%" class="body"> Resume</td>
            <td width="3%" class="body">:</td>
            <td width="74%"><input type="file" name="strresume"></td>
        </tr>
        <tr><td style="height:10px"></td></tr>
        <tr>

        </tr>

      </table>   
     </form>

</td>
    </tr>
    <tr>
      <td colspan="2" align="center"> </td>
  </tr>
    </table>
</body>
</html>
';

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);
?>

我假設該表單是從網站提交的,並且您不是要在電子郵件中創建可提交表單。 復制以$ _POST [“”]格式提交的數據; 肯定是獲取數據的最佳方法。 顯然,一旦電子郵件到達收件人的收件箱,數據將無法處理,但是,正如我所說,我假設您不打算創建可以通過電子郵件提交的表單。

您在網站上的表格很好:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />


<title>Untitled Document</title>
</head>

<body style="padding:3px; margin:0px;" bgcolor="#FFFFFF">
<table cellpadding="0" cellspacing="0" border="0" width="440">

<tr><td style="height:10px"></td></tr>
<tr>
  <td colspan="2" style="text-align:justify; line-height:15px;" class="body">

  <form name="frm" method="POST" action="careersuccess.php" enctype="multipart/form-data">
  <table cellpadding="0" cellspacing="0" border="0" width="100%">
      <tr>
        <td width="23%" class="body"> Name</td>
        <td width="3%" class="body">:</td>
        <td width="74%"><input type="text" name="strname" class="textfield"></td>
    </tr>
    <tr><td style="height:3px"></td></tr>
    <tr>
        <td width="23%" class="body"> Address</td>
        <td width="3%" class="body">:</td>
        <td width="74%"><textarea cols="16" name="straddress"></textarea></td>
    </tr>
    <tr><td style="height:3px"></td></tr>
    <tr>
        <td width="23%" class="body"> City</td>
        <td width="3%" class="body">:</td>
        <td width="74%"><input type="text" name="strcity" class="textfield"></td>
    </tr>
    <tr><td style="height:3px"></td></tr>
    <tr>
        <td width="23%" class="body"> State</td>
        <td width="3%" class="body">:</td>
        <td width="74%"><input type="text" name="strstate" class="textfield"></td>
    </tr>
    <tr><td style="height:3px"></td></tr>
    <tr>
        <td width="23%" class="body"> Contact No</td>
        <td width="3%" class="body">:</td>
        <td width="74%"><input type="text" name="strno" class="textfield"></td>
    </tr>
    <tr><td style="height:3px"></td></tr>
    <tr>
        <td width="23%" class="body"> Email</td>
        <td width="3%" class="body">:</td>
        <td width="74%"><input type="text" name="stremail" class="textfield"></td>
    </tr>
    <tr><td style="height:3px"></td></tr>
    <tr>
        <td width="23%" class="body"> Comments</td>
        <td width="3%" class="body">:</td>
        <td width="74%"><textarea cols="16" name="strcomments"></textarea></td>
    </tr>
    <tr><td style="height:3px"></td></tr>
    <tr>
        <td width="23%" class="body"> Resume</td>
        <td width="3%" class="body">:</td>
        <td width="74%"><input type="file" name="strresume"></td>
    </tr>
    <tr><td style="height:10px"></td></tr>
    <tr>

    </tr>

  </table>   
 </form>

</td>
</tr>
<tr>
  <td colspan="2" align="center"> </td>
</tr>
</table>
</body>
</html>

然后,您的文件careersuccess.php將需要包含以下內容:

<?php
// multiple recipients
$to  = 'demo@localhost.com' . ', '; // note the comma
$to .= 'demo@localhost.com';

// subject
$subject = 'Birthday Reminders for August';

$strName = $_POST["strname"]; //assigns strname from form to PHP variable
$strAddress = $_POST["straddress"];
$strCity = $_POST["strcity"];
$strState = $_POST["strstate"];
$strNumber = $_POST["strnumber"];
$strEmail = $_POST["stremail"];
$strComments = $_POST["strcomments"];
$strResume = $_POST["strresume"];
$lineBreak = "<br />\n\"; //adds carriage return to break up your message

$message = "Name: ".$strName.$lineBreak."Address: ".$strAddress.$lineBreak."City: ".$strCity.$lineBreak."State: ".$strState.$lineBreak...for the rest of your variables;

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);
?>

這將導致表單的提交值被復制到mail()函數的$ message變量中的電子郵件中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM