【问题标题】:PHP variables in mail message body are not shown in the actual email邮件正文中的 PHP 变量未显示在实际电子邮件中
【发布时间】:2015-10-23 18:09:29
【问题描述】:

最初,phpmailer 没有从我的这个页面发送邮件,但我设法修复了它。这一次,消息发送到 email 变量,除了页面中已经声明的变量之外的所有其他内容都没有显示。

页面源代码如下;

<?php
require_once("connection.php");
?>

<?php

$date_order = gmdate('M d, Y');
$time_order = gmdate('H:i:s');
$cust_ID = $_SESSION['cust_ID'];
$order_ref = $_SESSION['order_ref'];

$query1 = mysql_query("SELECT * FROM order_receipts WHERE   order_ref='$order_ref'");
$num1 = mysql_num_rows($query1);

if($num1<1)
{
$query = mysql_query("INSERT INTO order_receipts      (order_ref,cust_id,date_order,time_order) VALUES ('$order_ref','$cust_ID','$date_order','$time_order')");

}

$query2 = mysql_query("SELECT * FROM personal_details WHERE id='$cust_ID'");
while($rows = mysql_fetch_assoc($query2))
{
$title = $rows['title'];
$first_name = $rows['first_name'];
$last_name = $rows['last_name'];
$middle_name = $rows['middle_name'];
$email_address = $rows['email_address'];
$fullname = $title." ".$first_name." ".$middle_name." ".$last_name;
}

$query3 = mysql_query("SELECT * FROM order_details WHERE order_ref='$order_ref' AND customer_id = $cust_ID");
while($row2 = mysql_fetch_assoc($query3))
{
$title = $row2['title'];
$product_sample = $row2['product_sample'];
$dress_type = $row2['dress_type'];
$name_of_dress = $row2['name_of_dress'];
$sewing_end_date = $row2['sewing_end_date'];
$service_charge = $row2['service_charge'];
$amount_paid = $row2['amount_paid'];
$prod_desc = $row2['prod_desc'];
$date_order = $row2['date_order'];
$sewing_start_date = $row2['sewing_start_date'];
$sumtotal += $rows2['service_charge'];
$sumadvance += $rows2['amount_paid'];
}

require("/home/sewsoftc/public_html/PHPMailer_5.2.0/class.smtp.php");

require("/home/sewsoftc/public_html/PHPMailer_5.2.0/class.phpmailer.php");

$mail = new PHPMailer();

$mail->IsSMTP();                                      
$mail->Host = "localhost";  
$mail-> Port = 587;
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;     // turn on SMTP authentication
$mail->SMTPSecure = 'tls';  
$mail->Username = "sender@mail.com";  // SMTP username
$mail->Password = "sender_password"; // SMTP password

$mail->From = "noreply@mail.com";
$mail->FromName = "Unania Ltd.";
$mail->AddAddress("$email_address", "$fullname");
$mail->AddReplyTo("admin@mail.com", "Information");
$mail->AddEmbeddedImage  ("images/una_mail.png","unanialogo","images/una_mail.png");
$mail->AddEmbeddedImage("$product_sample","ordersample","$product_sample");

$mail->WordWrap = 50;                                 
$mail->IsHTML(true);                                 

//$message = file_get_contents("msg.php");
$mail->Subject = "Order Notification From Unania Couture";
$mail->Body    = "<table>
<tr style='background:#D71576; height:140px; width:100%;'>
<td style='padding:22px 0; text-align:center;'><img src='cid:unanialogo' /></td>
</tr>

<tr style='color:#056FFF; font-size:18px; font-weight:bold;'>
<td><br />Dear <?php echo ''.$fullname; ?>,<br /><br /><span     style='color:#636D76; font-size:16px; font-weight:normal;'>Thank you for ordering our tailoring service. The order details of your dress are enclosed in the receipt below for your record.</span><br /></td></tr>

<tr>
<td>
<table style='width:90%; margin:0 auto; font-family:tahoma; font-size:12px;' align='center'>
<tr style='background:#056FFF; color:#fff; font-size:16px;'>
<td style='width:19%; font-size:113%; font-weight:bold; font-family:calibri;'>Name</td>
<td colspan='3' style='width:49%; background: #3B88F3;'><?php    echo ''.$title.' '.$first_name.' '.$middle_name.' '.$last_name; ?></td>
<td style='width:19%; font-size:113%; font-weight:bold; font-family:calibri;'>Invoice No</td>
<td style='width:15%; background: #3B88F3;'><?php echo ''.$order_ref; ?></td>
</tr>
<?php
while($row2 = mysql_fetch_assoc($query3))
{?>
<tr style='background:#fff; color:#000; font-size:16px;'>
<td style='width:19%; font-size:113%; font-weight:bold; font-family:calibri;'>Dress Style</td>
<td  style='width:19%; background:#E5DDDD;'><?php echo ''.$dress_type.' - '.$name_of_dress; ?></td>
<td style='width:16%; font-size:113%; font-weight:bold; font-family:calibri;'>Projected Date of Completion</td>
<td style='width:12%; background:#E5DDDD;'><?php echo ''.$sewing_end_date; ?></td>
<td style='width:19%; font-size:113%; font-weight:bold; font-family:calibri;'>Service Charge</td>
<td style='width:15%; background:#E5DDDD;'><?php echo ''.$service_charge; ?></td>
</tr>

<tr style='background:#056FFF; color:#fff; font-size:16px;'>
<td style='width:19%; font-size:113%; font-weight:bold; font-family:calibri;'>Dress Description</td>
<td colspan='3' style='width:49%; background: #3B88F3;'><?php   echo ''.$prod_desc; ?></td>
<td style='width:19%; font-size:113%; font-weight:bold; font-family:calibri;'>Dress Sample</td>
<td style='width:15%; background: #3B88F3;'><img src='cid:ordersample' width='50' height='50'/></td>
</tr>
<?php } ?> 
<tr style='background:#fff; color:#000; font-size:16px;'>
<td style='width:19%; font-size:113%; font-weight:bold; font-family:calibri;'>Total Charge</td>
<td colspan='3' style='width:49%; background: #E5DDDD;'><?php echo ''.$sumtotal; ?></td>
<td style='width:19%; font-size:113%; font-weight:bold; font-family:calibri;'>Advance Payment</td>
<td style='width:15%; background:#E5DDDD;'><?php echo ''.$sumadvance; ?></td>
</tr>

