Working with CordovaEmailComposer

191 Views Asked by At

I am new on Angular and Ionic Framework and want to incorporate the cordova email composer plugin. But failed to complete this. Here is my all code.

I did install the CLI :

ionic cordova plugin add https://github.com/katzer/cordova-plugin-email-composer.git

Index.html

    <script src="lib/ionic/js/ionic.bundle.js"></script>
    <script src="cordova.js"></script>
    <script src="js/openfb.js"></script>
    <script src="js/ngopenfb.js"></script>
    <script src="js/ngCordovaOauth.js"></script>
    <script src="settings.js"></script>
    <script src="js/angular-translate.min.js"></script>
    <script src="translate.js"></script>
    <script src="js/app.js"></script>
    <script src="js/services.js"></script>
    <script src="js/controllers.js"></script>
    <script src="js/directives.js"></script>
    <script src="js/ngStorage.js"></script>
    <script src="js/ionic-close-popup.js"></script>
    <script src="js/angular-base64.js"></script>

App.JS

angular.module('starter', ['ionic', 'starter.services', 'starter.controllers', 'starter.translate', 'ngStorage', 'ionic.closePopup', 'ab-base64', 'ngOpenFB', 'ngCordovaOauth','starter.directive'])

Controller:

.controller('EmailController', function($scope) {
    $scope.sendEmail=function(EmailAddr){
        var email = 
            {
                to: '[email protected]', 
                subject: 'Test Message',
                body: 'This is a test message',
                isHtml: true
            }; 
        $cordovaEmailComposer.isAvailable().then(function() {
            $cordovaEmailComposer.open(email).then(null, function () {
            // user cancelled email
            });
        }, function () {
            // not available
        });
    };    
})

HTML:

<div class="list theme-forms" ng-controller="EmailController">
<button class="button button-block button-positive" ng-click="sendEmail()">SEND</button>
</div>

When clicked on SEND Button, following error displayed on chrome console and email is not sent -

ionic.bundle.js:150 ReferenceError: $cordovaEmailComposer is not defined
    at b.$scope.sendEmail (controllers.js:746)
    at fn (eval at compile (ionic.bundle.js:260), <anonymous>:4:218)
    at ionic.bundle.js:472
    at b.$eval (ionic.bundle.js:176)
    at b.$apply (ionic.bundle.js:177)
    at HTMLButtonElement.<anonymous> (ionic.bundle.js:472)
    at Pf (ionic.bundle.js:70)
    at HTMLButtonElement.d (ionic.bundle.js:70)
    at n (ionic.bundle.js:22)
    at t (ionic.bundle.js:22)

Could you help to give a hint what I have missed here?

EDIT 1

Now I've added following js file on index.html

js/ng-cordova.min.js
js/email_composer.js // form Plugin files

and getting error console -

require is not defined

When Click on SEND Button -

TypeError: Cannot read property 'isAvailable' of undefined
1

There are 1 best solutions below

0
On
try to use this plugin:-
http://ngcordova.com/docs/plugins/emailComposer/

module.controller('ThisCtrl', function($cordovaEmailComposer) {

 $cordovaEmailComposer.isAvailable().then(function() {
   // is available
 }, function () {
   // not available
 });

  var email = {
    to: '[email protected]',
    cc: '[email protected]',
    bcc: ['[email protected]', '[email protected]'],
    attachments: [
      'file://img/logo.png',
      'res://icon.png',
      'base64:icon.png//iVBORw0KGgoAAAANSUhEUg...',
      'file://README.pdf'
    ],
    subject: 'Cordova Icons',
    body: 'How are you? Nice greetings from Leipzig',
    isHtml: true
  };

 $cordovaEmailComposer.open(email).then(null, function () {
   // user cancelled email
 });
});