5

So I was stuck trying to send email with ionic. I tried many tutorials, examples but nothing worked except this one: https://www.thepolyglotdeveloper.com/2014/08/send-email-android-ios-ionicframework/.

I'm leaving this tutorial here. Please see below for the answer.

Nic Raboy
  • 3,143
  • 24
  • 26
CommonSenseCode
  • 23,522
  • 33
  • 131
  • 186

2 Answers2

3

Very Easy

  1. Go to your app root directory
  2. Install Email Composer with Attachments Plugin type: cordova plugin add https://github.com/jcjee/email-composer.git, link for repo
  3. Re-build project for your desired platform for example for android: ionic build android
  4. Now prepare your AngularJS controller:

    angular.module('myApp').controller('WhatToDoController', function ($scope, $state) {
    
    var whatToDo = this;
    
    /**
     * Sends an email using Email composer with attachments plugin and using
     * parameter email.
     *
     * @param email
     */
    whatToDo.sendEmail = function (email) {
      if (window.plugins && window.plugins.emailComposer) { //check if plugin exists
    
        window.plugins.emailComposer.showEmailComposerWithCallback(function (result) {
            //console.log("Email sent successfully");
          },
    
          null,        // Subject
          null,        // Body
          [email],     // To (Email to send)
          null,        // CC
          null,        // BCC
          false,       // isHTML
          null,        // Attachments
          null);       // Attachment Data
      }
    
    }
    });
    
  5. Now in your html view you can use the method sendEmail(email):

    <p>
    Send an email to <a href="#" ng-click="whatToDo.sendEmail('example@email.com')">example@email.com</a>
    </p>
    

  6. Try to use this on an actual smartphone since in the emulator if you have no configured email app it won't work properly.

If you get stuck or something try: https://www.youtube.com/watch?v=kFfNTdJXVok or https://blog.nraboy.com/2014/08/send-email-android-ios-ionicframework

CommonSenseCode
  • 23,522
  • 33
  • 131
  • 186
  • I followed the blog, the function in the library fires the callback is never executed. I can't seem to find the issue. There's no error on console :/ – Muhammad Ahsan Ayaz Feb 29 '16 at 07:39
3

Here is how I use it in my app.js:

.controller('EmailCtrl', function($cordovaEmailComposer, $scope) {
$cordovaEmailComposer.isAvailable().then(function() {
   // is available
   alert("available");
 }, function () {
   // not available
   alert("not available");
 });
 $scope.sendEmail = function(){
  var email = {
     to: 'teste@example.com',
     cc: 'teste@example.com',
     bcc: ['john@doe.com', 'jane@doe.com'],
     attachments: [
       'file://img/logo.png',
       'res://icon.png',
       'base64:icon.png//iVBORw0KGgoAAAANSUhEUg...',
       'file://README.pdf'
     ],
     subject: 'Mail subject',
     body: 'How are you? Nice greetings from Leipzig',
     isHtml: true
  };

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

And here in my index.html:

<button ng-click="sendEmail()" class="button button-icon icon ion-email">
   Send mail
</button>
bandojulio
  • 129
  • 2
  • 5