Jump to content

Embed HTML in PHP script...


m_tyhurst2002

Recommended Posts

Hello! I have this script that I use that emails a form to me. The problem is that I am having a hard time to make the email that it sends me to be "pretty" with html code using a table and stuff to make it layout like I like it. The code that I am using is as follows. How can I add html to it and it have a custom layout like I would like. If anyone can help me that would be totally awesome!!!

 

<?php
$Checked = false;  
  if (isset($_POST['submit'])) {
    $pickupdate = $_POST['pickupdate'];
    $pickuptime = $_POST['pickuptime'];
    $AMPM = $_POST['AMPM'];
    $lastname = $_POST['lastname'];
    $firstname = $_POST['firstname'];
    $email = $_POST['email'];
    $cellphone = $_POST['cellphone'];
    $pickuplocation = $_POST['pickuplocation'];
    $destination = $_POST['destination'];
    $airline = $_POST['airline'];
    $flightnumber = $_POST['flightnumber'];
    $orderedbyname = $_POST['orderedbyname'];
    $orderedbynumber = $_POST['orderedbynumber'];
    $company = $_POST['company'];
if ( !empty($pickupdate) && 
	 !empty($pickuptime) &&
         !empty($AMPM) &&
         !empty($lastname) &&
         !empty($firstname) &&
	 !empty($email) && 
         !empty($cellphone) &&
         !empty($pickuplocation) &&
         !empty($destination) &&
         !empty($airline) &&
	 !empty($flightnumber) &&
	 !empty($orderedbyname) &&
	 !empty($orderedbynumber) &&
         !empty($company)) {

    if(isset($_POST['submit'])) {
         $to = "XXX";
         $subject = "XXX";
         $pickupdate_field = $_POST['pickupdate'];
         $pickuptime_field = $_POST['pickuptime'];
         $AMPM_field = $_POST['AMPM'];
         $lastname_field = $_POST['lastname'];
         $firstname_field = $_POST['firstname'];
     $email_field = $_POST['email'];
         $cellphone_field = $_POST['cellphone'];
         $pickuplocation_field = $_POST['pickuplocation'];
         $destination_field = $_POST['destination'];
         $airline_field = $_POST['airline'];
         $flightnumber_field = $_POST['flightnumber'];
         $orderedbyname_field = $_POST['orderedbyname'];
         $orderedbynumber_field = $_POST['orderedbynumber'];
         $company_field = $_POST['company'];
	 $comments_field = $_POST['comments'];
         $body = "From:\n Last Name: $lastname_field\n First Name: $firstname_field\n Email: $email_field\n Pick-Up Date: $pickupdate_field\n Pick-Up Time: $pickuptime_field\n AM/PM: $AMPM_field\n Cell Phone: $cellphone_field\n Pick-Up Location: $pickuplocation_field\n Destination: $destination_field\n Airline: $airline_field\n Flight Number: $flightnumber_field\n Ordered By Name: $orderedbyname_field\n Ordered By Number: $orderedbynumber_field\n Company: $company_field\n Comments: $comments_field";
$Checked = true;//Checked
        mail($to, $subject, $body, $headers, "From:$email_field");
         } else {
        echo "blarg!";
  }
  }
    else { $error = true; }
  }
?>

Link to comment
Share on other sites

I'm assuming you have some PHP knowledge so I have just done a quick example on how to send emails with HTML in them.

 

I haven't tested it, so if doesn't work, I'll take another look later :).

 

<?php
// Load and grab the contents of the email template and place them in to the $strData variable.
$strEmail = "email.html";
$objFile = fopen($strEmail, 'rb');
$strData = fread($objFile, filesize($strEmail));
fclose($objFile);

// Make sure the email will be sent as HTML.
$strHeaders = "From: " . $email_field . "\r\n";
$strHeaders.= "MIME-Version: 1.0\nContent-type: text/html; charset=iso-8859-1";

// Replace the values in the variable holding the email template ($strData).
$strBody = str_replace("[LNAME]", "Test", $strBody);
$strBody = str_replace("[FNAME]", "First", $strBody);
$strBody = str_replace("[email]", "test@test.com", $strBody);
// etc..

// Send the email
mail( $email, "Test", $strBody, $strHeaders );

// This will work, however here is how I would handle the email template.
// Create an array which holds the values that need changing with the value to change it too.

