使用PHPMailer和html模板发送html电子邮件 [英] Send html emails using PHPMailer and html templates

查看:262
本文介绍了使用PHPMailer和html模板发送html电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从我的联系人发送的电子邮件(使用PHPMailer)发送到一个很好的html模板(实际上是phtml)。



html模板所以没有传输问题



不是工作:变量(消息,电话号码等)不反映在我的html模板的正文。
我在html模板中尝试了几件事情,但没有成功:<?= htmlspecialchars($ message)?> #消息#<?php echo $ _POST ['message']?>



这是什么问题?



谢谢,

以下是PHPMailer代码:

 <?php 

要求'PHPMailer / PHPMailerAutoload.php';

$ mail = new PHPMailer;
$ mail-> CharSet ='utf-8';
$ body = file_get_contents('htmlemail.phtml');

//启用SMTP调试。
$ mail-> SMTPDebug = false;
//将PHPMailer设置为使用SMTP。
$ mail-> isSMTP();
//设定SMTP主机名称
$ mail-> Host =smtp.sendgrid.net;
//如果SMTP主机需要身份验证才能发送电子邮件,则将其设置为true
$ mail-> SMTPAuth = true;
//提供用户名和密码
$ mail->用户名=;
$ mail-> Password =;
//如果SMTP需要TLS加密,则将其设置为
$ mail-> SMTPSecure =tls;
//设置TCP端口连接到
$ mail->端口= 587;

$ mail-> From = $ _POST ['email'];
$ mail-> FromName = $ _POST ['first_name']。 。 $ _POST [姓氏];

$ mail-> addAddress(@ gmail.com);
// CC和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'=>消息已成功发送,'status'=> 1);
}

/ *发送内容类型标题* /
标题('Content-Type:application / json');

/ *以json * / $ b $的形式发送响应echo json_encode($ response);

?>


解决方案

使用ob_start

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

或者

<您也可以使用模板方法来生成多种用途的电子邮件正文。



例如



您的html模板中有这样的变量:
$ b


感谢{NAME}与我们联系。



您的电话号码是{PHONE}


然后在调用phpmailer之前,创建一个数组来处理电子邮件正文:

pre $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'$' $ b'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);


code


$ b这样你的电子邮件就会拥有你所有的动态内容需要在身体里。


I'm trying to get the emails sent from my contact (using PHPMailer) sent in a nice html template (phtml actually).

What works: I receive the html template so no issue with the transmission

What does not work: The variables (message, phone number, etc) are not reflected in the body of my html template. I have tried several things in the html template, without success: <?= htmlspecialchars($message) ?> and #message# and <?php echo$_POST['message'] ?>

What is the issue?

Thanks,

Here is the PHPMailer code:

<?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);

?>

解决方案

Using ob_start

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

Or

You can also use a templating method to generate the email body for multiple uses.

E.g.

In your html template, have variables assigned like this:

Thank you {NAME} for contacting us.

Your phone number is {PHONE}

Then before calling your phpmailer, create an array to process the email body:

$email_vars = array(
    'name' => $_POST['name'],
    'phone' => $_POST['phone'],
);

And finally, with phpmailer...

$body = file_get_contents('htmlemail.phtml');

if(isset($email_vars)){
    foreach($email_vars as $k=>$v){
        $body = str_replace('{'.strtoupper($k).'}', $v, $body);
    }
}

This way your emails will have all the dynamic content you need in the body.

这篇关于使用PHPMailer和html模板发送html电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