我如何在html中回显php var

时间:2015-08-01 03:47:05

标签: php html variables

我正在为我的客户工作一个网站,我希望应用程序阅读html的内容并向客户的电子邮件发送电子邮件,但我想显示客户在html中输入的一些变量内容并通过电子邮件发送。这是我的代码。

这是html代码:

<p> {$cname} </p>
      <p> Thank you for contacting our support team. A support ticket has now been   opened for your request. You will be notified when a response is made   by email. The details of your ticket are shown below. </p>
      <p> Subject: {$sub} <br />
        Priority: {$prio} <br />
        Status: Open </p>
      <p> Thanks as always for being our customer.<br />
          <br />
    Regards,</p></td>

这是我的PHP代码:

<?php

//error_reporting(E_ALL);
error_reporting(E_STRICT);

date_default_timezone_set('America/Toronto');

require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded


$_SESSION['studentid'] = $_POST['sid'];
$_SESSION['cname'] = $_POST['namee'];
$_SESSION['email'] = $_POST['email']; 
$_SESSION['department'] = $_POST['department'];
$_SESSION['priority'] = $_POST['priority'];
$_SESSION['subjectd'] = $_POST['subject'];
$_SESSION['commentc'] = $_POST['message'];

$cname =$_SESSION['cname'];
$detailz = $_SESSION['commentc'];
$email = $_SESSION['email'];
$cname = $_SESSION['cname'];
$ip = getenv("REMOTE_ADDR");
$prio =$_SESSION['priority'];
$stid = $_SESSION['studentid'];
$dept = $_SESSION['department'];
$sub  = $_SESSION['subjectd'];


$mail             = new PHPMailer();

$body             = file_get_contents('contents.html');
$body             = preg_replace('/[\]/','',$body);

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = $row_prs['shost']; // SMTP server
$mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
                                           // 1 = errors and messages
                                           // 2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->Host       = $row_prs['shost']; // sets the SMTP server
$mail->Port       = $row_prs['portx'];                    // set the SMTP port for the GMAIL server
$mail->Username   = $row_prs['susername']; // SMTP account username
$mail->Password   = $row_prs['spassword'];        // SMTP account password

$mail->SetFrom($row_pr['from'], $row_pr['sname']);

//$mail->AddReplyTo("name@yourdomain.com","First Last");

$mail->Subject    = "[Ticket ID:" .$tno."]".$sub;

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);

$address = $_SESSION['emid'];
$mail->AddAddress($address, "John Doe");

//$mail->AddAttachment("images/phpmailer.gif");      // attachment
//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}

?>

4 个答案:

答案 0 :(得分:0)

试试这个,

<p> <?php echo $cname; ?> </p>
  <p> Thank you for contacting our support team. A support ticket has now been   opened for your request. You will be notified when a response is made   by email. The details of your ticket are shown below. </p>
  <p> Subject: <?php echo $sub; ?> <br />
    Priority: <?php echo $prio; ?> <br />
    Status: Open </p>
  <p> Thanks as always for being our customer.<br />
      <br />
Regards,</p></td>

答案 1 :(得分:0)

您将数据存储在会话中。所以你可以用它们来展示它。使用此

<p> <?php echo $_SESSION['cname']  ?> </p>
<p> Thank you for contacting our support team. A support ticket has now been   opened for your request. You will be notified when a response is made   by email. The details of your ticket are shown below. </p>
<p> Subject: <?php echo $_SESSION[' subjectd']  ?> <br />
        Priority: <?php echo $_SESSION['priority']  ?> <br />
        Status: Open </p>
<p> Thanks as always for being our customer.<br />
     <br />
    Regards,</p>

您的数据已在会话中播放。因此,在会话被解除之前,您也可以使用这些变量。

答案 2 :(得分:0)

一些参考,但只是替换测试&#39; {$ XXX}&#39; ,不是真正的php var

$replace_values = array(
  '$cname' => $_SESSION['cname'],

);

$result = preg_replace_callback('!\{(\$\w+)\}!', function($ma) use ($replace_values) {
    if(isset($ma[1]) && isset($replace_values[$ma[1]])){
    return $replace_values[$ma[1]];
    }
    return '';
}, $body);

https://eval.in/409962

答案 3 :(得分:0)

这是有效的代码,我只想对那些回答我问题的人表示感谢,但我只是想要感谢李小姐。我拥有一瓶啤酒。对于

///to get the content of the html and replace it with the variables
    $body = file_get_contents('contents.html');

    $replace_values = array(
      '$cname' => $_SESSION['cname'],
'$prio' => $_SESSION['priority'],
'$sub' => $_SESSION['subjectd'],
    );

$result =preg_replace_callback('!\{(\$\w+)\}!', function($ma) use ($replace_values) {
    if(isset($ma[1]) && isset($replace_values[$ma[1]])){
    return $replace_values[$ma[1]];
    }
    return '';
}, $body);

////

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = $row_prs['shost']; // SMTP server
//$mail->SMTPDebug  = 1;                     // enables SMTP debug information (for testing)
                                           // 1 = errors and messages
                                           // 2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->Host       = $row_prs['shost']; // sets the SMTP server
$mail->Port       = $row_prs['portx'];                    // set the SMTP port for the GMAIL server
$mail->Username   = $row_prs['susername']; // SMTP account username
$mail->Password   = $row_prs['spassword'];        // SMTP account password

$mail->SetFrom($row_prs['from'], $row_prs['sname']);

//$mail->AddReplyTo("name@yourdomain.com","First Last");

$mail->Subject    = "[Ticket ID:" .$tno."]".$sub;

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($result);