[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

php mail from

Posted on 2006-05-04
12
Medium Priority
?
283 Views
Last Modified: 2008-03-06
I use this code with a form that sends email through a html template included in a variable in the code.

The procedure is like that:

You see the form
You fill the boxes etc
Press Submit and the email is sent.

What i want to do is to have a "preview" of the html code filles with the variables that gets from the form,
so I can take a look how the completed html page will look like to the reciever.


The code is :

<?php


$today = date("d.m.y");     // 03.10.01
$nextday = mktime(0, 0, 0, date("m")  , date("d")+1, date("Y"));
$nextday = date("d.m.y",$nextday);

$template = "
<html>
my html code here with tha variables
</html>


";





if ($_POST[Submit]){

      $to = $_POST[email];

      // subject
      $subject = 'MY SUBJECT';

      // message
      $message = $template;

      // To send HTML mail, the Content-type header must be set
      $headers  = 'MIME-Version: 1.0' . "\n";
      $headers .= 'Content-type: text/html; charset=utf-8' . "\n";

      // Additional headers

      $headers .= 'Bcc: MY BCC ADDRESS<myemail@domain.tld>' . "\n";
      $headers .= 'From: THE SENDER<'.$from.'>' . "\n";
      // Mail it
      $mail_err = mail($to, $subject, $message, $headers);
      if (!$mail_err){echo "Unexpected error, please inform the <a href='mailto:myemail@domain.tld?subject=Contact form error at a custom form'>webmaster</a>";}
      else {echo "<span style='font-weight: bold'><BR />&nbsp;&nbsp;&nbsp;Everything was cool and smooth!<BR />&nbsp;&nbsp;&nbsp;<form method='post'><input type=\"submit\" name=\"Again\" value=\"Again ?\" /></form></span>";}

echo "<a href=\"_index.html\">&lt;&lt; Back</a>";

}

else {
?>
<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
<html><head>
<title>Tophost</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

  <style type="text/css">
<!--
.generic_lifestyle {
}
.generic_lifestyle td {
font-family: tahoma;font-size: small;
      padding: 4px;
}

.generic_lifestyle input{
font-family: tahoma;font-size: small;
border: 1px solid #666666;
}

.generic_lifestyle select{
font-family: tahoma;font-size: small;
border: 1px solid #666666;
}

.generic_lifestyle
-->
  </style>
</HEAD>
<body>
<div class="generic_lifestyle">
<form method="post" action="notpayed.php">
  <table border="0" cellspacing="0" cellpadding="0">
    <tr>
      <!-- <td colspan="2">The early bird catches the worm. </td> -->
    </tr>
    <tr>
      <td>From</td>
    <td><select name="from" id="from">
        <option value="Sender1" selected="selected"><strong>info</strong>@domain.tld</option>
        <option value="Sender2"><B>sales</B>@domain.tld</option>
        </select>
      </td>
    </tr>
    <tr>
      <td>Client's email </td>
      <td><input name="email" type="text" id="email" /></td>
    </tr>
      <tr>
      <td>LastName</td>
      <td><input name="lastname" type="text" id="lastname" /></td>
    </tr>
    <tr>
      <td>Date B</td>
      <td><input name="orderdate" type="text" id="orderdate" value="<?=$today?>"/></td>
    </tr>
    <tr>
      <td>Date End</td>
      <td><input name="lastdate" type="text" id="lastdate" value="<?=$nextday?>" /></td>
    </tr>


      <!-- <td>Click :P ----&gt; </td> -->
      <td><input type="submit" name="Submit" value="Submit" /></td>
    </tr>
  </table>
</form>
</div>

<a href="_index.html">&lt;&lt; Back</a>

</body>
</html>
<?
}



?>
0
Comment
Question by:MaRiOsGR
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 20

Expert Comment

by:Gawai
ID: 16605074
<?php
echo "From email address is: $from";
and so on
?>
from is the of html field where use wil insert email address.
hope this wil help u out
0
 
LVL 2

Author Comment

by:MaRiOsGR
ID: 16605893
I know how to do echo,
the problem is that if I push submit to ,make the form take date i give, it will automaticaly send also the email...
so i will get the html with echo on my screen but the email will be on its way....
know what i mean ?
0
 
LVL 20

Expert Comment

by:Muhammad Wasif
ID: 16606193
use output buffering for this. take a look at
http://www.php.net/manual/en/function.ob-get-contents.php
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Expert Comment

by:Gawai
ID: 16606820
u need to create 3 pages one for form, second for confirmation and last for submission
0
 
LVL 4

Expert Comment

by:Gee_Orge
ID: 16609018
the solution is very simple

all that u need is to have those email templates as an extrenal file,
then if the u fill the data and want to preview this tpl with the user data
can use a combination of Javascript and PHP to view those before send them.

i will post later the hole solution that i am suggesting.

Gee_Orge
:P
0
 
LVL 6

Expert Comment

by:soapergem
ID: 16609198
You don't need any external pages. Just use different buttons to submit the form. Set it up in the following way:

1) If no data has been sent by the form, output the form with the "Submit1" button
2) If data has been sent along with the "Submit1" button, output a preview, along with the form and a "Submit2" button.
3) If data has been sent along with the "Submit2" button, mail it.


