php 邮件程序和 html 包含 php 变量

发布于 2024-11-23 18:54:39 字数 1006 浏览 3 评论 0原文

您好,我正在尝试使用 php 邮件程序类发送 html 电子邮件。问题是我想在我的电子邮件中包含 php 变量,同时使用 include 来保持事情井井有条。这是我的 php 邮件程序...

 $place = $data['place'];
 $start_time = $data['start_time'];

$mail->IsHTML(true);    // set email format to HTML
$mail->Subject = "You have an event today";
$mail->Body = file_get_contents('../emails/event.html');
$mail->Send(); // send message

我的问题是,是否可以在 event.html 中包含 php 变量?我尝试了这个但没有运气(下面是 event.html)..

<table width='600px' cellpadding='0' cellspacing='0'>
<tr><td bgcolor='#eeeeee'><img src='logo.png' /></td></tr>
<tr><td bgcolor='#ffffff'  bordercolor='#eeeeee'>
<div style='border:1px solid #eeeeee;font-family:Segoe UI,Tahoma,Verdana,Arial,sans-serif;padding:20px 10px;'>
<p style=''>This email is to remind you that you have an upcoming meeting at $place on $start_time.</p>
<p>Thanks</p>
</div>
</td></tr>
</table>

Hello I am trying to send html emails using php mailer class. The problem is i would like to incllude php variables in my email while using includes as to keep things organized. Heres my php mailer....

 $place = $data['place'];
 $start_time = $data['start_time'];

$mail->IsHTML(true);    // set email format to HTML
$mail->Subject = "You have an event today";
$mail->Body = file_get_contents('../emails/event.html');
$mail->Send(); // send message

my question is, is it possible to have php variables in event.html ? i tried this with no luck (below is event.html)..

<table width='600px' cellpadding='0' cellspacing='0'>
<tr><td bgcolor='#eeeeee'><img src='logo.png' /></td></tr>
<tr><td bgcolor='#ffffff'  bordercolor='#eeeeee'>
<div style='border:1px solid #eeeeee;font-family:Segoe UI,Tahoma,Verdana,Arial,sans-serif;padding:20px 10px;'>
<p style=''>This email is to remind you that you have an upcoming meeting at $place on $start_time.</p>
<p>Thanks</p>
</div>
</td></tr>
</table>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

爱的那么颓废 2024-11-30 18:54:39

是的,使用 include 和一个简短的辅助函数非常容易:

function get_include_contents($filename, $variablesToMakeLocal) {
    extract($variablesToMakeLocal);
    if (is_file($filename)) {
        ob_start();
        include $filename;
        return ob_get_clean();
    }
    return false;
}

