Etape 1 : Installer le plugin email-composer
ionic cordova plugin add cordova-plugin-email-composer
npm install --save @ionic-native/email-composer
Etape 2 : Créer une claase (provider ou service) pour gérer les mails:
ionic g provider email
Etape 3 : Déclarer Email Composer dans app.module.ts
Aller dans app/app.modle.ts
Ajouter la bibliothèque Email Composer dans :
- les imports :
import { EmailComposer } from '@ionic-native/email-composer';
- et dans provides: [ ajouter
EmailComposer,
Etape 4 : Modifier la classe email.ts dans les providers
Aller dans providers/email/email.ts et modifier la classe :
import { Injectable } from '@angular/core';
import { EmailComposer } from '@ionic-native/email-composer';
/************************************* *
* Gestion de l'envoi des emails
*****************************/
@Injectable()
export class EmailProvider {
constructor(private_EMAIL:EmailComposer)
{ }
/**
*
* @public
* @method sendMail
* @param to {string} The primary e-mail address
* @param cc {string} The carbon copy e-mail address
* @param bcc {string} The blank carbon copy e-mail address
* @param attachment {string} The attachment to be sent
* @param subject {string} The subject for the e-mail message
* @param body {string} The message content
*
*/
sendEmail(to:string,
cc:string,
bcc:string,
attachment:string,
subject:string,
body:string) :void
{
// Use the plugin isAvailable method to check whether
// the user has configured an email account
this._EMAIL.isAvailable()
.then((available: boolean) =>
{
// Check that plugin has been granted access permissions to
// user's e-mail account
this._EMAIL.hasPermission()
.then((isPermitted : boolean) =>
{
// Define an object containing the
// keys/values for populating the device
// default mail fields when a new message
// is created
letemail:any= {
app :'mailto',
to :to,
cc :cc,
bcc :bcc,
attachments : [
attachment
],
subject :subject,
body :body
};
// Open the device e-mail client and create
// a new e-mail message populated with the
// object containing our message data
this._EMAIL.open(email);
})
.catch((error : any) =>
{
console.log('No access permission granted');
console.dir(error);
});
})
.catch((error : any) =>
{
console.log('User does not appear to have device e-mail account');
console.dir(error);
});
}
}
Etape 5 : Envoyer des emails des pages.ts
importer le service EmailProvider dans votre page.ts :
Source : http://masteringionic.com/blog/2017-10-09-adding-email-functionality-to-an-ionic-application/