使用 PHPMailer 和 html 模板发送 html 电子邮件

新手上路,请多包涵

我试图让我的联系人发送的电子邮件(使用 PHPMailer)以一个漂亮的 html 模板(实际上是 phtml)发送。

什么有效:我收到了 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);

?>

原文由 Greg 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1k
2 个回答

使用 ob_start

 ob_start();
include 'htmlemail.php';
$body = ob_get_clean();

要么

您还可以使用模板方法生成电子邮件正文以供多次使用。

例如

在您的 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);
    }
}

这样您的电子邮件将在正文中包含您需要的所有动态内容。

原文由 Onimusha 发布,翻译遵循 CC BY-SA 3.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>";
?>

输出是: 嵌入在邮件正文中的自定义 html 代码的图像。 谢谢。祝一切顺利。

原文由 KOTAGIRI GANGAPRASAD 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题