$arrBody["LNAME"] = "Test";
$arrBody["FNAME"] = "First";
$arrBody["EMAIL"] = "test@test.com";

        // Loop through the array changing the values in the same way we did before.
foreach($arrBody as $strKey => $strVal)
{
	$strBody = str_replace("[".strtoupper($strKey)."]", $strVal, $strBody);
}

// It will get exactly the same results, but it just looks a lot nicer .
?>

 

HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
[LNAME]<br />
[FNAME]<br />
[email]<br />

</body>
</html>

 

Link to comment
Share on other sites

I really don't know much about PHP. I guess what I don't understand with what you replied is how I would get the variables like $pickuptime or $pickupdate, etc. How does the html email grab those variables? Do I use the script you wrote and put it in the existing one I have or is this separate. This is kind of hard to explain I guess, maybe confusing myself as well!... Does this make any sense? Thanks for your help and patience!

Link to comment
Share on other sites

Oh ok, not a problem :)

 

You'll be able to use my script below, I've made the modifications for you :).

 

$to = "XXX";
$subject = "XXX";

$strEmail = "email.html";
$objFile = fopen($strEmail, 'rb');
$strData = fread($objFile, filesize($strEmail));
fclose($objFile);

$strHeaders = "From: \r\n";
$strHeaders.= "MIME-Version: 1.0\nContent-type: text/html; charset=iso-8859-1";

$arrBody["LNAME"] = $_POST['lastname'];
$arrBody["FNAME"] = $_POST['firstname'];
$arrBody["EMAIL"] = $_POST['email'];
$arrBody["PUDATE"] = $_POST['pickupdate'];
$arrBody["PUTIME"] = $_POST['AMPM'];
$arrBody["AMPM"] = $_POST['AMPM'];
$arrBody["CELL"] = $_POST['cellphone'];
$arrBody["PULOCATION"] = $_POST['pickuplocation'];
$arrBody["DESTINATION"] = $_POST['destination'];
$arrBody["AIRLINE"] = $_POST['airline'];
$arrBody["FLIGHTNO"] = $_POST['flightnumber'];
$arrBody["ORDEREDBY"] = $_POST['orderedbyname'];
$arrBody["ORDEREDBYNO"] = $_POST['orderedbynumber'];
$arrBody["COMPANY"] = $_POST['company'];
$arrBody["COMMENTS"] = $_POST['comments'];

foreach($arrBody as $strKey => $strVal)
{
$strBody = str_replace("[".strtoupper($strKey)."]", $strVal, $strBody);
}

mail( $to, $subject, $strBody, $strHeaders );

 

The html email template. It has all of the information the current email has and is set out the same way, you just need to change it to the way you want it to be now.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
Last Name: [LNAME]<br />
First Name: [FNAME]<br />
Email: [email]<br />
Pick-Up Date: [PUDATE]<br />
Pick-Up Time: [PUTIME]<br />
AM/PM: [AMPM]<br />
Cell Phone: [CELL]<br />
Pick-Up Location: [PULOCATION]<br />
Destination: [DESTINATION]<br />
Airline: [AIRLINE]<br />
Flight Number: [FLIGHTNO]<br />
Ordered By Name: [ORDEREDBY]<br />
Ordered By Number: [ORDEREDBYNO]<br />
Company: [COMPANY]<br />
Comments: [COMMENTS]<br />
</body>
</html>

 

Hope this helps. :)

 

Link to comment
Share on other sites

Wow! Thank you so much for your help. When I fill out the fields of the for and click submit I get an email but the email is empty... Is this the right code now?

