简体   繁体   English

使用php通过电子邮件生成具有动态值的html

[英]Email generated html with dynamic values using php

I have a format simple certificate. 我有一个格式简单的证书。 The certificate needs to be populated with values from the database and emailed. 需要使用数据库中的值填充证书并通过电子邮件发送。 Below is the quick fix I did. 以下是我所做的快速修复。 The problem is the certificates sent are not of one person instead of those queried. 问题是发送的证书不是一个人的证书,而是不是一个人的证书。

$query ="SELECT  r.email, r.LastName, r.OpNo, 
r.QuizNo From tbl_cert where Pass =1 AND Printed is null;
$result=mysql_query($query);
while( ($row= mysql_fetch_array($result)) ){
            $subject = "CPD Certificate";
    $email = $row['email'];
    $LastName = $row['LastName'];
    $OpNo = $row['OpNo'];
    $TestNo = $row['QuizNo'];

            $headers  = "From: online@example.com";
    $headers = 'MIME-Version: 1.0' . "\r\n"; 
    $headers .= 'Content-type: text/html; 
              charset=iso-8859-1' . "\r\n";

            $mail_body ='<html>...Here its were my my 
            html comes with values from table...</html>';

            if (mail($to, $subject, $mail_body, $headers)){
            $query = "UPDATE `tbl_cert` SET Printed = 1 
               WHERE CertificateNo = "  . $CertificateNo;

            $certresult=mysql_query($query);

            if ($certresult) {

        header('Location:tsCertlist.php');
        }

The problem is how to get the $mail_body to have both html and php within the while loop. 问题是如何使$ mail_body在while循环中同时包含html和php。 My generated form or certificate was the same for the 4 users who had Passed and had not printed their certificate. 我生成的表单或证书与4个已通过但未打印其证书的用户相同。

create a separate template file which contains your HTML and the places you want to insert a variable / replace something put in something like %REPLACE_FIRSTNAME% 创建一个单独的模板文件,其中包含您的HTML以及要插入变量的位置/替换%REPLACE_FIRSTNAME%之类的内容

Then use str_replace() to change your replacement vars with the db/input/whatever variables with the data you require. 然后使用str_replace()更改db / input / whatever变量和您所需数据的替换变量。

$mail_body = file_get_contents("templates/emailtemplate.htm");
$mail_body = str_replace("%REPLACE_FIRSTNAME%",value_from_DB,$template);

Sorted and if you ever want to change your layout just edit the template file accordingly. 排序后,如果要更改布局,只需相应地编辑模板文件。

$certNo = 0;
//get $certNo from data base
$mail_body ='<html>'.$certNo.'</html>';
//send

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM