尝试使用PHP邮件程序发送响应式电子邮件 [英] Trying to send a responsive email using PHP mailer

查看:87
本文介绍了尝试使用PHP邮件程序发送响应式电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在php文件中有一个响应电子邮件模板,试图用PHP邮件发送器发送失败,但没有成功.我的代码看起来像这样.

I have a responsive email template in a php file and trying to send it with PHP mailer with no success. 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>';
}

推荐答案

这与响应能力无关-这只是在Zurb CSS中使用CSS媒体查询的问题,它不需要任何javascript.

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.

您看到的问题是file_get_contents从字面上获取文件的内容,它不作为PHP脚本运行.有几种解决方法.

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. There are several ways to solve this.

您可以在将文件分配给变量的同时include,如下所示:

You can include the file while assigning it to a variable, like this:

$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:

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

,模板将很简单:

<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.

还有其他选项,例如使用eval,但是效率低下且易于实现 做错事.

There are other options such as using eval, but it's inefficient and easy to do things wrong.

使用输出缓冲是最简单的方法,但是如果您想要更大的灵活性和控制力,请使用模板语言,例如 Smarty 嫩枝.

Using output buffering is the simplest, but if you want lots more flexibility and control, use a templating language such as Smarty or Twig.

要使用Zurb,您确实需要CSS内衬,例如 emogrifier ,以便对渲染的图像进行后处理模板,否则,在gmail和其他低质量的邮件客户端中,一切都会崩溃.

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模板,Smarty,emogrifier,PHPMailer-正是 smartmessages.net 中使用的堆栈,是我建造的.

FYI, this stack - Zurb templates, Smarty, emogrifier, PHPMailer - is exactly what's used in smartmessages.net, which I built.

这篇关于尝试使用PHP邮件程序发送响应式电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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