<?php
$Checked = false;  
  if (isset($_POST['submit'])) {
    $pickupdate = $_POST['pickupdate'];
    $pickuptime = $_POST['pickuptime'];
    $AMPM = $_POST['AMPM'];
    $lastname = $_POST['lastname'];
    $firstname = $_POST['firstname'];
    $email = $_POST['email'];
    $cellphone = $_POST['cellphone'];
    $pickuplocation = $_POST['pickuplocation'];
    $destination = $_POST['destination'];
    $airline = $_POST['airline'];
    $flightnumber = $_POST['flightnumber'];
    $orderedbyname = $_POST['orderedbyname'];
    $orderedbynumber = $_POST['orderedbynumber'];
    $company = $_POST['company'];
if ( !empty($pickupdate) && 
	 !empty($pickuptime) &&
         !empty($AMPM) &&
         !empty($lastname) &&
         !empty($firstname) &&
	 !empty($email) && 
         !empty($cellphone) &&
         !empty($pickuplocation) &&
         !empty($destination) &&
         !empty($airline) &&
	 !empty($flightnumber) &&
	 !empty($orderedbyname) &&
	 !empty($orderedbynumber) &&
         !empty($company)) {

    if(isset($_POST['submit'])) {
$to = "whatever@yahoo.com";
$subject = "XXX";

$strEmail = "email.html";
$objFile = fopen($strEmail, 'rb');
$strData = fread($objFile, filesize($strEmail));
fclose($objFile);

$strHeaders = "From: \r\n";
$strHeaders.= "MIME-Version: 1.0\nContent-type: text/html; charset=iso-8859-1";

$arrBody["LNAME"] = $_POST['lastname'];
$arrBody["FNAME"] = $_POST['firstname'];
$arrBody["EMAIL"] = $_POST['email'];
$arrBody["PUDATE"] = $_POST['pickupdate'];
$arrBody["PUTIME"] = $_POST['pickuptime'];
$arrBody["AMPM"] = $_POST['AMPM'];
$arrBody["CELL"] = $_POST['cellphone'];
$arrBody["PULOCATION"] = $_POST['pickuplocation'];
$arrBody["DESTINATION"] = $_POST['destination'];
$arrBody["AIRLINE"] = $_POST['airline'];
$arrBody["FLIGHTNO"] = $_POST['flightnumber'];
$arrBody["ORDEREDBY"] = $_POST['orderedbyname'];
$arrBody["ORDEREDBYNO"] = $_POST['orderedbynumber'];
$arrBody["COMPANY"] = $_POST['company'];
$arrBody["COMMENTS"] = $_POST['comments'];

foreach($arrBody as $strKey => $strVal)
{
   $strBody = str_replace("[".strtoupper($strKey)."]", $strVal, $strBody);
}
$Checked = true;//Checked
        mail($to, $subject, $headers, "From:$email_field");
         } else {
        echo "blarg!";
  }
  }
    else { $error = true; }
  }
?>

 

Thank you very much for helping me with this!!! I just used the email template that you posted. The email.html file is in the root of where the form.php file is that has the code. Is this all correct? Thanks

Link to comment
Share on other sites

All you need to do is change the following and it should (fingers crossed) work.

$strHeaders = "From: \r\n";

to

$strHeaders = "From: $email_field\r\n";

 

And also.

mail($to, $subject, $headers, "From:$email_field");

to

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

 

You should also make sure that you have read access to the html email template on the server.

Link to comment
Share on other sites

