Skip to content Skip to sidebar Skip to footer

How To Email Dynamic Block Of Html Code?

hello i am wondering if there is any way to send a block of html code to an email address. i have a variable that is set in an external javaScript file named 'sum' and i am calling

Solution 1:

(Consult my Edit below)

You can use output buffering to capture the form and use a full http call for the action and send as HTML.

Sidenote: Be sure to change all instances example.com to your server address.

Example:

<?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);

Then in your mail.php file:

Sidenote: I left out some of the POST arrays. You'll need to add those yourself. This is merely an example of something that worked for me.

<?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);

Footnotes:

You may not be able to use JS here, but you can try it. I didn't use it in my test.

You'll also have to use some form of button or link to tell people to click to send. This is an experimental answer. I will end up modifying it as soon as I can.

References:


Edit:

Here's how this (new one, edited) works. (You will need to add your other POST arrays to mail.php as discussed in comments).

Sidenote: See comments in code for some added instructions. // comment...

  • The user enters their email address (taken from a separate form).
  • If it's not empty, capture the email in a POST array.
  • If the email is not empty, capture the whole thing in the output buffer.
  • Sends the form to the user.

New code:

<?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);


}

Again, the javascript may not work for this, so you might have to think of another way or just omit it.


Post a Comment for "How To Email Dynamic Block Of Html Code?"