Here lie the ramblings of the MaxGen Media team. These things are all the rage and we want to be one of the cool kids, so now you can read about what goes on in our sick little minds.

Grab the RSS feed

Follow us on twitter

Recent Posts

    Tags

    Archives

    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.

    Trackback This Post | Subscribe to the comments through RSS Feed

    Leave a Reply

    Have a topic that you want covered?

    Send us an email and we'll try to write a blog post about it. Only if it's a good topic of course.