Clients
       >>   Client Sign In

                Affiliates
         >>   Affiliate Info
         >>   Affiliate Sign In

       

      Tel (Lon, UK): 020 8133 3460

      Quick, easy tutorial of user web forms using phpmailer

       

      In this quick tutorial, we're going to look at adding webpage forms on your website for your users to fill in. We use (and love) phpmailer. In this example, you can download the phpmailer web forms tutorial file.

      The zip download file contains:

      • The php file for your user form
      • The php file to handle the user submitted form
      • The phpmailer class
      • A test PDF document which is used as an attachement.

      We love phpmailer for one reason. It simply works! This is hugely important in web development. When using multiple technologies and classes, you don't want to spend all your time finding  out why plugins and classes don't work, you just want them to work and phpmailer is one of the few classes we've seen which just does what its supposed to. Another great reason is its ease of use... oh, and its flexibility.

      This simple tutorial is just a quick start guide. In this tutorial we'll look at the user form, and then we'll use phpmailer in the form submit file to send the client a thank you email, and to send yourself a different email to notify yourself of the user's form.

      We also show you how to send an attachement, in this case a PDF document, as well as using HTML code in your email.

       

      Installing it

      Provided you have a web server running PHP, you really don't have anything to install. Simply copy all of the files into a single directory and use it! If you want to move directories into different paths, then you'll need to update the file and path locations in the files, but if your still learning, don't worry about that just yet. Just copy the files into a single directory!

       

      The web mail functionality

      Before we get started, go on over to www.php-web-host.com/phpmailer/userform.php and submit the form. You'll get two emails, one which would normally go to the user with the attachment, and the second which would normally go to yourself. We just use the same email address in both for this example, but it would obviously be changed in your real application.

      Once you've tried it and seen the mails, lets explain what's going on.

      1) userform.php - This contains the actual user form. This is where the user fills in their details.

      The line

      <form action="SendForm.php" method="post">

      has an action and a method. The action is the page which this form gets submitted to for processing. The method is the type of headers which the server uses, but lets not worry about that for now.

      The user form will contain your real form and can contain any fields you want.

      Company:

      <input name="Company" type="text">

      Each field has a name, in this case "Company". These names are used in SendForm.php to process the data

      2) SendForm.php - This is where the processing occurs.

      The first line, require("class.phpmailer.php");, tells the processor where to find the phpmailer class file. This is needed in order to use the functionality of phpmailer.

      $mail = new PHPMailer(); --> This causes a new instance of the functionailty. This causes a fresh instance of the mailer.

       

      $message = "Hello <font color="red">".$_POST["FirstName"]." ".$_POST["Surname"]."</font>. Thank you for your enquiry from ".$_POST["Company"].". We will contact you on your email address, ".$_POST["Email"];http://www.softsmart.co.za:2082/frontend/x3/filemanager/editit.html?file_charset=us-ascii&dir=%2fhome%2fjsmcm%2fpublic_html%2fblogs%2fweb-development%2fphp&file=phpmailer.inc $message = $message."<br>Regards,<p><a href="http://www.php-web-host.com">PHP-Web-Host.com</a>";

      These lines are the message we want to send to our user. Note that we make the message up by including the form's field names from userfile.php. Note that to include one of these fields in our string, we need to insert the field into the string. This gets done by closing the quotes, adding a ., then the field name, then another ., then opening quotes again, ie

      "This is where we insert".$_POST["FieldName]." our field";

      Note that the second line is where we add more info to the message variable. $message = $message."More info"; basically says message = message + whatever else follows. Follow?

                  $mail->ClearAddresses();
                  $mail->ClearAttachments();

      These clear any addresses and attachements. This is handy because if you had previously used phpmailer and there are still email addresses or attachments in memory, you could end up sending mail and / or attachement to users you never intended.

      $mail->IsHTML(true);

      This  line tells phpmailer that we are including HTML in our email. In this example we just use simple html (break, colour and a link), but you could include images,etc too.

      $mail->AddAttachment("./test.pdf", "Test PDF.pdf");

      Here we instruct phpmailer to add an attachment. The "./test.pdf" is the location of the actual attachement and the "Test PDF.pdf" part is what the user will save the attachement as.

                  $mail->AddReplyTo('[email protected]', 'Admin Dept');
                  $mail->From = "[email protected]";
                  $mail->FromName = "SoftSmart - Admin";

       

      These lines are the reply to and from email address and from names. This is what you see in your email client (outlook, etc). This is who the email will appear to have come from.

       

      $mail->AddAddress($_POST["Email"]);

      This is where we specify the email address(es) to send to. This could include multiple calls, ie

      $mail->AddAddress([email protected]);

      $mail->AddAddress([email protected]);

      $mail->AddAddress([email protected]);

      or, if you were looking email addresses up from a database you could have a loop

      while(recordsexit)

      {

              $mail->AddAddress(dbLookup);

      }

                  $mail->Subject = "User Form Submitted";
                  $mail->Body = $message;
                  $mail->WordWrap = 50;

      These lines are the subject of the email, the message and how many letters the email must use before wrapping around.

      $mail->Send();

      Sends the email...

       

      Pretty simple. In this example we create another instance of the phpmailer so that we can send a separate email to the user and one to the admins.

      Our zip file contains all the files needed to try this out. Of course you will need a web server running PHP. Also note that although we have included the phpmailer class file in the zip file, it is only because that is the version we've used in the example. For the most recent version and for more examples and documentation, please visit their site at phpmailer

       

       

      Comments

      Name:RFID
       
      Comment:
      This is why I love php mysql website hosting, and you guys make my hosting account so easy to manage. There are tons of free scripts and tutorials and blog articles on how to manage and setup my website using your web hosting service. Thank you..

       

      Add your comment

      Display Name:  
      Web Address:  
      Email Address: (not displayed)

      Comment

      Security Verified Seal SSL Certificate Authority Paypal Verified We accept PayPal

       
       
        © Free, easy content management system
      HOME | HOME / STUDENT WEB HOSTING | SMALL BUSINESS WEB HOSTING | BUSINESS WEB HOSTING | COMPANY WEB HOSTING | CORPORATE WEB HOSTING | PACKAGE COMPARISON | AFFILIATES | RESELLER HOSTING | RESELLER 50GB | RESELLER 100GB | RESELLER 150GB | RESELLER 200GB