Part 3: Send file attachments by email

Montag, 3. Februar 2014
In this last part I want you to show how you can send an attachment by email. Generally you can add any file from the device you have in your sandbox. I will show you how to add the csv file which we created in Part 1 and Part 2

First we need a Phonegap / Cordova plugin. If you didnt know how to setup your project for use with phonegap, you would have to take a look at Part 2.

1. Add emailComposer Plugin
Open the terminal and cd to your Phonegap / Cordova folder and execute the following command:

cordova plugin add https://github.com/katzer/cordova-plugin-email-composer.git

This is the call structure from the developer website:

/*
 * Opens an email draft pre-filled with the passed properties.
 */
window.plugin.email.open({
    to:          Array, // contains all the email addresses for TO field
    cc:          Array, // contains all the email addresses for CC field
    bcc:         Array, // contains all the email addresses for BCC field
    attachments: Array, // contains all full paths to the files you want to attach
    subject:    String, // represents the subject of the email
    body:       String, // represents the email body (could be HTML code, in this case set isHtml to true)
    isHtml:    Boolean, // indicats if the body is HTML or plain text
});

For further informations about the plugin take a look to the developer website: https://github.com/katzer/cordova-plugin-email-composer

2. Send the email

The function should be self explaining. You can either pass the recipient, cc, bbc as string or as Array. Of course you can send any kind of attachments using this function. If an example is needed how to read file from LocalFileSystem and send them please ask for it and I'm willing to prove an example.

FILE2MAIL: function(recipient, cc, bcc, subject, body, attachementURL) {
    if (!Ext.isArray(recipient)) {
        recipient = new Array(recipient);
    }

    if (!Ext.isArray(cc)) {
        cc = new Array(cc);
    }

    if (!Ext.isArray(bcc)) {
        bcc = new Array(bcc);
    }

    if (!Ext.isArray(attachementURL)) {
        attachementURL = new Array(attachementURL);
    }

    window.plugin.email.open({
        to:          recipient, // contains all the email addresses for TO field
        cc:          cc, // contains all the email addresses for CC field
        bcc:         bcc, // contains all the email addresses for BCC field
        attachments: attachementURL, // contains all full paths to the files you want to attach
        subject:    subject, // represents the subject of the email
        body:       body, // represents the email body (could be HTML code, in this case set isHtml to true)
        isHtml:    true // indicats if the body is HTML or plain text
   });
}

3. Combining Part 1 & Part 2 & Part 3

var dummyStore = Ext.getStore('dummyStore');

STORE2CSV(dummyStore,null, stringSuccess);

function stringSuccess(csv){
    DATA2FILE('export.csv',csv,fileSavedSuccess)
}

function fileSavedSuccess(file){
    FILE2MAIL('dummyEmail@gmail.com','','','hallo test file','Test Attachement',file.fileName);
}


If you have any kind of questions or improvements please leave a comment or contact me. I appreciate any help or hints!


Keine Kommentare:

Kommentar veröffentlichen