I did everything as suggested. This is the php file I am using...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/ DTD/xhtml1-transitional.dtd"> 
<?php
$Checked = false;  
  if (isset($_POST['submit'])) {
    $pickupdate = $_POST['pickupdate'];
    $pickuptime = $_POST['pickuptime'];
    $AMPM = $_POST['AMPM'];
    $lastname = $_POST['lastname'];
    $firstname = $_POST['firstname'];
    $email = $_POST['email'];
    $cellphone = $_POST['cellphone'];
    $pickuplocation = $_POST['pickuplocation'];
    $destination = $_POST['destination'];
    $airline = $_POST['airline'];
    $flightnumber = $_POST['flightnumber'];
    $orderedbyname = $_POST['orderedbyname'];
    $orderedbynumber = $_POST['orderedbynumber'];
    $company = $_POST['company'];
if ( !empty($pickupdate) && 
	 !empty($pickuptime) &&
         !empty($AMPM) &&
         !empty($lastname) &&
         !empty($firstname) &&
	 !empty($email) && 
         !empty($cellphone) &&
         !empty($pickuplocation) &&
         !empty($destination) &&
         !empty($airline) &&
	 !empty($flightnumber) &&
	 !empty($orderedbyname) &&
	 !empty($orderedbynumber) &&
         !empty($company)) {

    if(isset($_POST['submit'])) {
$to = "m_tyhurst2002@yahoo.com";
$subject = "sdf";

$strEmail = "email.html";
$objFile = fopen($strEmail, 'rb');
$strData = fread($objFile, filesize($strEmail));
fclose($objFile);

$strHeaders = "From: $email_field";
$strHeaders .= "MIME-Version: 1.0\nContent-type: text/html; charset=iso-8859-1";

$arrBody["LNAME"] = $_POST['lastname'];
$arrBody["FNAME"] = $_POST['firstname'];
$arrBody["EMAIL"] = $_POST['email'];
$arrBody["PUDATE"] = $_POST['pickupdate'];
$arrBody["PUTIME"] = $_POST['pickuptime'];
$arrBody["AMPM"] = $_POST['AMPM'];
$arrBody["CELL"] = $_POST['cellphone'];
$arrBody["PULOCATION"] = $_POST['pickuplocation'];
$arrBody["DESTINATION"] = $_POST['destination'];
$arrBody["AIRLINE"] = $_POST['airline'];
$arrBody["FLIGHTNO"] = $_POST['flightnumber'];
$arrBody["ORDEREDBY"] = $_POST['orderedbyname'];
$arrBody["ORDEREDBYNO"] = $_POST['orderedbynumber'];
$arrBody["COMPANY"] = $_POST['company'];
$arrBody["COMMENTS"] = $_POST['comments'];

foreach($arrBody as $strKey => $strVal)
{
   $strBody = str_replace("[".strtoupper($strKey)."]", $strVal, $strBody);
}
$Checked = true;//Checked
        mail($to, $subject, $strBody, $strHeaders);
         } else {
        echo "blarg!";
  }
  }
    else { $error = true; }
  }
?>
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head profile="http://gmpg.org/xfn/11"> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title></title> 
<link rel="stylesheet"  type="text/css" href="css/main.css"/> 
</head> 
<body>



  <?php
if($Checked)
{
        echo "<script type='text/javascript'>location.href='http://yahoo.com'</script>";

}
?>        
<p>
<h2> Please fill out the online reservation form below so we can serve you more efficiently. All fields are required to submit the form. Please note that we require at least 24 hours notice for any cancellation. If you are in need of service within the next 24 hours please bypass this step and contact us directly.</h2>
<p> </p>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table width="775" height="422"  border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <th width="275" height="22" scope="row"><p align="left">Pickup Date: (mm/dd/yyyy) </p></th>
    <td width="500"><input name="pickupdate" type="text" value="<?php echo $pickupdate; ?>" size="10" maxlength="10"><span class="resistratioerror"><?php
  if ( $error==true && empty($pickupdate) ) {
    echo 'Please enter pickupdate.<br>';
  }
?></span></td>
  </tr>
  <tr>
    <th height="24" scope="row"><p align="left">Time:</p></th>
    <td><input name="pickuptime" type="text" size="10" maxlength="10"> <select name="AMPM">
               <option value="0" <?php 
  if (empty($AMPM)) echo "selected";
?>>- Select -</option>
                <option value="AM">AM</option>
                <option value="PM">PM</option>
                </select>
      <span class="resistratioerror"><?php
  if ( $error==true && empty($pickuptime) ) {
    echo 'Please enter pick-up time.';
  }
?><?php
  if ( $error==true && empty($AMPM) ) {
    echo 'Select AM/PM.<br>';
  }
?></span></td>
  </tr>
  <tr>
    <th height="22" scope="row"> <div align="left">Last Name:</div></th>
    <td><input name="lastname" type="text" value="<?php echo $lastname; ?>"><span class="resistratioerror"><?php
  if ( $error==true && empty($lastname) ) {
    echo 'Please enter Last Name.<br>';
  }
?></span></td>
  </tr>
  <tr>
    <th height="22" scope="row"> <div align="left">First Name:</div></th>
    <td><input name="firstname" type="text" value="<?php echo $firstname; ?>"><span class="resistratioerror"><?php
  if ( $error==true && empty($firstname) ) {
    echo 'Please enter First Name.<br>';
  }
?></span></td>
  </tr>
  <tr>
    <th height="22" scope="row"><div align="left">Email:</div></th>
    <td><input name="email" type="text" value="<?php echo $email; ?>" /><span class="resistratioerror"><?php
  if ( $error==true && empty($email) ) {
    echo 'Please enter Email address.<br>';
  }
