13

您好我正在尝试使用 php mailer 类发送 html 电子邮件。问题是我想在我的电子邮件中包含 php 变量,同时使用包含来保持事情井井有条。这是我的 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>
4

4 回答 4

21

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.

于 2011-07-14T04:07:39.987 回答
11

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;
于 2011-07-14T04:13:16.743 回答
3

您可以将变量放在 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
}

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

于 2013-10-09T05:04:23.203 回答
1

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

$e = "gsmith@gmail.com";
$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_replace效果更好strtr- (后者根本不起作用)。

于 2013-04-19T06:19:00.823 回答