【问题标题】:php mail template variables from mysql来自mysql的php邮件模板变量
【发布时间】:2014-09-22 18:32:18
【问题描述】:

您好,我正在尝试在 mysql 中制作电子邮件模板,但是如何将变量从 mysql 解析到 php 脚本?

例子:

如果我在 mysql 中有一行名为“subject”和“title”的行,例如这里的文本:

你好 $UserFirstName

这是 $domain 的自动通知系统。 这封电子邮件是为了通知您,新账单已添加到您的帐户中


我使用mysqli连接

这是我现在使用的 php 文件源,但想在 mysql 中有模板:

<?php

$gp['template']->get_header();

if ($_SESSION['userlevel'] > 3) {

if ( isset ( $_POST['submit'] ) ) {

  if (empty($_POST["name"]))
    {$nameErr = $gp['lang_ALERT_NAME_REQUIRED'];
  }
  else {
  if (empty($_POST["username"]))
    {$usernameErr = $gp['lang_ALERT_USERNAME_REQUIRED'];
  }
  else {
    if (empty($_POST["email"]))
    {$emailErr = $gp['lang_ALERT_EMAIL_REQUIRED'];
  }
  else {

  $name = $_POST['name'];
  $address = $_POST['address'];
  $city = $_POST['city'];
  $zip = $_POST['zip'];
  $country = $_POST['country'];
  $website = $_POST['website'];
  $password = $_POST['password'];
  $userlevel = "1";
  $active = "1";
// Create a random salt
$random_salt = $gp['user']->createSalt();
// Create salted password (Careful with the chilli)
$password = hash('sha256', $random_salt . $password . $random_salt);
$username = $_POST['username'];
$email = $_POST['email'];
if ($insert_stmt = $gp['mysqli']->prepare("INSERT INTO ".$gp['settings_db_prefix']."users (name, username, email, password, salt, userlevel, active, address, city, zip, country, website) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"))
{
$insert_stmt->bind_param('ssssssssssss', $name, $username, $email, $password, $random_salt, $userlevel, $active, $address, $city, $zip, $country, $website);
$insert_stmt->execute();

// Send Email to user

            $urlpath = $gp['settings_url'];
            $password = $_POST["password"];
            $userdata_name = $gp['session_userdata']['name'];
            $mail_to = $_POST["email"];


            $mail_from = $gp['settings_mail_from'];
            $mail_subject = 'GamePanel - New user';

            $message = '
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
            <html xmlns="http://www.w3.org/1999/xhtml">
             <head>
              <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
              <title>'.$mail_subject.'</title>
              <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
            </head>
              <body bgcolor="#758899"> 
                <center>

                <table border="0" style="display: inline-block;line-height: 39px;margin: 0 0 16px 0;padding: 7px 16px;padding-top: 7px;letter-spacing: -1px;color: #fff;background-color: #21282f;font-family: Purista,sans-serif;font-size: 36px;font-style: normal;font-weight: bold;text-transform: uppercase;">
                  <tr>
    <td height="81"><img src="http://'.$urlpath.'/themes/default/images/logo_white.png" width="315" /></td>
  </tr>
</table>

                <table bgcolor="" align="center" cellpadding="0" cellspacing="0" width="100%" border="0" style="padding: 0px; border: 0px; padding-top: 0px;">
                 <tr>
                 <td height="218">
                 <table bgcolor="#758899" align="center" cellpadding="0" cellspacing="0" border="0" style="padding:40px 10px 40px 10px;">
                 <tr>
                 <td>
                    <table bgcolor="" align="center" cellpadding="0" cellspacing="0" width="600" border="0" style="padding:0px;border:0px;">
                     <tr>
                      <td bgcolor="" style="background-color: #191c20;color: #d5dde5;padding: 8px 12px; margin-top: 0px;font-family: Purista,sans-serif;font-size: 16px;font-style: normal;font-weight: 700;">
                       '.$mail_subject.'
                      </td>
                     </tr>
                     <tr>
                      <td bgcolor="" style="float: left;width: 685px;background-color: #2e3741;padding: 10px 10px 0;margin-top: 1px;color: #FFFFFF; padding-bottom:10px;">
                        To login go to <a href="'.$urlpath.'" style="color:#d5dde5;">'.$urlpath.'</a><br /><br />
                        Your password is: '.$_POST["password"].'
                        <br />
                        <br />
                        Please login and update your information on Account / My Account.
                        <br /><br />Best Regards GamePanel.
                      </td>
                     </tr>
                     <tr>
                      <td bgcolor="" style="float: left;width: 700px;background-color: #3c4655;margin-bottom: 25px;margin-top: 1px; padding-left:5px; padding-top:5px; padding-bottom:5px;color: #FFFFFF;font-family: Purista,sans-serif;font-weight: bold; font-size:14px;">
                       Copyright © 2014 <a href="http://game-panel.dk" style="color:#d5dde5;">GamePanel</a>, All rights reserved.
                      </td>
                     </tr>
                    </table>
                 </td>
                 </tr>
                </table>
                </td>
                </tr>
                </table>
                </center>
              </body> 
            </html>
            ';

            $headers  = "From: ".$mail_from."\r\n"; 
            $headers .= "Content-type: text/html\r\n"; 
            mail($mail_to, $mail_subject, $message, $headers);

            header("Location: ?success=1");
}
else
{
            header("Location: ?registrationfailed=1");
}
}
}
}
}

?>

【问题讨论】:

  • 你能解释得更好吗?是否要将 php 变量替换为从 mysql 获得的字符串模板?还是相反?
  • 如果我有一个名为 $domain = $gp['settings_url']; 的变量在 php 中显示 url 如果我回显它,但我如何使用 example: $domain in the mysql template 从 php 获取变量?希望你现在明白了 :) 正如你现在在我的脚本中看到的那样,我在 php 中拥有一切,但我想在 mysql 中拥有所有模板。

标签: php mysql email mysqli


【解决方案1】:

如果我理解正确,您可以尝试以这种方式替换占位符(根据需要编辑此代码):

$templateString = $gp['mysqli']->query('select template from tpl_table')->template;
$bodyMessage = str_replace(array('%TITLE%','%FOO%'), array($yourTitle,$someFoo), $templateString);

而mysql中的模板格式应该是这样的:

<html><head><title>%TITLE%</title></head><body>Bar: %FOO%</body></html>

希望这就是你要找的东西

【讨论】:

  • 是的,我想我明天会尝试,然后我会告诉你它有效 :) 并感谢到目前为止的帮助 :)
猜你喜欢
  • 2014-06-25
  • 1970-01-01
  • 2015-12-11
  • 1970-01-01
  • 2017-10-16
  • 1970-01-01
  • 2015-09-02
  • 2014-09-12
  • 1970-01-01
相关资源
最近更新 更多