Files
awesome-cordova-plugins/src/@ionic-native/plugins/email-composer/index.ts
T

198 lines
4.4 KiB
TypeScript
Raw Normal View History

import { Injectable } from '@angular/core';
2018-04-06 22:38:59 +02:00
import {
Cordova,
CordovaCheck,
IonicNativePlugin,
2018-09-17 18:23:18 +02:00
Plugin,
getPromise
2018-04-06 22:38:59 +02:00
} from '@ionic-native/core';
2016-07-08 00:58:21 +02:00
export interface EmailComposerOptions {
/**
* App to send the email with
*/
2016-12-06 08:02:00 -05:00
app?: string;
/**
* Email address(es) for To field
*/
2018-09-17 16:05:37 +02:00
to?: string | string[];
2016-12-06 08:02:00 -05:00
/**
* Email address(es) for CC field
*/
2018-09-17 16:05:37 +02:00
cc?: string | string[];
2016-12-06 08:02:00 -05:00
/**
* Email address(es) for BCC field
*/
2018-09-17 16:05:37 +02:00
bcc?: string | string[];
2016-12-06 08:02:00 -05:00
/**
* File paths or base64 data streams
*/
attachments?: string[];
2016-12-06 08:02:00 -05:00
/**
* Subject of the email
*/
2016-12-06 08:02:00 -05:00
subject?: string;
/**
* Email body (for HTML, set isHtml to true)
*/
2016-12-06 08:02:00 -05:00
body?: string;
/**
* Indicates if the body is HTML or plain text
*/
2016-12-06 08:02:00 -05:00
isHtml?: boolean;
2018-04-06 22:38:59 +02:00
/**
* Content type of the email (Android only)
*/
type?: string;
2016-12-06 08:02:00 -05:00
}
2016-04-05 21:55:24 +10:00
/**
* @name Email Composer
* @description
*
* Requires Cordova plugin: cordova-plugin-email-composer. For more info, please see the [Email Composer plugin docs](https://github.com/hypery2k/cordova-email-plugin).
2016-04-05 21:55:24 +10:00
*
*
2016-04-05 21:55:24 +10:00
* @usage
2016-07-20 17:17:09 +02:00
* ```typescript
2018-10-10 16:13:45 -05:00
* import { EmailComposer } from '@ionic-native/email-composer/ngx';
2016-04-05 21:55:24 +10:00
*
* constructor(private emailComposer: EmailComposer) { }
2016-04-29 23:56:49 -04:00
*
* ...
*
*
* this.emailComposer.isAvailable().then((available: boolean) =>{
2016-04-06 22:06:46 +10:00
* if(available) {
* //Now we know we can send
* }
* });
2016-04-05 21:55:24 +10:00
*
* let email = {
* to: 'max@mustermann.de',
* cc: 'erika@mustermann.de',
* bcc: ['john@doe.com', 'jane@doe.com'],
* attachments: [
* 'file://img/logo.png',
* 'res://icon.png',
* 'base64:icon.png//iVBORw0KGgoAAAANSUhEUg...',
* 'file://README.pdf'
* ],
* subject: 'Cordova Icons',
* body: 'How are you? Nice greetings from Leipzig',
* isHtml: true
2017-12-28 07:28:44 -05:00
* }
2016-04-05 21:55:24 +10:00
*
* // Send a text message using default options
* this.emailComposer.open(email);
* ```
2016-04-05 21:55:24 +10:00
*
* You can also assign aliases to email apps
* ```ts
* // add alias
* this.email.addAlias('gmail', 'com.google.android.gm');
*
* // then use alias when sending email
* this.email.open({
* app: 'gmail',
* ...
* });
2016-04-05 21:55:24 +10:00
* ```
* @interfaces
* EmailComposerOptions
2016-04-05 21:55:24 +10:00
*/
@Plugin({
pluginName: 'EmailComposer',
plugin: 'cordova-plugin-email-composer',
2016-04-06 00:24:50 +10:00
pluginRef: 'cordova.plugins.email',
repo: 'https://github.com/katzer/cordova-plugin-email-composer',
2018-04-06 22:38:59 +02:00
platforms: ['Amazon Fire OS', 'Android', 'Browser', 'iOS', 'Windows', 'macOS']
2016-04-05 21:55:24 +10:00
})
@Injectable()
export class EmailComposer extends IonicNativePlugin {
2018-09-18 16:24:05 +02:00
/**
* Checks if the app has a permission to access email accounts information
* @return {Promise<boolean>} returns a promise that resolves with a boolean that indicates if the permission was granted
*/
@Cordova({
successIndex: 0,
errorIndex: 2
})
hasPermission(): Promise<boolean> {
return;
}
/**
* Request permission to access email accounts information
* @return {Promise<boolean>} returns a promise that resolves with a boolean that indicates if the permission was granted
*/
@Cordova({
successIndex: 0,
errorIndex: 2
})
requestPermission(): Promise<boolean> {
return;
}
2016-04-29 23:56:49 -04:00
/**
2016-04-06 22:06:46 +10:00
* Verifies if sending emails is supported on the device.
2016-04-29 23:56:49 -04:00
*
2018-04-08 21:21:37 +02:00
* @param {string} [app] App id or uri scheme.
* @returns {Promise<any>} Resolves if available, rejects if not available
2016-04-06 22:06:46 +10:00
*/
@CordovaCheck()
isAvailable(app?: string): Promise<any> {
2017-12-29 10:15:44 -05:00
return getPromise<boolean>((resolve, reject) => {
2016-08-22 19:19:43 -03:00
if (app) {
EmailComposer.getPlugin().isAvailable(app, (isAvailable: boolean) => {
2016-08-22 19:19:43 -03:00
if (isAvailable) {
resolve();
} else {
reject();
}
});
} else {
EmailComposer.getPlugin().isAvailable((isAvailable: boolean) => {
2016-08-22 19:19:43 -03:00
if (isAvailable) {
resolve();
} else {
reject();
}
});
}
2016-04-06 22:06:46 +10:00
});
}
2016-04-05 21:55:24 +10:00
/**
2018-09-18 16:24:05 +02:00
* Displays the email composer pre-filled with data.
*
* @param {EmailComposerOptions} options Email
* @param {any} [scope] Scope for the promise
* @returns {Promise<any>} Resolves promise when the EmailComposer has been opened
*/
@Cordova({
2018-09-18 16:24:05 +02:00
successIndex: 1,
errorIndex: 3
})
2018-09-18 16:24:05 +02:00
open(options: EmailComposerOptions, scope?: any): Promise<any> {
2017-12-28 07:28:44 -05:00
return;
}
2016-04-06 22:06:46 +10:00
/**
* Adds a new mail app alias.
*
2018-04-08 21:21:37 +02:00
* @param {string} alias The alias name
* @param {string} packageName The package name
2016-04-06 22:06:46 +10:00
*/
2016-04-06 00:24:50 +10:00
@Cordova()
2018-04-06 22:38:59 +02:00
addAlias(alias: string, packageName: string): void {}
2016-04-05 21:55:24 +10:00
}