【问题标题】:how to email dynamic block of html code?如何通过电子邮件发送 html 代码的动态块?
【发布时间】:2016-11-06 17:16:09
【问题描述】:

您好,我想知道是否有任何方法可以将一段 html 代码发送到电子邮件地址。我在名为“sum”的外部 javaScript 文件中设置了一个变量,并且我在我的 html 代码中调用该变量,因此 html 块的一部分正在被动态填充。我不知道如何以普通的 php 方式发送电子邮件,所以我只是想知道我是否可以将整个 html 代码块发送到电子邮件地址。

这是我要发送的整个 html 代码块:

<div class="select-location-page">
      <h2>Where should we meet you?</h2>
      <p class="location-sub-header">Fill out this form and we will meet you within 1 hour!</p>


      <form action="../mail.php" method="POST" class="location-form">

            <div class="device-details">
              <h2>Device: iPhone5</h2>
              <h2>Repair Cost: $<span id="result">0</span></h2>
            </div>

            <input name="name" type="text" id="name" placeholder="Name" maxlength="20">
            <input name="number" type="text" id="number" placeholder="Number" maxlength="15">
            <input name="address" type="text" id="address" placeholder="Address" maxlength="40">
            <input name="city" type="text" id="city" placeholder="City" maxlength="20">
            <input name="zip" type="text" id="state" placeholder="Zip" maxlength="20">

            <input name="device" type="hidden" id="device" value="iPhone5" maxlength="20">


            <div class="submit-btn">
                <input type="submit"  value="Submit Request" />
            </div>

      </form>

      <script>
        document.getElementById("result").innerHTML = localStorage.getItem("sum");
      </script>

  </div>

这是我尝试创建的 php 文件,除了修复成本外,它都可以工作(我尝试在 html 代码中声明一个 php 变量,然后将其放入 mail.php 文件,但它没有工作)

<?php
$name = $_POST['name'];
$number = $_POST['number'];
$address = $_POST['address'];
$city = $_POST['city'];
$zip = $_POST['zip'];
$device = $_POST['device'];
$formcontent=" From: $name \n number: $number \n Address: $address \n City: $city \n Zip $zip \n Device: $device \n Repair Cost: (need this to work)";
$recipient = "joe.t.oconnor@gmail.com";
$subject = "Device Repair Request";
$mailheader = "From: $name \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header('Location: thankyou.html');
?>

这是一个令人困惑的问题,任何帮助将不胜感激。谢谢!

【问题讨论】:

  • 尝试创建另一种隐藏输入类型,并动态设置该输入的值以修复成本,就像您对跨度所做的那样。
  • 值得一提的是,很多 电子邮件客户端(我敢说大多数甚至是)不支持 Javascript - 更不用说电子邮件被标记为垃圾邮件或完全被拒绝。我建议将他们重定向到这样做的网站,而不是在电子邮件本身中包含表格。哦,我刚刚注意到..- action="../mail.php" 在通过电子邮件发送时不会真正起作用 - 如果表单本身开始工作(它可以,但可能不会)。
  • 或在只读输入中显示总和,因此只需更新一个提交的元素
  • 电子邮件中的 html 表单永远不会起作用
  • @heybignick 我尝试将 html 块放在 value 标记内,它只会返回实际的 html 代码而不是来自 js 文件的值

标签: javascript php html email dynamic


【解决方案1】:

(请参阅下面的编辑)

您可以使用输出缓冲来捕获表单,并对操作使用完整的 http 调用并作为 HTML 发送。

旁注:确保将所有实例 example.com 更改为您的服务器地址。

例子:

<?php 

ob_start();

echo '

      <form action="http://www.example.com/mail.php" method="POST" class="location-form">

            <div class="device-details">
              <h2>Device: iPhone5</h2>
              <h2>Repair Cost: $<span id="result">0</span></h2>
            </div>

            <input name="name" type="text" id="name" placeholder="Name" maxlength="20">
            <input name="number" type="text" id="number" placeholder="Number" maxlength="15">
            <input name="address" type="text" id="address" placeholder="Address" maxlength="40">
            <input name="city" type="text" id="city" placeholder="City" maxlength="20">
            <input name="zip" type="text" id="state" placeholder="Zip" maxlength="20">

            <input name="device" type="hidden" id="device" value="iPhone5" maxlength="20">


            <div class="submit-btn">
                <input type="submit"  value="Submit Request" />
            </div>

      </form>

