Email (contact) form class: make sending emails easier!
Tags CMS,Code,Dan,Website Development - posted by Daniel on 15 May, 2009 05:18 pm
As a PHP developer, I’m always having to create functions to send email forms to both customer and website administrator so that everyone can keep track of what’s going on. A basic example would be your standard web contact form.
Sometimes it can be a big job if I’m dealing with lots of fields, (we once had a project with over 100 fields – big form!!!) as each one needs to be built individually and maintained manually.
What I’ve done to speed up the process is written a small class which works with the most popular PHP email library – PHP Mailer. This small class will generate email form automatically according the $_POST information, and I’m gonna share it with you lucky web cats.
Step 1: Create your email template database
First we need create a table to store your email form, this table can be defined by yourself to suit the project you’re working on
Example:
1 2 3 4 5 6 7 8 | CREATE tblEmail ( code varchar(10) from_Email varchar(60), from_Name varchar(60), subject varchar(80), content Text, Primary key (Code) } |
In the content field, you can store your email template with tag you’ve defined. In my example, the tag will be formatted like this: [tagname].
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <h1>My Contact Form</h1> <table> <tr> <td>Name</td> <td>[name]</td> </tr> <tr> <td>Phone</td> <td>[phone]</td> </tr> <tr> <td>Email</td> <td>[email]</td> </tr> </table> |
Step 2: Update the HTML form on your website
You don’t need change your form too much, simply update the name of each field and make sure it comes with the same name of your tag. This class works with text, select, checkbox, option and textarea.
Sample form:
1 2 3 4 5 6 | <form action=”send.php” method=”post”> <p>Enter your name: <input type=”text” name=”name”></p> <p>Enter your phone number: <input type=”text” name=”phone”></p> <p>Enter your email: <input type=”text” name=”email”></p> <input type=”submit” value=”submit”> </form> |
Step 3: Use the class to send email
Remember to download PHPMailer and include it in your script with the email form class.
The rest is simple:
1 2 3 4 5 6 | Include (“phpmailer.php”) Include (“emailform.class.php”) Form=new EmailForm(“code”); ← The key to loading up the correct email template from database Form.generate_email($_POST); Form.send(“MaxGen Media”,info@maxgen.co.nz”); |
And that’s it!! You can use this class to generate big forms that get sent out to your customer and web admin.
You can also customise the constructor
You can modify this EmailForm class to load up an email template:
1 2 3 4 5 6 7 8 9 10 11 | function __construct($id=0) { $this->id = $id; // Place your query to load your email template here. $rs=mysql_query(“select * from table where code=’$id’); row=mysql_fetch_row($rs)) // Fetch data and store email information $this->subject = $row['subject']; $this->from_name = $row[‘from_name’]; $this->from_email = $row['from_email']; $this->content = $row['content']; } |
Download the files
Enjoy. If you need a hand be sure to leave a comment.
Flash Stretch (AS3 Class)
Tags Code,Flash,Website Development - posted by Alysson on 03 Dec, 2008 02:07 pm
Hey all, my name is Alysson. I’m the lead Flash developer at MaxGen, and would like to share an AS3 class that I’ve made to stretch things in Flash.
During the planning stages of developing a website, we often consider the option to build it fully in Flash. If we decide that Flash is the best way forward, a consideration that always arises is whether we should design the site with a fixed width or if it should scale to 100% of the browser window.
As web designers/developers are aware, this is very much a question of aesthetics. In our case, we needed a small and fast AS3 class to help us stretch certain content like the navigation menu and background image. In fact the class we created can dynamically enlarge any content without distortion as the user increases the browser window size.
We’ve found it pretty useful for our projects, and hopefully you will too.
How to use:
Click here to see a live demo. (resize it)
Step 1: Download these files (download here).
Step 2: Create a new Flash AS3 document and create a MovieClip and instance it as “myMc”. Once that’s done, open the Actionscript console and type the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import maxgen.utils.Stretch; stage.align = StageAlign.TOP_LEFT; stage.quality = StageQuality.HIGH; stage.scaleMode = StageScaleMode.NO_SCALE; var s:Stretch = new Stretch(); stage.addEventListener(Event.RESIZE, onResize); function onResize(e:Event):void { s.baseWidth(myMc, stage.stageWidth); if(s.target.height < stage.stageHeight) { s.baseHeight(s.target, stage.stageHeight); } } |
And that’s it. To explain:
The files that you have downloaded must be in the same directory as your .fla file.
The code inside the “onResize” function is the most important: “s.baseWidth(myMc, stage.stageWidth);” This says that “myMc” will be of the same width as the browser window when resized.
If the content to be resized is smaller than the height of the browser window, this code will correct it:
15 16 17 18 | if(s.target.height < stage.stageHeight) { s.baseHeight(s.target, stage.stageHeight); } |
Enjoy. and be sure to leave a comment if you’ve found this class helpful for your own projects.