【问题标题】:Issue adding HTML tables into PHP Mail script将 HTML 表格添加到 PHP 邮件脚本中的问题
【发布时间】:2019-10-01 13:32:55
【问题描述】:

我有以下邮件脚本,其结构如下:

<?php
$to = 'example@testing';
$subject = 'Testing';
$from = 'test@email.com';

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Create email headers
$headers .= 'From: '.$from."\r\n".
    'Reply-To: '.$from."\r\n" .
    'X-Mailer: PHP/' . phpversion();

// Compose a simple HTML email message
$message = '<html><body>';
$message .= '<h1 style="color:#f40;">Hi!</h1>';
$message .= '<p style="color:#080;font-size:18px;">This is a test.</p>';
$message .= '</body></html>';

// Sending email
if(mail($to, $subject, $message, $headers)){
    echo 'Your mail has been sent successfully.';
} else{
    echo 'Unable to send email. Please try again.';
}
?>

然后我也想将下表放入正文中,但添加以下内容时,PHP 不起作用:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
        <style type="text/css">
            tr.header
            {
                font-weight:bold;
            }
            tr.alt
            {
                background-color: #777777;
            }
        </style>
        <script type="text/javascript">
            $(document).ready(function(){
               $('.striped tr:even').addClass('alt');
            });
        </script>
        <title></title>
    </head>
    <body>
        <?php

            $server = mysql_connect("localhost","root", "");
            $db =  mysql_select_db("MyDatabase",$server);
            $query = mysql_query("SELECT first_name, last_name, sign_date FROM Table1 WHERE sign_date = NOW()");
        ?>
        <table class="striped">
            <tr class="header">
                <td>first_name</td>
                <td>last_name</td>
                <td>sign_date</td>
            </tr>
            <?php
               while ($row = mysql_fetch_array($query)) {
                   echo "<tr>";
                   echo "<td>".$row[first_name]."</td>";
                   echo "<td>".$row[last_name]."</td>";
                   echo "<td>".$row[sign_date]."</td>";
                   echo "</tr>";
               }

            ?>
        </table>

                <?php

            $server = mysql_connect("localhost","root", "");
            $db =  mysql_select_db("MyDatabase",$server);
            $query = mysql_query("SELECT employee_id, job_title, address FROM Table1 WHERE sign_date = NOW()");
        ?>
        <table class="striped">
            <tr class="header">
                <td>employee_id</td>
                <td>job_title</td>
                <td>address</td>
            </tr>
            <?php
               while ($row = mysql_fetch_array($query)) {
                   echo "<tr>";
                   echo "<td>".$row[employee_id]."</td>";
                   echo "<td>".$row[job_title]."</td>";
                   echo "<td>".$row[address]."</td>";
                   echo "</tr>";
               }

            ?>
        </table>

    </body>
</html>

如何简单地将两者相加。将两者结合起来时似乎没有读取PHP并被忽略,让两个脚本一起工作的最有效方法是什么?

【问题讨论】:

  • 向我们展示究竟是什么 “但是当添加以下内容时” 看起来是这样的。
  • 是否可以将整个表格 HTML 脚本及其格式和 PHP 添加到 PHP 电子邮件函数中
  • 旁注: 添加 javascript 以在电子邮件中发送将无济于事。无论如何,它们都会被剥离以避免 XSS 攻击。

标签: php html email


【解决方案1】:

您可以像这样组合这两个部分 - PHP 代码需要在发送邮件之前运行(我发现 "but when adding the following, the PHP does not work" 有点混乱)

