简体   繁体   English

尝试使用PHP邮件程序发送响应式电子邮件

[英]Trying to send a responsive email using PHP mailer

I have a responsive email template in a php file and trying to send it with PHP mailer with no success. 我在php文件中有一个响应电子邮件模板,试图用PHP邮件发送器发送失败,但没有成功。 My code looks like this. 我的代码如下所示。

$m = new PHPMailer;
$m ->isSMTP();
$m->SMTPAuth=true;

// debugging
// $m->SMTODebug=1
// endof debug
$m->Host="smtp.gmail.com";
$m->Username="example@gmail.com";
$m->Password="blahblah";
$m->SMTPSecure='ssl';
$m->Port=465;
$m->isHtml(true);

$m->Subject = 'Welcome to Efie';
$m->msgHTML(file_get_contents('functions/register-email.php'), dirname(__FILE__));
$m->FromName="Contact Form Efie";
$m->AddAddress($email,$fname);
if($m->send()) {
    echo '<p class="errors bg-success text-success">Email   Received</p>';
}

This isn't anything to do with it being responsive - that's just a matter of using the CSS media queries in the Zurb CSS, it doesn't need any javascript. 这与响应能力没有任何关系-只需在Zurb CSS中使用CSS媒体查询即可,它不需要任何JavaScript。

The problem you're seeing is that file_get_contents literally gets the contents of the file, it does not run it as a PHP script. 您看到的问题是file_get_contents从字面上获取文件的内容,它没有作为PHP脚本运行。 There are several ways to solve this. 有几种解决方法。

You can include the file while assigning it to a variable, like this: 您可以在将文件分配给变量时include文件,如下所示:

$body = include 'functions/register-email.php';
$m->msgHTML($body, dirname(__FILE__));

The problem with this approach is that you can't just have content sitting in the file, you need to return it as a value, so your template would be something like: 这种方法的问题在于,您不能只将内容放在文件中,而是需要将其作为值return ,因此您的模板应类似于:

<?php
$text = <<<EOT
<html>
<body>
<h1>$headline</h1>
</body>
</html>
EOT;
return $text;

An easier approach is to use output buffering, which makes the template file simpler: 一种更简单的方法是使用输出缓冲,这使模板文件更简单:

ob_start();
include 'functions/register-email.php';
$body = ob_get_contents();
ob_end_clean();
$m->msgHTML($body, dirname(__FILE__));

and the template would be simply: 模板将很简单:

<html>
<body>
<h1><?php echo $headline; ?></h1>
</body>
</html>

Either way, the template file will have access to your local variables and interpolation will work. 无论哪种方式,模板文件都可以访问您的局部变量,插值将起作用。

There are other options such as using eval , but it's inefficient and easy to do things wrong. 还有其他选项,例如使用eval ,但是效率低下且容易出错。

Using output buffering is the simplest, but if you want lots more flexibility and control, use a templating language such as Smarty or Twig . 使用输出缓冲是最简单的方法,但是如果您想要更大的灵活性和控制力,请使用诸如SmartyTwig之类的模板语言。

For working with Zurb, you really need a CSS inliner such as emogrifier to post-process your rendered template, otherwise things will fall apart in gmail and other low-quality mail clients. 为了使用Zurb,您确实需要CSS内衬(例如emogrifier)来对渲染的模板进行后处理,否则,在gmail和其他低质量的邮件客户端中,这些东西会分崩离析。

FYI, this stack - Zurb templates, Smarty, emogrifier, PHPMailer - is exactly what's used in smartmessages.net , which I built. 仅供参考,这个堆栈-Zurb模板,Smarty,emogrifier,PHPMailer-正是我构建的smartmessages.net中使用的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM