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

how to send email with ionic framework using the native email app

所以我一直在尝试使用 ionic 发送电子邮件。我尝试了很多教程、示例,但除了这个以外没有任何效果:https://www.thepolyglotdeveloper.com/2014/08/send-email-android-ios-ionicframework/

我将在此处离开本教程。请看下面的答案。

非常简单

  1. 转到您的应用根目录
  2. 安装 Email Composer with Attachments Plugin 类型: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. 尝试在实际的智能手机上使用它,因为在模拟器中,如果您没有配置电子邮件应用程序,它将无法正常工作。

如果你卡住了或尝试:https://www.youtube.com/watch?v=kFfNTdJXVok or https://blog.nraboy.com/2014/08/send-email-android-ios-ionicframework

这是我在 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>