<?php

    $server = mysql_connect("localhost","root", "");
    $db =  mysql_select_db("MyDatabase",$server);


    $to = 'example@testing';
    $subject = 'Testing';
    $from = 'test@email.com';

    // To send HTML mail, the Content-type header must be set
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    // Create email headers
    $headers .= 'From: '.$from."\r\n".
        'Reply-To: '.$from."\r\n" .
        'X-Mailer: PHP/' . phpversion();


    $message="
        <html>
            <head>
            <title></title>
            <style>
                tr.header { font-weight:bold; }
                tr.alt { background-color: #777777; }
                tr:nth-of-type(even) td{background-color:#777777}
            </style>
            </head>
        <body>
            <table class='striped'>
                <tr class='header'>
                    <td>first_name</td>
                    <td>last_name</td>
                    <td>sign_date</td>
                </tr>";


    $query = mysql_query('SELECT first_name, last_name, sign_date FROM Table1 WHERE sign_date = NOW()');
    while ( $row = mysql_fetch_array( $query ) ) {
        $message.='
                <tr>
                    <td>'.$row['first_name'].'</td>
                    <td>'.$row['last_name'].'</td>
                    <td>'.$row['sign_date'].'</td>
                </tr>';
    }

    $message.="
            </table>

            <table class='striped'>
                <tr class='header'>
                    <td>employee_id</td>
                    <td>job_title</td>
                    <td>address</td>
                </tr>";


        $query = mysql_query('SELECT employee_id, job_title, address FROM Table1 WHERE sign_date = NOW()');
        while ($row = mysql_fetch_array($query)) {
            $message.='
                <tr>
                    <td>'.$row[employee_id].'</td>
                    <td>'.$row[job_title].'</td>
                    <td>'.$row[address].'</td>
                </tr>';
        }


    $message.="
            </table>
        </body>
    </html>
    ";



    // Sending email
    if(mail($to, $subject, $message, $headers)){
        echo 'Your mail has been sent successfully.';
    } else{
        echo 'Unable to send email. Please try again.';
    }
?>

【讨论】:

  • 有没有办法限制表格的宽度?目前它尽量符合每个设备的宽度,如果屏幕太小,它看起来有点扭曲。
  • 你可以设置一个明确的宽度 - 或者使用 css 来根据屏幕大小调整它的大小。
  • 我可以在脚本中使用任何发送电子邮件,它说什么重要吗?
  • 我不知道你最后的评论是什么意思,对不起!请解释一下。
  • 这说什么重要吗? $from = 'test@email.com';
【解决方案2】:

当您将 HTML 添加到正文时,它会变成一个字符串。 字符串可以将 php 变量转换为字符串。但无法执行php代码。 您必须提前执行 PHP 代码。将 PHP 代码中的数据添加到 HTML,然后将 HTML 添加到消息正文。

编辑:

这是一个如何工作的例子:

<?php

$html = generateHTML();

$to = 'example@testing';
$subject = 'Testing';
$from = 'test@email.com';

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Create email headers
$headers .= 'From: '.$from."\r\n".
    'Reply-To: '.$from."\r\n" .
    'X-Mailer: PHP/' . phpversion();

// Compose a simple HTML email message
$message = $html;


// Sending email
if(mail($to, $subject, $message, $headers)){
    echo 'Your mail has been sent successfully.';
} else{
    echo 'Unable to send email. Please try again.';
}

function generateHTML(){
    $html = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script><style type="text/css">tr.header{font-weight:bold;}tr.alt{background-color: #777777;}</style><script type="text/javascript">$(document).ready(function(){$(".striped tr:even").addClass("alt");});</script><title></title></head><body><table class="striped"><tr class="header"><td>first_name</td><td>last_name</td><td>sign_date</td></tr>';

    $server = mysql_connect("localhost","root", "");
            $db =  mysql_select_db("MyDatabase",$server);
            $query = mysql_query("SELECT first_name, last_name, sign_date FROM Table1 WHERE sign_date = NOW()");

    while ($row = mysql_fetch_array($query)) {
        $html.="<tr>";
        $html.="<td>".$row[first_name]."</td>";
        $html.="<td>".$row[last_name]."</td>";
        $html.="<td>".$row[sign_date]."</td>";
        $html.="</tr>";
    }

$html.='</table><table class="striped"><tr class="header"><td>employee_id</td>td>job_title</td><td>address</td></tr>';

$server = mysql_connect("localhost","root", "");
            $db =  mysql_select_db("MyDatabase",$server);
            $query = mysql_query("SELECT employee_id, job_title, address FROM Table1 WHERE sign_date = NOW()");

while ($row = mysql_fetch_array($query)) {
    $html.="<tr>";
    $html.="<td>".$row[employee_id]."</td>";
    $html.="<td>".$row[job_title]."</td>";
    $html.="<td>".$row[address]."</td>";
    $html.="</tr>";
}
$html.= '</table></body></html>';
return $html;
}
?>

【讨论】:

  • 我似乎在网上收到错误“Unexpected T_PRIVATE”:private function generateHTML(){
  • 抱歉,忘记了我们不在课堂环境中。它只是函数,而不是私有函数。已编辑。
猜你喜欢
  • 1970-01-01
  • 2011-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多