Here's how. This is a two-step process.
----------------------------------------------------------
FIND (toward the bottom):
<td><input type="submit" name="Submit" value="Submit" /></td>

REPLACE WITH:
<?php
if ( $_POST['Submit1'] == 'Submit' )
{
    echo '<td><input type="submit" name="Submit2" value="Submit" /></td>';
}
else
{
    echo '<td><input type="submit" name="Submit1" value="Submit" /></td>';
}
?>
----------------------------------------------------------
FIND (toward the top):
if ($_POST[Submit]){
    // lots of stuff here, including the mail() function to mail out your data
}

REPLACE WITH:
if ($_POST['Submit2'] == 'Submit')
{
    // mail the data
}
else if ($_POST['Submit1'] == 'Submit')
{
    // give a preview of the data
}
----------------------------------------------------------
I assume you know how to write all the echo commands to generate the preview.
0
 
LVL 2

Author Comment

by:MaRiOsGR
ID: 16609780
well soapergem everything i tried gives me parse errors...
0
 
LVL 6

Expert Comment

by:soapergem
ID: 16610330
Can you post some code so we can try to debug?
0
 
LVL 6

Expert Comment

by:soapergem
ID: 16610370
(and perhaps what the errors said, as well?)
0
 
LVL 4

Accepted Solution

by:
Gee_Orge earned 2000 total points
ID: 16613051
Well hi again.
Here is what i am suggesting.

this is your initial file with some changes

<?php


$today = date("d.m.y");     // 03.10.01
$nextday = mktime(0, 0, 0, date("m")  , date("d")+1, date("Y"));
$nextday = date("d.m.y",$nextday);

if ($_POST[Submit]){
      
      $mailBody = file_get_contents("email_tpl.html");
     $to = $_POST[email];
      
    $mailBody = str_replace("{FROM}", $_POST["from"], $mailBody);
      $mailBody = str_replace("{EMAIL}", $_POST["email"], $mailBody);
      $mailBody = str_replace("{LASTNAME}", $_POST["lastname"], $mailBody);
      $mailBody = str_replace("{DATEB}", $_POST["orderdate"], $mailBody);
      $mailBody = str_replace("{DATEE}", $_POST["lastdate"], $mailBody);
      
      $message = $mailBody;
      // subject
     $subject = 'MY SUBJECT';

     // message
     $message = $template;

     // To send HTML mail, the Content-type header must be set
     $headers  = 'MIME-Version: 1.0' . "\n";
     $headers .= 'Content-type: text/html; charset=utf-8' . "\n";

     // Additional headers

     $headers .= 'Bcc: MY BCC ADDRESS<myemail@domain.tld>' . "\n";
     $headers .= 'From: THE SENDER<'.$from.'>' . "\n";
     // Mail it
     $mail_err = mail($to, $subject, $message, $headers);
     if (!$mail_err){echo "Unexpected error, please inform the <a href='mailto:myemail@domain.tld?subject=Contact form error at a custom form'>webmaster</a>";}
     else {echo "<span style='font-weight: bold'><BR />&nbsp;&nbsp;&nbsp;Everything was cool and smooth!<BR />&nbsp;&nbsp;&nbsp;<form method='post'><input type=\"submit\" name=\"Again\" value=\"Again ?\" /></form></span>";}

echo "<a href=\"_index.html\">&lt;&lt; Back</a>";

}