<tr style='background:#056FFF; color:#fff; font-size:16px;'>
<td style='width:19%; font-size:113%; font-weight:bold; font-family:calibri;'>Balance Remaining</td>
<td colspan='3' style='width:49%; background: #E5DDDD;'>₦<?php echo ''.$balance_remaining = -($sumtotal - $sumadvance); ?></td>
<td style='width:19%; font-size:113%; font-weight:bold; font-family:calibri;'>Order Date</td>
<td style='width:15%; background:#3B88F3;'><?php echo ''.$order_date; ?></td>
</tr>

</table>
</td>
</tr>

<tr>
<td><br /><span style='color:#636D76; font-size:16px; font-weight:normal;'>
Please note that all outstanding payments will be duly collected when the dress is completed and ready to be collected. We hope to bring you an unequaled satisfactory service. We will keep in touch with you to let you know when your dress is completed.</span>
<br /><br /><br /><span style='color:#637894; font-size:15px; font-weight:normal;'>This email you received is an automated message sent to you by Unania Couture in response of any activity that concerns you within the company.</span><br /><span style='color:#637894; font-size:15px; font-weight:normal;'>Do not reply back to this mail, as our administration staff will not be able to give any feedback following your reply, but contact us on admin@sew-soft.com</span><br /><br /><br /><span style='color:#637894; font-size:15px; font-weight:normal;'>To unsubscribe from automatic email notification, please click this link <a href='#'>Unsubscribe me</a></span>
</td></tr>

</table>";

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

//echo $_SESSION['order_ref'];
//echo "<br />".$fullname;

//echo("<script>location.href = 'receipt2.php?order_ref=$order_ref&cust_ID=$cust_ID';</script>");

?>

我还回显了变量以查看它是否已设置,并且显示正常。

【问题讨论】:

  • 尝试格式化您的问题,答案将显而易见。您正在尝试在字符串文字中执行 &lt;?php echo... 。那是行不通的。
  • 另外,请don't use mysql_*; mysql_* 函数已过时、deprecated 且不安全。请改用MySQLiPDO
  • 除了@EdCottrell,您可能希望在问题中摆脱(公开地​​)您客户的公司名称,因为他肯定不希望看到他的名字出现在编程站点中......
  • 非常感谢(Ed Cottrell),它成功了。但是对于mysqli_query,不是很熟悉,同事说是面向对象的php,目前用旧形式的程序式php编程很好用,可以不用声明类的情况下使用这个函数吗?你能给我推荐一些我可以用来非常擅长的东西吗?我会感激的
  • @optimalresource:您也可以为此使用库。一个非常好的使用 mysqli 的是来自joshcam 的那个。阅读 github 上的整个文档,一切顺利!

标签: php email variables session phpmailer


【解决方案1】:

就像@EdCottrell 所说,你必须像这样(缩短)摆脱&lt;?php echo 语句:

$mail->Body    = "<table>
<tr style='background:#D71576; height:140px; width:100%;'>
<td style='padding:22px 0; text-align:center;'><img src='cid:unanialogo' /></td>
</tr>
<tr style='color:#056FFF; font-size:18px; font-weight:bold;'>
<td><br />Dear {$fullname},<br /><br /><span     style='color:#636D76; font-size:16px; font-weight:normal;'>Thank you for ordering our tailoring service. The order details of your dress are enclosed in the receipt below for your record.</span><br /></td></tr>

<tr>
<td>
<table style='width:90%; margin:0 auto; font-family:tahoma; font-size:12px;' align='center'>
<tr style='background:#056FFF; color:#fff; font-size:16px;'>
<td style='width:19%; font-size:113%; font-weight:bold; font-family:calibri;'>Name</td>
<td colspan='3' style='width:49%; background: #3B88F3;'>{$title} {$first_name} {$middle_name} {$last_name}</td>
<td style='width:19%; font-size:113%; font-weight:bold; font-family:calibri;'>Invoice No</td>
<td style='width:15%; background: #3B88F3;'>{$order_ref}</td>
</tr>";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-24
    • 2019-12-12
    • 1970-01-01
    • 2012-09-07
    相关资源
    最近更新 更多