Tuesday, June 23, 2015

Send E-mail using PHP and AngularJS

Implementing contact form in your website is a nice feature. The data of contact form can be stored in database or directly sent as e-mail.


Here are the steps to create e-mail contact form using php and AngularJS:
  1. Setup a gmail account to receive the contact e-mail. You can also setup Outlook or other account and change the below configuration accordingly.
  2. Create a page for contact form:
  3. We will including bootstrap.min.css and bootstrap font (//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet) in html first.
    Observe the following points in below code:
    • ng-submit="processForm()" will call preocessForm function
    • ng-class="{ 'has-error' : errorName }" is used for binding the error to class
    • Name, e-mail and content input field and Submit button.
    • formData.name, formData.superheroAlias and formData.content model created for name, email and content



  1. Set up JS - Observe following points in the code:
    • $scope.formData used to make glue between model and controller
    • $scope.processForm function making request to index.php to validate and send the e-mail
    • On success, the successful message and on failure, the error is assigned to property




  1. Set up PHP file - Observe following points in the code:
    • class.phpmailer.php is included and referred to send the mail
    • Validation of the values
    • $mail object and its property
    • If input field is valid then send the e-mail
    class.phpmailer.php will also using other classes, so we need to include class.phpmailer.php, class.pop3.php, class.smtp.php and PHPMailerAutoload.php. 





5 comments:

  1. The explanation is really good.Easy to Understand

    ReplyDelete
  2. Hello. Do you enter the email you want the data to be sent to inside the Quotations?

    $mail->AddAddress(""); //Pass the e-mail that you setup

    ReplyDelete
  3. Hello. Do you enter the email you want the data to be sent to inside the Quotations?

    $mail->AddAddress(""); //Pass the e-mail that you setup

    ReplyDelete
    Replies
    1. Hi.

      Probably a bit late but in PHP you would put your email address in single 'quotations'. I used a HTML to PHP method quite a bit differant to this but the synax I know would use ' ' ... I hope this helps someone in the future.

      Delete
  4. Thank you nice explanation easy and simple.This article made my day...

    ReplyDelete