如何使用本机电子邮件应用程序发送带离子框架的电子邮件

时间:2015-09-30 18:24:56

标签: ionic-framework ionic-view

所以我一直试图用离子发送电子邮件。我尝试了许多教程,例子,但除了这一个之外没有任何工作:https://blog.nraboy.com/2014/08/send-email-android-ios-ionicframework/

我要离开这个教程。请参阅下面的答案。

2 个答案:

答案 0 :(得分:3)

以下是我在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
  });
 }
});

在我的index.html:

<button ng-click="sendEmail()" class="button button-icon icon ion-email">
   Send mail
</button>

答案 1 :(得分:2)

非常容易

  1. 转到您的应用根目录
  2. 安装带有附件插件的Email Composer 键入:cordova plugin add https://github.com/jcjee/email-composer.gitlink for repo
  3. 为您所需的平台重新构建项目,例如android:ionic build android
  4. 现在准备AngularJS控制器:

    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. 现在,在您的html视图中,您可以使用方法sendEmail(email)

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

  6. 尝试在实际的智能手机上使用它,因为在模拟器中如果您没有配置的电子邮件应用程序,它将无法正常工作。

  7. 如果您遇到问题或尝试某事:https://www.youtube.com/watch?v=kFfNTdJXVokhttps://blog.nraboy.com/2014/08/send-email-android-ios-ionicframework