$mail->IsHTML(true);    // set email format to HTML
$mail->Subject = "You have an event today";
$mail->Body = get_include_contents('../emails/event.php', $data); // HTML -> PHP!
$mail->Send(); // send message
  • get_include_contents 函数由 PHP include 文档提供< /a>,稍作修改以包含数组变量。


  • 重要提示:由于您的包含是在函数内处理的,因此 PHP 模板文件 (/emails/event.php) 的执行范围位于该函数的范围内(除了超级全局变量

    之外,没有立即可用的变量

  • 这就是为什么我 可以立即使用已添加extract($variablesToMakeLocal) — 它从 $variablesToMakeLocal 中提取所有数组键作为函数作用域中的变量,这意味着它们位于所包含文件的作用域内。< /p>

    由于您在 $data 数组中已经有了 placestart_time,我只是将其直接传递到函数中。您可能想知道这将提取 $data 中的所有键 - 您可能想要也可能不想要。

  • 请注意,现在您的模板文件正在作为 PHP 文件进行处理,因此所有相同的注意事项和语法规则都适用。您不应该将其暴露给外界编辑,并且必须使用 来输出变量,就像在任何 PHP 文件中一样.

Yes, very easily with include and a short helper function:

function get_include_contents($filename, $variablesToMakeLocal) {
    extract($variablesToMakeLocal);
    if (is_file($filename)) {
        ob_start();
        include $filename;
        return ob_get_clean();
    }
    return false;
}

$mail->IsHTML(true);    // set email format to HTML
$mail->Subject = "You have an event today";
$mail->Body = get_include_contents('../emails/event.php', $data); // HTML -> PHP!
$mail->Send(); // send message
  • The get_include_contents function is courtesy of the PHP include documentation, modified slightly to include an array of variables.

  • Important: Since your include is processing within a function, the scope of execution of the PHP template file (/emails/event.php) is in that function's scope (no variables immediately available besides super globals

  • That is why I have added extract($variablesToMakeLocal) — it extracts all array keys from $variablesToMakeLocal as variables in the function's scope, which in turn means they are within scope of the file being included.

    Since you already had place and start_time in the $data array, I simply passed that straight into the function. You may want to be aware that this will extract all keys within $data — you may or may not want that.

  • Note that now your template file is processing as a PHP file, so all the same caveats and syntax rules apply. You should not expose it to be edited by the outside world, and you must use <?php echo $place ?> to output variables, as in any PHP file.

我的奇迹 2024-11-30 18:54:39

有几种方法可以做到这一点:

令牌模板

<p> Some cool text %var1%,, %var2%,etc...</p>

令牌邮件程序

$mail->Body = strtr(file_get_contents('path/to/template.html'), array('%var1%' => 'Value 1', '%var2%' => 'Value 2'));

缓冲区模板

<p> Some cool text $var1,, $var2,etc...</p>

缓冲区邮件程序

$var1 = 'Value 1';
$var2 = 'Value 2';
ob_start();
include('path/to/template.php');
$content = ob_get_clean();
$mail->Body = $content;

Couple ways to do it:

Token Template

<p> Some cool text %var1%,, %var2%,etc...</p>

Token Mailer

$mail->Body = strtr(file_get_contents('path/to/template.html'), array('%var1%' => 'Value 1', '%var2%' => 'Value 2'));

Buffer Template

<p> Some cool text $var1,, $var2,etc...</p>

Buffer Mailer

$var1 = 'Value 1';
$var2 = 'Value 2';
ob_start();
include('path/to/template.php');
$content = ob_get_clean();
$mail->Body = $content;
笑脸一如从前 2024-11-30 18:54:39

您可以将变量放入 html 电子邮件中,然后执行 string_replace ,以便内容而不是变量出现在电子邮件中:

try {
    $mail = new PHPMailer(true);
    $body = file_get_contents('phpmailer/subdir/contents.html');
    $body = str_replace('$fullname', $fullname, $body);
    $body = str_replace('$title', $title, $body);
    $body = str_replace('$email', $email, $body);
    $body = str_replace('$company', $company, $body);
    $body = str_replace('$address', $address, $body);
    // strip backslashes
    $body = preg_replace('/\\\\/','', $body);
    // mail settings below including these:
    $mail->MsgHTML($body);
    $mail->IsHTML(true); // send as HTML
    $mail->CharSet="utf-8"; // use utf-8 character encoding
}

这是对我有用的设置。也许它不干燥,但它有效。

You can put variables in the html email and then do a string_replace so the contents appear in the email instead of the variables:

try {
    $mail = new PHPMailer(true);
    $body = file_get_contents('phpmailer/subdir/contents.html');
    $body = str_replace('$fullname', $fullname, $body);
    $body = str_replace('$title', $title, $body);
    $body = str_replace('$email', $email, $body);
    $body = str_replace('$company', $company, $body);
    $body = str_replace('$address', $address, $body);
    // strip backslashes
    $body = preg_replace('/\\\\/','', $body);
    // mail settings below including these:
    $mail->MsgHTML($body);
    $mail->IsHTML(true); // send as HTML
    $mail->CharSet="utf-8"; // use utf-8 character encoding
}

This is the setup that worked for me. It not DRY perhaps, but it works.

香草可樂 2024-11-30 18:54:39

使用 prodigitalson 的令牌方法,以下内容对我有用。 PHP 代码是:

$e = "[email protected]";
$sc = "2sbd2152g#!fsf";
$body = file_get_contents("../email/recovery_email.html");
$body  = eregi_replace("%e%" ,$sc, $body);
$body  = eregi_replace("%sc%" ,$sc, $body);
$mail->MsgHTML($body);

HTML 只是:

<p>Click this link: www.mysite.com/recover.php?e=%e%&sc=%sc%<p>

在我的例子中,eregi_replacestrtr 效果更好 - (后者根本不起作用)。

Using prodigitalson's token method, the following worked for me. PHP code was:

$e = "[email protected]";
$sc = "2sbd2152g#!fsf";
$body = file_get_contents("../email/recovery_email.html");
$body  = eregi_replace("%e%" ,$sc, $body);
$body  = eregi_replace("%sc%" ,$sc, $body);
$mail->MsgHTML($body);

The HTML was just:

<p>Click this link: www.mysite.com/recover.php?e=%e%&sc=%sc%<p>

The eregi_replace worked better that strtr in my case - (the latter didn't work at all).

乖乖哒 2024-11-30 18:54:39

也许有点晚了,但这是我经常使用的方法。
(在 stackoverflow 上找到了这个,但找不到它的链接。所以这个解决方案的积分不适合我!)

首先要做的就是替换 html 电子邮件中的变量,如下所示:

<p style=''>This email is to remind you that you have an upcoming meeting at {{ place }} on {{ start_time }}.</p>

然后在 PHP 中,您首先获取内容邮件模板并将其分配给一个变量,比如说$mailBody。然后使用您想要插入到 HTML 电子邮件模板中的变量创建一个新数组。设置后,您可以使用循环来获取电子邮件内的正确变量。

$mailBody = file_get_contents('../emails/event.html');
//make the new array
$mailVariables = array();
//Assign needed variables to the new array
$mailVariables['place'] = $data['place'];
$mailVariables['start_time'] = $data['start_time']; 

foreach($mailVariables as $key => $value) {
    $mailBody = str_replace('{{ '.$key.' }}', $value, $mailBody);
}

$mail->IsHTML(true);    // set email format to HTML
$mail->Subject = "You have an event today";
$mail->Body = $mailBody;
$mail->Send(); // send message

这样你只需几行额外的代码就可以保持一切干净。
我什至用它来发送包含所有不同正文和大量变量的批量电子邮件,并且运行速度相当快。

希望有帮助。

Maybe a little bit late to the party, but here is the method I always use.
(Found this one somewhere on stackoverflow but can't find the link to it. so credits for this solution are not for me!)

First thing to do is replace the variables in html email like so:

<p style=''>This email is to remind you that you have an upcoming meeting at {{ place }} on {{ start_time }}.</p>

Then in PHP you first get the contents of the mailtemplate and assign it to a variable, lets say $mailBody. Then make a new array with the variables you would like to insert in the HTML email template. Once set, you can use a loop to get correct varibales inside the email.

$mailBody = file_get_contents('../emails/event.html');
//make the new array
$mailVariables = array();
//Assign needed variables to the new array
$mailVariables['place'] = $data['place'];
$mailVariables['start_time'] = $data['start_time']; 

foreach($mailVariables as $key => $value) {
    $mailBody = str_replace('{{ '.$key.' }}', $value, $mailBody);
}

$mail->IsHTML(true);    // set email format to HTML
$mail->Subject = "You have an event today";
$mail->Body = $mailBody;
$mail->Send(); // send message

This way you can keep everything clean with just a few lines of extra code.
I've used this even to send bulk emails with all diffrent body's and a lot of variables and it runs fairly quick.

Hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文