ankur
ankur

Reputation: 181

Sending email in Ionic app results in following error: TypeError: $cordovaEmailComposer.isAvailable is not a function

I am working on an Ionic Android/iOS app that will primarily be used to scan barcodes then email a CSV (.txt) attachment containing all scans to a distribution group. This app uses ngCordova + the Cordova Email Composer plugin (cordova-plugin-email-composer) for email functionality. Upon initiating an email send in Android (6.0.1 on Nexus 5), I get the following console error:

TypeError: $cordovaEmailComposer.isAvailable is not a function

All other processes seem to work correctly (e.g. correct platform-dependent file path generation, attachment formatting and generation) and ngCordova + Cordova Barcode Scanner plugin (phonegap-plugin-barcodescanner) functions properly.

I did have some issues with Cordova Email Composer v0.8.3 always returning 'false' on .isAvailable() in Android, however I successfully worked around this using v0.8.2. This new problem occurs with both versions.

Please see below for the portion of the Angular service that contains the problem code. scanData is a simple service that temporarily holds the scanned information including paths and an array of all scans. processFile is a servce that handles all file processing (e.g. save, load, remove, dynamically generate file name, determine correct file path).

angular
    .module('app')
    .factory('emailService', ['$ionicPlatform', '$ionicPopup', '$ionicHistory', '$cordovaEmailComposer', 'scanData', 'processFile', emailService]);

function emailService($ionicPlatform, $ionicPopup, $cordovaEmailComposer, $ionicHistory, scanData, processFile) {
  var path = scanData.filePath,
      file = scanData.fileName;

    var service = {
    send: send
  };

  return service;

//------------------------------

  /**
   * Send email
   */
  function send() {
    processFile.save('csv')
      .then(function () {
        console.info('CSV file saved.');

        sendEmail();

      }, function (error) {
        console.error(error);
        //TODO: handle failed save attempt
      });
  }

  /**
   * Invoke cordova email composer to open email client and create pre-defined draft with attachment.
   */
  function sendEmail() {
    console.info('Sending email...');

    $ionicPlatform.ready(function () {
      $cordovaEmailComposer
        .isAvailable()    //ERROR OCCURS HERE
        .then(function () {
          console.info('Email app available.');

          var attachmentPath = getAttachmentPath();

          console.info('Attachment path: ' + attachmentPath);

          var email = {
            to: '[email protected]',
            attachments: [ attachmentPath ],
            subject: 'Incoming Scan',
            body: 'See attached.'
          };

          $cordovaEmailComposer
            .open(email)
            .then(null, function () {
              clearDataPopup();
            });

        }, function () {
          console.warn('Email app not available.');
        });
    });
  }

Also, all components/dependencies are up-to-date, and I have run ionic state reset with no change. I haven't had a chance to test on iOS yet but will update the question once I can do so. Android is the most important platform at this time, so I'm focusing on it for now.

It's very likely this is just some stupid little thing that I'm overlooking, but I'm currently at a loss.

Upvotes: 4

Views: 775

Answers (1)

Pankaj Parkar
Pankaj Parkar

Reputation: 136174

You had mismatch in dependency sequence. Keep one thing in mind, the sequence in which you inject dependency inside DI Inline array do add you should get their respective instance inside controller factory function one the same number of parameter. You $ionicHistory is missed to inject in 3rd place of controller factory function.

.factory('emailService', ['$ionicPlatform', '$ionicPopup', '$ionicHistory', '$cordovaEmailComposer', 'scanData', 'processFile', emailService]);
                                                      //VVVVV//this was missing
  function emailService($ionicPlatform, $ionicPopup, $ionicHistory, $cordovaEmailComposer, $ionicHistory, scanData, processFile) {

Because of missing parameter $cordovaEmailComposer is holding the reference of $ionicHistory

Upvotes: 2

Related Questions