尝试使用 PHP 邮件程序发送响应式电子邮件
Trying to send a responsive email using PHP mailer
我在 php 文件中有一个响应式电子邮件模板,并尝试使用 PHP 邮件程序发送它但没有成功。我的代码看起来像这样。
$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.
您看到的问题是 file_get_contents
从字面上获取文件的内容,而不是 运行 作为 PHP 脚本。有几种方法可以解决这个问题。
您可以 include
将文件分配给变量,如下所示:
$body = include 'functions/register-email.php';
$m->msgHTML($body, dirname(__FILE__));
这种方法的问题是您不能只将内容放在文件中,您需要 return
它作为一个值,因此您的模板将类似于:
<?php
$text = <<<EOT
<html>
<body>
<h1>$headline</h1>
</body>
</html>
EOT;
return $text;
一种更简单的方法是使用输出缓冲,这会使模板文件更简单:
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>
无论哪种方式,模板文件都可以访问您的局部变量并且插值将起作用。
还有其他选项,例如使用eval
,但效率低且容易
做错事。
使用输出缓冲是最简单的,但如果您想要更多的灵活性和控制,请使用模板语言,例如 Smarty or Twig.
为了使用 Zurb,您确实需要一个 CSS 内联器,例如 emogrifier 来 post 处理您呈现的模板,否则在 gmail 和其他低版本中会崩溃优质邮件客户端。
仅供参考,这个堆栈 - Zurb 模板、Smarty、emogrifier、PHPMailer - 正是我构建的 smartmessages.net 中使用的。
我在 php 文件中有一个响应式电子邮件模板,并尝试使用 PHP 邮件程序发送它但没有成功。我的代码看起来像这样。
$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.
您看到的问题是 file_get_contents
从字面上获取文件的内容,而不是 运行 作为 PHP 脚本。有几种方法可以解决这个问题。
您可以 include
将文件分配给变量,如下所示:
$body = include 'functions/register-email.php';
$m->msgHTML($body, dirname(__FILE__));
这种方法的问题是您不能只将内容放在文件中,您需要 return
它作为一个值,因此您的模板将类似于:
<?php
$text = <<<EOT
<html>
<body>
<h1>$headline</h1>
</body>
</html>
EOT;
return $text;
一种更简单的方法是使用输出缓冲,这会使模板文件更简单:
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>
无论哪种方式,模板文件都可以访问您的局部变量并且插值将起作用。
还有其他选项,例如使用eval
,但效率低且容易
做错事。
使用输出缓冲是最简单的,但如果您想要更多的灵活性和控制,请使用模板语言,例如 Smarty or Twig.
为了使用 Zurb,您确实需要一个 CSS 内联器,例如 emogrifier 来 post 处理您呈现的模板,否则在 gmail 和其他低版本中会崩溃优质邮件客户端。
仅供参考,这个堆栈 - Zurb 模板、Smarty、emogrifier、PHPMailer - 正是我构建的 smartmessages.net 中使用的。