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. 





10 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

  5. Hey there! I know this is kinda off topic but I was wondering if you knew where I could find a captcha plugin for my comment form? I'm using the same blog platform as yours and I'm having problems finding one? Thanks a lot! netflix.com login

    ReplyDelete
  6. The great service in this blog and the nice technology is visible in this blog. I am really very happy for the nice approach is visible in this blog and thank you very much for using the nice technology in this blog
    Angularjs course in chennai

    ReplyDelete
  7. The blog you presented was very nice and interesting which helped me to get update on the recent technologies.

    angularjs training in chennai

    devops training in chennai

    ReplyDelete
  8. I just needed to record a speedy word to express profound gratitude to you for those magnificent tips and clues you are appearing on this site. We are providing AngularJs training in Velachery.
    For more details: AngularJs Training in Chennai

    ReplyDelete