Send mail from Ionic

-1

I'm following this guide , and I can not get it to work for me.

I have installed the pluggin and I have copied the following code in my method:

public enviarCorreo() {
      let email = {
      app: 'gmail',
      to: '[email protected]',
      cc: '[email protected]',
      subject: 'Hola',
      body: 'Que piensas?',
      isHtml: true
    };
    this.emailComposer.open(email)
}

But it does not do anything to me when I put "Send". Investigating the console, he tells me the following message:

  

Ionic Native: tried calling EmailComposer.open, but Cordova is not   available Make sure to a) run in a real device or simulator and b)   include cordova.js in your index.html

In the basic installation I understand that I should already create the file cordova.js but I searched the entire disk and it is not. I searched for the code on the Cordova website, but I also did not find anything. What file do I need? Is there an alternative?

    
asked by Vitaly 10.08.2018 в 15:22
source

1 answer

2

I'm going to give you a step by step, and now you see what you've missed.

First we must install the plugin or respective plugin of cordova-plugin-email-composer , we do it with the respective commands:

$ ionic cordova plugin add cordova-plugin-email-composer
$ npm install --save @ionic-native/email-composer

You can run each command separately or you can execute them in a single line in the following way:

$ ionic cordova plugin add cordova-plugin-email-composer && $ npm install --save @ionic-native/email-composer

As a second step we must add the plugin to the module of your application import { EmailComposer } from '@ionic-native/email-composer'; .

As it should be seen in app.module.ts :

  

Note: Just add what I mentioned, the rest is a simple base.

import { BrowserModule } from '@angular/platform-browser';
import { ErrorHandler, NgModule } from '@angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';

import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';

import { Camera } from '@ionic-native/camera';
//Agregamos el componente:
import { EmailComposer } from '@ionic-native/email-composer';

@NgModule({
  declarations: [
    MyApp,
    HomePage
  ],
  imports: [
    BrowserModule,
    IonicModule.forRoot(MyApp)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    HomePage
  ],
  providers: [
    //Además procedemos a agregarlo como un provider.
    EmailComposer,
    {provide: ErrorHandler, useClass: IonicErrorHandler}
  ]
})
export class AppModule {}

And as a last step you must execute it in your component or in the view where you want to perform this function:

//Importacion del plugin
import { EmailComposer } from '@ionic-native/email-composer';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  //Lo adicionamos en el constructor de la clase
  //emailComposer puedes declararla como tu quieras, es una variable, pero debes de tener cuidado cuando la vayas a usar, porque debe de llamarse igual.
  constructor(private emailComposer: EmailComposer) { }


  enviarCorreo() {
    let email = {
      to: '[email protected]',
      cc: '[email protected]',
      subject: 'Hola',
      body: 'Que piensas?',
      isHtml: true
    };

    this.emailComposer.open(email);
  }

}

I have attached documentation so you can expand your concepts about this plugin: Video tutorial , and a Tutorial about this functionality for ionic or the documentation from ionic Native

Problems with CORDOVA

Brief description of what this resolves:

Native plugins do not work with the live server for users who use Cordova 8 . The current route in the configuration is valid for versions less than Cordova 8 . However, if you install ionic today, following the instructions on the site you get Cordova 8 by default and therefore the live server can not find Cordova . Everything you try to do in terms of native operations native operations results in the error message: Cordova is not available. Make sure to include cordova.js or run in a device/simulator (running in emulator)

This has been broken since at least this 11/26/2016 Ask in StackOverflow English .

To maintain compatibility with previous versions of Cordova 7, modify the variables of the platform route to the string arrays and include the possible routes of the platform.

As a solution, you can edit the file:

  

node_modules/@ionic/app-scripts/dist/dev-server/serve-config.js   replace

Which contains this section of JavaScript code:

exports.ANDROID_PLATFORM_PATH = path.join('platforms', 'android', 'assets', 'www');

So you should replace or update it with this section:

exports.ANDROID_PLATFORM_PATH = path.join('platforms', 'android', 'app', 'src', 'main', 'assets', 'www');
    
answered by 10.08.2018 в 15:52