如何通过电子邮件发送html代码的动态块?

时间:2016-07-05 00:42:38

标签: javascript php html email dynamic

你好我想知道是否有办法将一段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');
?>

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

1 个答案:

答案 0 :(得分: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,如评论中所述。

旁注:请参阅代码中的注释以获取一些补充说明。 // 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可能不适用于此,因此您可能需要考虑另一种方式或只是省略它。