?></span></td>
  </tr>
  <tr>
    <th height="22" scope="row"> <div align="left">Cell Phone:</div></th>
    <td><input name="cellphone" type="text" value="<?php echo $cellphone; ?>"><span class="resistratioerror"><?php
  if ( $error==true && empty($cellphone) ) {
    echo 'Please enter Cell Phone.<br>';
  }
?></span></td>
  </tr>
  <tr>
    <th height="22" scope="row"> <div align="left">Pickup Location:</div></th>
    <td><input name="pickuplocation" type="text" value="<?php echo $pickuplocation; ?>"><span class="resistratioerror"><?php
  if ( $error==true && empty($pickuplocation) ) {
    echo 'Please enter Pick-Up Location.<br>';
  }
?></span></td>
  </tr>
  <tr>
    <th height="22" scope="row"> <div align="left">Destination:</div></th>
    <td><input name="destination" type="text" value="<?php echo $destination; ?>"><span class="resistratioerror"><?php
  if ( $error==true && empty($destination) ) {
    echo 'Please enter Destination.<br>';
  }
?></span></td>
  </tr>
  <tr>
    <th height="22" scope="row"> <div align="left">Airline:</div></th>
    <td><input name="airline" type="text" value="<?php echo $airline; ?>"><span class="resistratioerror"><?php
  if ( $error==true && empty($airline) ) {
    echo 'Please enter Airline.<br>';
  }
?></span></td>
  </tr>
  <tr>
    <th height="22" scope="row"> <div align="left">Flight Number:</div></th>
    <td><input name="flightnumber" type="text" value="<?php echo $flightnumber; ?>"><span class="resistratioerror"><?php
  if ( $error==true && empty($flightnumber) ) {
    echo 'Please enter Flight Number.<br>';
  }
?></span></td>
  </tr>
  <tr>
    <th height="22" scope="row"> <div align="left">Ordered by Name:</div></th>
    <td><input name="orderedbyname" type="text" value="<?php echo $orderedbyname; ?>"><span class="resistratioerror"><?php
  if ( $error==true && empty($orderedbyname) ) {
    echo 'Please enter Ordered By Name.<br>';
  }
?></span></td>
  </tr>
  <tr>
    <th height="22" scope="row"><div align="left">Ordered by Number:</div></th>
    <td><input name="orderedbynumber" type="text" value="<?php echo $orderedbynumber; ?>" /><span class="resistratioerror"><?php
  if ( $error==true && empty($orderedbynumber) ) {
    echo 'Please enter Ordered by Number.<br>';
  }
?></span></td>
  </tr>
  <tr>
    <th height="22" scope="row"> <div align="left">Company:</div></th>
    <td><input name="company" type="text" value="<?php echo $company; ?>"><span class="resistratioerror"><?php
  if ( $error==true && empty($company) ) {
    echo 'Please enter Company.<br>';
  }
?></span></td>
  </tr>
  <tr>
    <th height="24" valign="top" scope="row"> <div align="left">Comments:</div></th>
    <td><textarea name="comments" cols="35" rows="4" id="comments"></textarea></td>
  </tr>
  <tr>
    <th height="24" scope="row"><div align="left"></div></th>
    <td><input type="reset" value="Reset">
      <input type="submit" name="submit" value="Submit"></td>
  </tr>
  <tr>
    <th height="24" scope="row"> <div align="left" class="receiptlink">
      <p><a href="ETReceipt.pdf">Downloadable Receipt</a></p>
      </div></th>
    <td><p> </p>      </td>
  </tr>
</table>
</form>

	</body>
</html>

 

This is the html file I am using.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<body>
Last Name: [LNAME]<br />
First Name: [FNAME]<br />
Email: [email]<br />
Pick-Up Date: [PUDATE]<br />
Pick-Up Time: [PUTIME]<br />
AM/PM: [AMPM]<br />
Cell Phone: [CELL]<br />
Pick-Up Location: [PULOCATION]<br />
Destination: [DESTINATION]<br />
Airline: [AIRLINE]<br />
Flight Number: [FLIGHTNO]<br />
Ordered By Name: [ORDEREDBY]<br />
Ordered By Number: [ORDEREDBYNO]<br />
Company: [COMPANY]<br />
Comments: [COMMENTS]<br />
</body>
</html>

 

I cannot understand what the deal is... Thank you sooo much for your continued help!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.