简体   繁体   English

在电子邮件PHP中复制HTML表单

[英]Copy HTML form in email PHP

I am trying to copy my HTML form in email body. 我正在尝试将HTML表单复制到电子邮件正文中。 Below is code where the form is already in php file but I want if the user submits html form file then all the form data between that is not hidden is copied (as disabled elements) in the body of the email. 下面是其中表单已经存在于php文件中的代码,但是我想如果用户提交html表单文件,则所有未隐藏的表单数据都将被复制(作为禁用元素)在电子邮件正文中。 Also if there is any file attachment field, it should be attached to the email. 另外,如果有任何文件附件字段,则应将其附加到电子邮件中。

<?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);
?>

I'll assume the form is submitted from a website, and that you're not trying to create a submittable form in an email. 我假设该表单是从网站提交的,并且您不是要在电子邮件中创建可提交表单。 To copy the data submitted in the form, $_POST[""]; 复制以$ _POST [“”]格式提交的数据; would surely be the best way to obtain the data. 肯定是获取数据的最佳方法。 Obviously the data wouldn't be manipulatable once the email has arrived in the recipient's inbox but, as I say, I'm assuming you're not trying to create a form which can be submitted from an email. 显然,一旦电子邮件到达收件人的收件箱,数据将无法处理,但是,正如我所说,我假设您不打算创建可以通过电子邮件提交的表单。

Your form on the website was fine: 您在网站上的表格很好:

<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>

Your file careersuccess.php would then need to include something like: 然后,您的文件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);
?>

That will result in the submitted values of the form being copied in to the email in the $message variable for the mail() function. 这将导致表单的提交值被复制到mail()函数的$ message变量中的电子邮件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM