Create a postcard sender script



by Administrator author list

 

Step 3. 

 

The next task is to process the form and store the message in a file. Besides this we need to send an email to the recipient with the URL.

First the form processing task. In this step we need to check the inputs and create a unique filename using actual date and time information.

In this file we will store the selected image name, the email address and the message. If the file content is ready then we write it to a folder let's say messages. We can do this with the following code:

 

<?php
$filename 
date('YmdGis');
$f fopen('messages/'.$filename.".txt","w+");         
fwrite($f,$pic."\n");
fwrite($f,$_POST['email']."\n");
fwrite($f,htmlspecialchars($_POST['message'])."\n");
fclose($f);
?>

 

Step 4.

As last step in this thread we need to send an email to the recipient. 

To do this we compose a short message template and put the unique URL in it. Later on we will process the URL GET data to know what to do. How to send an email is not part of this tutorial so I just show you the simple code to send the message:

 

<?php
$postcardURL 
'http://'.$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
$from   "From: $senderName <$senderEmail>\r\n";
$replay "Reply-To: $senderEmail\r\n";    
$params "MIME-Version: 1.0\r\n";
$params .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$mailtext "You have just received a virtual postcard!\r\n\r\n"
          
"You can pick up your postcard at the following web address:\r\n"
          
"$postcardURL"."?show=$filename\r\n\r\n"
          
"We hope you enjoy your postcard, and if you do, "
          
"please take a moment to send a few yourself!\r\n\r\n"
          
"Regards,\r\n"
          
"Postcard Tutorial\r\n"
          
$postcardURL;

// Send email          
@mail($_POST['email'],"You've received a postcard",$mailtext,$from.$replay.$params);

?>           

 

If everything works fine then we will display a short message to the visitor that the postcard was send successfully and display it to check.

 

Step 5.

The final task to do is processing URLs to find out what task the script should do. As you saw we put a unique URL inside the mail where we used the show parameter inside the URL. If this parameter is set then the script knows that the postcard needs to be displayed. Without this parameter the normal postcard sending form will be displayed.

In case of showing postcard we get the requested information from the URL. It is stored in the $_GET array and find the relevant message file in the messages directory. Next we will read the file and process its content. Finally we display a quite simple HTML page with the image and the message.

The code looks like this:

 

<?php
$file 
= isset($_GET['show']) ?  $_GET['show'] : ''          ;
$content file('messages/'.$file.".txt");
$pic   $content['0'];
unset (
$content['0']);
unset (
$content['1']);
$main "";
foreach (
$content as $value) {
      
$main .= $value;
}
?>           
   <center>Your postcard!<br/><br/>
   <img src='images/<?php echo $pic?>' alt="postcard" />
   <br/><br/><br/>
   <?php echo nl2br(htmlspecialchars($main)); ?>
   </center>   

 

You can download a small postcard sender script as well.

On the next page you will find the complete code of the script

 



article index
page 1 : untitled page
page 2 - current : Part 2
page 3 : Complete code


Tags: postcard sender, sending virtual postcard

Php Toys - 2006 - Php resources, scripts and tutorials - Privacy Policy
Insurance Index - Tutorial collection - Forex trading, brokers, reviews - Mortgage payment calculator
{THEMEDISCLAIMER}
Render time: 0.1296 sec, 0.0198 of that for queries. DB queries: 25.