将 HTML POST 表单字段数据插入 PHPmailer 电子邮件正文

inserting HTML POST form field data into PHPmailer email body

我已经尝试了我在网上和这里找到的所有提示,但我无法实现的是发送的电子邮件在 html 格式的消息中包含表单数据。邮件功能正常,我可以正常收到邮件。

表格基本是:

<form method="post" action="test.php" name="Work Record" autocomplete="off">

<input type="text" name="first_name" id="staff_name2" placeholder="First" required />

<input type="text" name="last_name" id="staff_name" placeholder="Last" required />

<input name="checkbox" type="checkbox" class="head-l" id="checkbox" onChange="this.form.submit()">

并使用 google 的最新 phpmailer,操作脚本如下。我有通过 gmail 发送电子邮件的功能,我只是不确定从表单中提取数据并将其放入电子邮件正文的位置。现在粘贴在正文中的代码是我们在切换到 phpmailer 之前使用的。谢谢!

<?php
require '/home/newnplhftp/nplh.us/smtp/phpmailer/class.phpmailer.php';

$mail = new PHPMailer;

$mail->IsSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';  // Specify main and backup server
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'xxxxxxxxxxxxxxxx';                            // SMTP username
$mail->Password = 'xxxxxxxxx';                           // SMTP password
$mail->SMTPSecure = 'ssl';                            // Enable encryption, 'ssl' also accepted
$mail->Port = 465;                    // set the SMTP port for the GMAIL server

$mail->From = 'xxxxxxxxxxxxxx';
$mail->FromName = 'NPLH';
$mail->AddAddress('xxxxxxxxxxxxxxxxxxxxx');               // Name is optional

$mail->AddCC('');
$mail->AddBCC('');

$mail->WordWrap = 50;                                 // Set word wrap to 50 characters

$mail->IsHTML(true);                                  // Set email format to HTML

$mail->Subject = 'xxxxxxxxxxxxxxx';

$mail->Body = '
<html>
<h2><b>$first_name $last_name, $license_type</b></h2>
</html>';

$mail->AltBody = '';

if(!$mail->Send()) {
   echo 'Message could not be sent.';
   echo 'Mailer Error: ' . $mail->ErrorInfo;
   exit;
}

echo 'Thank you, your message has been sent!';

?>

您尝试发送的变量需要先从表单中获取。这会发生在 test.php 上,因为这是您要将值发布到的位置。

所以你会这样做

$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$license_type = $_POST['license_type'];

然后您就可以在邮件正文中使用它们了。

$mail->Body = "<html><h2><b>$first_name $last_name, $license_type</b></h2></html>";

虽然我强烈建议先以某种方式验证该数据。

请设置;

$mail->Body = '
<html>
<h2><b>$first_name $last_name, $license_type</b></h2>
</html>';

$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$license_type = $_POST['license_type'];

    $mail->Body = "
    <html>
    <h2><b>".$first_name." ".$last_name.", ".$license_type."</b></h2>
    </html>";