13

こんにちは、php メーラー クラスを使用して 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

私の質問は、php 変数を event.html に含めることは可能ですか? 私はこれを試してみましたが、うまくいきませんでした (以下は 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

はい、インクルードと短いヘルパー関数で非常に簡単です:

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関数はPHPincludeドキュメントの厚意によるもので、変数の配列を含めるようにわずかに変更されています。

  • 重要:インクルードは関数内で処理されているため、PHP テンプレート ファイル ( ) の実行のスコープはその関数のスコープ内にあります (スーパー グローバル/emails/event.php以外にすぐに使用できる変数はありません)。

  • それが私が追加した理由ですextract($variablesToMakeLocal)— すべての配列キーを$variablesToMakeLocal関数のスコープ内の変数として抽出します。これは、それらがインクルードされるファイルのスコープ内にあることを意味します。

    既にplacestart_time$data配列にあるので、単純にそれを関数に直接渡しました。これにより、内部のすべてのキーが抽出されることに注意して$dataください。それが必要な場合とそうでない場合があります。

  • テンプレート ファイルが PHP ファイルとして処理されるようになったため、同じ注意事項と構文規則がすべて適用されることに注意してください。外部から編集できるように公開してはなら<?php echo $place ?>、他の PHP ファイルと同様に、変数を出力するために使用する必要があります。

于 2011-07-14T04:07:39.987 に答える
11

それを行ういくつかの方法:

トークン テンプレート

<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;
于 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
}

これは私のために働いたセットアップです。おそらくDRYではありませんが、機能します。

于 2013-10-09T05:04:23.203 に答える