else {
?>
<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
<html><head>
<title>Tophost</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

  <style type="text/css">
<!--
.generic_lifestyle {
}
.generic_lifestyle td {
font-family: tahoma;font-size: small;
     padding: 4px;
}

.generic_lifestyle input{
font-family: tahoma;font-size: small;
border: 1px solid #666666;
}

.generic_lifestyle select{
font-family: tahoma;font-size: small;
border: 1px solid #666666;
}

.generic_lifestyle
-->
  </style>
  <script type="text/javascript" language="javascript">
  //<!--
  function getFormData(){
        var theForm = document.mailForm;
        var sqs;
        
        sqs += theForm.from.selectedIndex.name + '=' + theForm.from.selectedIndex.text + '&';
        sqs += theForm.email.name + '=' + theForm.email.value + '&';
        sqs += theForm.lastname.name + '=' + theForm.lastname.value + '&';
        sqs += theForm.orderdate.name + '=' + theForm.orderdate.value + '&';
        sqs += theForm.lastdate.name + '=' + theForm.lastdate.value + '&';
        
        return sqs
  }
 
  function showPreview(){
        window.open('mail_preview.php?' + getFormData(), 'Mail Preview', 'height=300, width=300');
  }
  //-->
  </script>
</HEAD>
<body>
<div class="generic_lifestyle">
<a href="javascript:;" onclick="showPreview();">Preview mail</a>
<form name="mailForm" method="post" action="notpayed.php">
  <table border="0" cellspacing="0" cellpadding="0">
    <tr>
      <!-- <td colspan="2">The early bird catches the worm. </td> -->
    </tr>
    <tr>
      <td>From</td>
    <td><select name="from" id="from">
        <option value="Sender1" selected="selected"><strong>info</strong>@domain.tld</option>
        <option value="Sender2"><B>sales</B>@domain.tld</option>
        </select>
     </td>
    </tr>
    <tr>
      <td>Client's email </td>
      <td><input name="email" type="text" id="email" /></td>
    </tr>
      <tr>
      <td>LastName</td>
      <td><input name="lastname" type="text" id="lastname" /></td>
    </tr>
    <tr>
      <td>Date B</td>
      <td><input name="orderdate" type="text" id="orderdate" value="<?=$today?>"/></td>
    </tr>
    <tr>
      <td>Date End</td>
      <td><input name="lastdate" type="text" id="lastdate" value="<?=$nextday?>" /></td>
    </tr>

      <!-- <td>Click :P ----&gt; </td> -->
      <td><input type="submit" name="Submit" value="Submit" /></td>
    </tr>
  </table>
</form>
</div>

<a href="_index.html">&lt;&lt; Back</a>

</body>
</html>
<?
}
?>

Here is the preview page code
call it "mail_preview.php"

<?php

      $mailBody = file_get_contents("email_tpl.html");
      
      $mailBody = str_replace("{FROM}", $_POST["from"], $mailBody);
      $mailBody = str_replace("{EMAIL}", $_POST["email"], $mailBody);
      $mailBody = str_replace("{LASTNAME}", $_POST["lastname"], $mailBody);
      $mailBody = str_replace("{DATEB}", $_POST["orderdate"], $mailBody);
      $mailBody = str_replace("{DATEE}", $_POST["lastdate"], $mailBody);

      echo $mailBody;
?>

And final here is the email template
name this file "email_tpl.html"

<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
<html><head>
<title>Tophost</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

  <style type="text/css">
<!--
.generic_lifestyle {
}
.generic_lifestyle td {
font-family: tahoma;font-size: small;
     padding: 4px;
}

.generic_lifestyle input{
font-family: tahoma;font-size: small;
border: 1px solid #666666;
}

.generic_lifestyle select{
font-family: tahoma;font-size: small;
border: 1px solid #666666;
}

.generic_lifestyle
-->
  </style>
  <body>
<div class="generic_lifestyle">
<table border="0" cellspacing="0" cellpadding="0">
    <tr>
      <!-- <td colspan="2">The early bird catches the worm. </td> -->
    </tr>
    <tr>
      <td>From</td>
    <td>{FROM}<td>
    </tr>
    <tr>
      <td>Client's email </td>
      <td>{EMAIL}</td>
    </tr>
      <tr>
      <td>LastName</td>
      <td>{LASTNAME}</td>
    </tr>
    <tr>
      <td>Date B</td>
      <td>{DATEB}</td>
    </tr>
    <tr>
      <td>Date End</td>
      <td>{DATEE}</td>
    </tr>
  </table>
  </div>
</body>
</html>

save all this files in the same directory
to do your testing.

chears m8 :)
0
 
LVL 4

Expert Comment

by:Gee_Orge
ID: 16613086
sorry i forgot to comment the folowing line

 // message
//     $message = $template; //<--comments this line

in the first file
0
 
LVL 4

Expert Comment

by:Gee_Orge
ID: 16613252
And one correction again

replace the Javascript with the following one

<script type="text/javascript" language="javascript">
  //<!--
  function getFormData(){
        var theForm = document.mailForm;
        var sqs="";
        
        sqs += theForm.from.name + '=' + theForm.from.options[theForm.from.selectedIndex].text + '&';
        sqs += theForm.email.name + '=' + theForm.email.value + '&';
        sqs += theForm.lastname.name + '=' + theForm.lastname.value + '&';
        sqs += theForm.orderdate.name + '=' + theForm.orderdate.value + '&';
        sqs += theForm.lastdate.name + '=' + theForm.lastdate.value + '&';
        
        return sqs;
  }
 
  function showPreview(){
        window.open('mail_preview.php?' + getFormData(), 'Mail Preview', 'height=300, width=300');
  }
  //-->
  </script>
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month18 days, 10 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question