如何将POST表单数据发送到电子邮件中

时间:2017-10-16 15:10:40

标签: php html forms email phpmailer

我在为客户订购产品时填写表格。 我试图这样做,当他们点击提交时,它会将数据通过电子邮件发送到我的电子邮件中,然后抄送他们。电子邮件中会有样式。我无法弄清楚如何将数据放入电子邮件中。我目前正在使用PHPMailer。
这是php文件

<?php

$FirstName = $_POST['First_Name'];
$LastName = $_POST['Last_Name'];
$Email = $_POST['Email'];
$Company = $_POST['Company'];


$dochtml = new DOMDocument();
require_once('PHPMailer/PHPMailerAutoload.php');
$mail = new PHPMailer();
$mail->isSMTP();
$mail->SetFrom('No-Reply@LDSVacuum.com');
$mail->Subject = 'Request';
$mail->AddAddress('Chase.Price@LDSVacuum.com');
$mail->AddCC($Email);
$strhtml = '
<div align="center">
<font face="arial" color="#336699" size="6">
    Request
</font>
</div>
<div> 
<font face="arial" color="#336699" size="5">
    Customer Information
</font>
<p>
    <label for="fname">
        <font face="arial" color="#336699" size="4">
            First Name:
        </font>
    </label>
    <br />
    <input type="text" name="fname" id="fname" size="50">
</p>
<p>
    <label for="lname">
        <font face="arial" color="#336699" size="4">
            Last Name:
        </font>
    </label>
    <br />
    <input type="text" name="lname" id="lname" size="50">
</p>
<p>
    <label for="email">
        <font face="arial" color="#336699" size="4">
            Email Address:
        </font>
    </label>
    <br />
    <input type="text" name="email" id="email" size="50">
</p>
<p>
    <label for="company">
        <font face="arial" color="#336699" size="4">
            Company Name:
        </font>
    </label>
    <br />
        <input type="text" name="company" id="company"size="50">
</p>
</div>
';

$dochtml->loadHTML($strhtml);
$fname = $dochtml->getElementById('fname');
$fname->value=$FirstName;
$lname = $dochtml->getElementById('lname');
$lname->value=$LastName;
$email = $dochtml->getElementById('email');
$email->value=$Email;
$company = $dochtml->getElementById('company');
$company->value=$FirstName;

$mail->Body = $strhtml;

$mail->Send();

if(!$mail->Send()) {
echo 'Unknown Error has Occured. Please try again Later.';
echo 'Mailer error: ' . $mail->ErrorInfo;
}else {
    echo 'Thank You, $FirstName . We have emailed your request to Info@LDSVacuum.com . Your email $Email has been cc to the email.';
}   

?>

这是表格

&#13;
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="POST" name="contactform" autocomplete="off" id="nipplerequest" action="http://localhost/emailform.php"> 
	<p>
		<label for='First_Name' class="FG3" width="157">
			<span class="red">
				*
			</span> 
			First Name:
		</label>
			<br />
		<input type="text" name="First_Name" id="First_Name" class="FG2" size="50" placeholder="John">
			<br />
		<label for='Last_Name' class="FG3">
			<span class="red">
				*
			</span> 
			Last Name:
		</label>
			<br />
		<input type="text" name="Last_Name" class="FG2" size="50" placeholder="Johnson">
			<br />
		<label for='Email' class="FG3">
			<span class="red">
				*
			</span> 
			Your Email Address:
		</label>
			<br />
		<input type="text" name="Email" id="Email" class="FG2" size="50" placeholder="John_Johnson@company.com">
			<br />
		<label for='Company' class="FG3">
			<span class="red">
				*
			</span> 
			Company Name:
		</label>
			<br />
		<input type="text" name="Company" class="FG2" size="50" placeholder="Company Inc.">
			<br />
	</p>
  </form>
  <br />
  <input type="Submit" value="Submit">
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

你是:

  1. 创建HTML字符串
  2. 从该HTML字符串创建DOM
  3. 通过设置值
  4. 修改DOM
  5. 通过电子邮件发送原始HTML字符串并忽略您生成的DOM
  6. 您需要将DOM转换回HTML($modified_html = $dochtml->saveHTML())并发送它而不是原始字符串。