【问题标题】:Send an email using PHP, HTML and JSON使用 PHP、HTML 和 JSON 发送电子邮件
【发布时间】:2016-11-03 22:26:11
【问题描述】:

我想使用一些静态和一些动态数据自动发送一封电子邮件。所以这是过程:

  1. 在我的主要*.php 中,我正在从我的 ddbb 和 创建 JSON。
  2. 我正在将此 JSON 发送到另一个 *.php 以管理此数据和 将其插入我的<html>(第二个*.php)。
  3. 再次在第一个 *.php 中,我正在准备邮件选项并发送 它。这是代码。

first.php

...   
$query1= mysqli_query($connect, "SELECT * FROM table1 WHERE id='".$field1."'");
$row1= mysqli_fetch_array($query1);
$data_row1[] = $row1;

$query2= mysqli_query($connect, "SELECT * FROM table2 WHERE id='".$field2."'");
$row2= mysqli_fetch_array($query2);
$data_row2[] = $row2;

$data = array(
    "data_row1" => $data_row1,
    "data_row2" => $data_row2
);

$json_data = json_encode($data);
$url = 'https://webpage.com/folder/second.php?';
$variables = 'data='.$json_data;

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $variables);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$email_text = curl_exec($ch);
curl_close($ch);

那么,在second.php

<?php
    $result = $_GET['data'];
    $data = json_decode($result);

    $row_data1[] = $data['row_data1'];
    $row_data2[] = $data['row_data2'];
    $name= $row_data1['var1']." ".$row_data1['var2'];
    $city= $row_data2['var2'];
    ...
?>
<html>
    ... Some html printing the dynamic values using <?php echo $name; ?>
</html>

最后还是first.php

...
$to = $destionation_email;
$subject = "The email subject";
$headers = "From: noreply@webpage.com".strip_tags($_POST['req-email']) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";

mail($to, $subject, $email_text, $headers);

但我的问题是second.php 没有获取 JSON,并且电子邮件仅显示静态 html,没有任何数据。

second.php 获取 JSON 的任何解决方案?

【问题讨论】:

    标签: php html json email html-email


    【解决方案1】:
    <php echo $first_variable ?>
    

    应该是:

    <?php echo $first_variable; ?>
    

    【讨论】:

    • 第一个“?”有但没有“;”。我会试试的;)
    • 当我发布它时它并不存在,假设你的意思是它在实际代码中。
    • 是的,它在我的代码中,但在这里。否则,添加“;”将不起作用。
    【解决方案2】:

    好吧,最后我决定在同一个*.php 中做所有事情,所以我不需要发送和接收任何东西。

    我使用动态值创建 HTML 的方式是:

    $email_text = 
        '<html>
            <body>
            ...
                <strong>'.$variable1.'</strong>
            </body>
        </html>';
    

    这是一个相当长的 HTML 消息,但我无法使用其他方式。我会保留这个问题,以防有人知道另一种方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-23
      • 2015-02-23
      • 1970-01-01
      相关资源
      最近更新 更多