使用 PHP、HTML 和 JSON 发送电子邮件
Send an email using PHP, HTML and JSON
我想使用一些静态和一些动态数据自动发送电子邮件。所以这是过程:
- 在我的主
*.php
中,我正在从我的 ddbb 中获取数据,并且
创建 JSON.
- 我正在将此 JSON 发送给另一个
*.php
以管理此数据和
将其插入我的 <html>
(第二个 *.php
)。
- 再次在第一个 *.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 echo $first_variable ?>
应该是:
<?php echo $first_variable; ?>
嗯,最后我决定在同一个地方做所有事情 *.php
,所以我不需要发送和接收任何东西。
我使用动态值创建 HTML 的方式是:
$email_text =
'<html>
<body>
...
<strong>'.$variable1.'</strong>
</body>
</html>';
这是一条很长的 HTML 消息,但我无法使用其他方式完成。我会一直打开这个问题,以防万一有人知道另一种方法。
我想使用一些静态和一些动态数据自动发送电子邮件。所以这是过程:
- 在我的主
*.php
中,我正在从我的 ddbb 中获取数据,并且 创建 JSON. - 我正在将此 JSON 发送给另一个
*.php
以管理此数据和 将其插入我的<html>
(第二个*.php
)。 - 再次在第一个 *.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 echo $first_variable ?>
应该是:
<?php echo $first_variable; ?>
嗯,最后我决定在同一个地方做所有事情 *.php
,所以我不需要发送和接收任何东西。
我使用动态值创建 HTML 的方式是:
$email_text =
'<html>
<body>
...
<strong>'.$variable1.'</strong>
</body>
</html>';
这是一条很长的 HTML 消息,但我无法使用其他方式完成。我会一直打开这个问题,以防万一有人知道另一种方法。