';

$output = ob_get_contents();
ob_end_clean();

$to = "email@example.com";
$from = "email@example.com";

$subject = "Form submission";

$message = $output;

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

$headers .= "From:" . $from;

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

然后在您的mail.php 文件中:

旁注:我遗漏了一些 POST 数组。你需要自己添加这些。这只是对我有用的一个例子。

<?php 

$first_name = $_POST['name'];

// The following is non-existant in your form. You can adjust to your liking.
$message = $_POST['message']; 

$to = "email@example.com";
$from = "email@example.com";

$subject = "Form submission 2";

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

$headers .= "From:" . $from;

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

脚注:

这里可能无法使用 JS,但可以尝试一下。我没有在测试中使用它。

您还必须使用某种形式的按钮或链接来告诉人们点击发送。这是一个实验性的答案。我会尽快修改它。

参考资料:


编辑:

这是(新的,已编辑)的工作原理。 (您需要将其他 POST 数组添加到 mail.php,如 cmets 中所述)。

旁注:有关一些附加说明,请参阅代码中的 cmets。 // comment...

  • 用户输入他们的电子邮件地址(取自单独的表单)。
  • 如果不为空,则将电子邮件捕获到 POST 数组中。
  • 如果电子邮件不为空,则在输出缓冲区中捕获整个内容。
  • 将表单发送给用户。

新代码:

<?php 

echo $form = '

      <form action="" method="POST" class="location-form">

            <input name="email_from" type="text" id="email_from">

                <input type="submit" name="submit" value="Submit Request" />

      </form>

';

if(!empty($_POST['email_from']) ){

// This outputs a message to the user after entering their email, and submits.
echo $from2 = "An email has been sent to: " . $_POST['email_from'];

}

$var = '

<div class="select-location-page">
      <h2>Where should we meet you?</h2>
      <p class="location-sub-header">Fill out this form and we will meet you within 1 hour!</p>


      <form action="http://www.example.com/mail.php" method="POST" class="location-form">

            <div class="device-details">
              <h2>Device: iPhone5</h2>
              <h2>Repair Cost: $<span id="result">0</span></h2>
            </div>

            <input name="name" type="text" id="name" placeholder="Name" maxlength="20">
            <input name="number" type="text" id="number" placeholder="Number" maxlength="15">
            <input name="address" type="text" id="address" placeholder="Address" maxlength="40">
            <input name="city" type="text" id="city" placeholder="City" maxlength="20">
            <input name="zip" type="text" id="state" placeholder="Zip" maxlength="20">

            <input name="device" type="hidden" id="device" value="iPhone5" maxlength="20">

            <div class="submit-btn">
                <input type="submit" value="Submit Request from Email" />
            </div>

      </form>

      <script>
        document.getElementById("result").innerHTML = localStorage.getItem("sum");
      </script>

  </div>

';

if(!empty($_POST['email_from']) ){

  ob_start();

    echo $var;

    $output = ob_get_contents();
    ob_end_clean();

    $to = $_POST['email_from']; // Send to the email entered by the user
    $from = "email@example.com"; // This should be YOUR E-mail address

    $subject = "Form submission";

    $message = $output;

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

    $headers .= "From:" . $from;

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


}

同样,javascript 可能无法解决此问题,因此您可能需要考虑其他方式或直接省略它。

【讨论】:

  • @FirstOne 呵呵没问题 ;-) 我已经完成了,请保留你的 cmets lol
  • 请注意,我至少会留下一个指向“输出缓冲”的链接以供进一步阅读......
  • @Fred-ii- 感谢您的回复!我应该用第一个 php sn-p 替换表单吗?当我这样做时,表格就会消失。
  • @FirstOne 我要去。给我一分钟。
  • @Fred-ii- 我试过了,它似乎在发送的电子邮件中显示表单,但不幸的是,这些字段没有填充,javascript 调用不起作用。有没有办法填充用户输入的字段以及 javascript 调用的变量?
猜你喜欢
  • 1970-01-01
  • 2022-10-23
  • 1970-01-01
  • 2016-11-23
  • 2015-12-05
  • 2014-02-08
  • 2013-02-24
  • 2014-05-23
  • 1970-01-01
相关资源
最近更新 更多