我正在尝试通过一个漂亮的html模板(实际上是phtml)发送我的联系人发送的电子邮件(使用PHPMailer)。
什么有效:我收到了html模板,因此没有传输问题
不工作原理:变量(消息,电话号码等)未反映在我的html模板正文中。
我在html模板中尝试了几件事,但没有成功:<?= htmlspecialchars($message) ?>
和#message#
以及<?php echo$_POST['message'] ?>
问题是什么?
谢谢,
这是PHPMailer代码:
<?php
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->CharSet = 'utf-8';
$body = file_get_contents('htmlemail.phtml');
//Enable SMTP debugging.
$mail->SMTPDebug = false;
//Set PHPMailer to use SMTP.
$mail->isSMTP();
//Set SMTP host name
$mail->Host = "smtp.sendgrid.net";
//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;
//Provide username and password
$mail->Username = "";
$mail->Password = "";
//If SMTP requires TLS encryption then set it
$mail->SMTPSecure = "tls";
//Set TCP port to connect to
$mail->Port = 587;
$mail->From = $_POST['email'];
$mail->FromName = $_POST['first_name'] . " " . $_POST['last_name'];
$mail->addAddress("@gmail.com");
//CC and BCC
$mail->addCC("");
$mail->addBCC("");
$mail->isHTML(true);
$mail->Subject = "Nouveau message depuis ";
$mail->MsgHTML($body);
$response = array();
if(!$mail->send()) {
$response = array('message'=>"Mailer Error: " . $mail->ErrorInfo, 'status'=> 0);
} else {
$response = array('message'=>"Message has been sent successfully", 'status'=> 1);
}
/* send content type header */
header('Content-Type: application/json');
/* send response as json */
echo json_encode($response);
?>
答案 0 :(得分:6)
使用ob_start
ob_start();
include 'htmlemail.php';
$body = ob_get_clean();
或强>
您还可以使用模板方法生成多个用途的电子邮件正文。
E.g。
在你的html模板中,让变量分配如下:
感谢{NAME}与我们联系。
您的电话号码是{PHONE}
然后在调用phpmailer之前,创建一个数组来处理电子邮件正文:
$email_vars = array(
'name' => $_POST['name'],
'phone' => $_POST['phone'],
);
最后,使用phpmailer ...
$body = file_get_contents('htmlemail.phtml');
if(isset($email_vars)){
foreach($email_vars as $k=>$v){
$body = str_replace('{'.strtoupper($k).'}', $v, $body);
}
}
通过这种方式,您的电子邮件将包含正文中所需的所有动态内容。
答案 1 :(得分:0)
$body = file_get_contents('htmlemail.phtml');
按原样读取文件并放入变量$body
。它不应该有<?php ?>
因为这意味着递归。
如果您希望执行php
内的htmlemail.phtml
代码,可以include()
或require()
。
在你的htmlemail.phtml中,使用:
<?php
$body = "
Hello {$_POST['name']}
This is your message {$_POST['message']}
Thanks
";
在你的php中,替换
$body = file_get_contents('htmlemail.phtml');
与
include('htmlemail.phtml');
请勿在{{1}}文件中包含其他双引号"
,或先在PHP中了解它们。
What is the difference between single-quoted and double-quoted strings in PHP?
答案 2 :(得分:0)
$mail->isHTML(true); // Define as HTML
$mail->Body = HTMLBODY
$mail->AltBody = TEXTBODY
无需定义内容类型PHPMailler设置所有内容
答案 3 :(得分:0)
试试这个,
$mail->Body = 'This is the HTML message body <strong>in bold!</strong>';
我希望它会有所帮助
答案 4 :(得分:0)
试试这个。从早上开始,我试图解决这个问题,最后我做到了。我想分享它,可能对某人有用。 这是我的代码:
<?php
// I removed mysql query
require '../../../PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'xxxxx';
$mail->SMTPAuth = true;
$mail->Username = 'xxxx';
$mail->Password = 'xxxxx';
$mail->SMTPSecure = 'openssl';
$mail->Port = 587;
$mail->isHTML(true);
$mail->setFrom('from', 'title');
$mail->addAddress($row1['email'] , $row1['fullname']);
$mail->addAttachment('p2t-'.$row1['id'].'.pdf');
$mail->addEmbeddedImage('logo.png', 'logo_p2t');
$mail->addEmbeddedImage('web/'.$row1['photo'].'', 'logo_p2t1');
$mail->Subject = 'Thanks for registering with us';
$mail->Body = "
<html>
<head>
<title></title>
</head>
<body>
<div style='width:800px;background:#fff;border-style:groove;'>
<div style='width:50%;text-align:left;'><a href='your website url'> <img
src=\"cid:logo_p2t\" height=60 width=200;'></a></div>
<hr width='100%' size='2' color='#A4168E'>
<div style='width:50%;height:20px; text-align:right;margin-
top:-32px;padding-left:390px;'><a href='your url' style='color:#00BDD3;text-
decoration:none;'>
My Bookings</a> | <a href='your url' style='color:#00BDD3;text-
decoration:none;'>Dashboard </a> </div>
<h2 style='width:50%;height:40px; text-align:right;margin:0px;padding-
left:390px;color:#B24909;'>Booking Confirmation</h2>
<div style='width:50%;text-align:right;margin:0px;padding-
left:390px;color:#0A903B'> Booking ID:1150 </div>
<h4 style='color:#ea6512;margin-top:-20px;'> Hello, " .$row1['fullname']."
</h4>
<p>Thank You for booking with us.Your Booking Order is Confirmed Now. Please
find the trip details along with the invoice. </p>
<hr/>
<div style='height:210px;'>
<table cellspacing='0' width='100%' >
<thead>
<col width='80px' />
<col width='40px' />
<col width='40px' />
<tr>
<th style='color:#0A903B;text-align:center;'>" .$row1['car_title']."</th>
<th style='color:#0A903B;text-align:left;'>Traveller Info</th>
<th style='color:#0A903B;text-align:left;'>Your Pickup Details: </th>
</tr>
</thead>
<tbody>
<tr>
<td style='color:#0A903B;text-align:left;padding-bottom:5px;text-
align:center;'><img src=\"cid:logo_p2t1\" height='90' width='120'></td>
<td style='text-align:left;'>" .$row1['fullname']." <br> " .$row1['email']."
<br> " .$row1['phone']." <br>" .$row1['nearestcity']." </td>
<td style='text-align:left;'>" .$row1['pickupaddress']." <br> Pickuptime:"
.$row1['pickuptime']." <br> Pickup Date:" .$row1['pickupdate']."
<br> Dropoff: " .$row1['dropoffaddress']."</td>
</tr>
<tr>
</tbody>
</table>
<hr width='100%' size='1' color='black' style='margin-top:10px;'>
<table cellspacing='0' width='100%' style='padding-left:300px;'>
<thead>
<tr>
<th style='color:#0A903B;text-align:right;padding-bottom:5px;width:70%'>Base
Price:</th>
<th style='color:black;text-align:left;padding-bottom:5px;padding-
left:10px;width:30%'>" .$row1['base_price']."</th>
</tr>
<tr>
<th style='color:#0A903B;text-align:right;padding-bottom:5px;'>GST(5%):</th>
<th style='color:black;text-align:left;padding-bottom:5px;padding-
left:10px;'>" .$row1['gst']."</th>
</tr>
<tr>
<th style='color:#0A903B;text-align:right;'>Total Price:</th>
<th style='color:black;text-align:left;padding-bottom:5px;padding-
left:10px;'>" .$row1['total_price']."</th>
</tr>
</thead>
</table>
</div>
</div>
</body>
</html>";
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
echo "<script type='text/javascript'> document.location = './'; </script>";
?>
输出是: Image of the custom html code embedded in mail body. 谢谢。 一切顺利。