使用PHP,HTML和JSON发送电子邮件

时间:2022-10-16 13:03:11

I want to send an email automatically using some static and some dynamic data. So this is the process:

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

  1. In my main *.php I´m fetching the data from my ddbb and creating the JSON.
  2. 在我的主* .php中,我从ddbb获取数据并创建JSON。

  3. I´m sending this JSON to another *.php to manage this data an insert it in my <html> (Second *.php).
  4. 我将此JSON发送到另一个* .php来管理这些数据,并将其插入到我的(Second * .php)中。

  5. Again in the first *.php I´m preparing the mail options and sending it. Here is the code.
  6. 再次在第一个* .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);

Then, in the second.php:

然后,在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>

Finally, again in the first.php

最后,再次在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);

But my problem is that second.php is not fetching the JSON and the email is only showing the static html, without any data.

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

Any solution to fetch the JSON from the second.php?

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

2 个解决方案

#1


1  

<php echo $first_variable ?>

Should be:

<?php echo $first_variable; ?>

#2


0  

Well, finally I´ve decided to do everything in the same *.php, so I don´t need to send and receive anything.

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

The way I´m creating the HTML with dynamic values is:

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

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

It´s a pretty long HTML message, but I wasn´t able to do using the other way. I will keep this question opened just in case someone knows another method to do.

这是一个相当长的HTML消息,但我无法使用其他方式。我将保持打开这个问题,万一有人知道另一种方法。

#1


1  

<php echo $first_variable ?>

Should be:

<?php echo $first_variable; ?>

#2


0  

Well, finally I´ve decided to do everything in the same *.php, so I don´t need to send and receive anything.

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

The way I´m creating the HTML with dynamic values is:

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

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

It´s a pretty long HTML message, but I wasn´t able to do using the other way. I will keep this question opened just in case someone knows another method to do.

这是一个相当长的HTML消息,但我无法使用其他方式。我将保持打开这个问题,万一有人知道另一种方法。