Open New Email in Default Application with Javascript

147 views Asked by At

I appreciate any and all help on this. I am a novice. I am customizing an OwnCloud setup, which has a great setup for sharing a link that the application creates via an internal system, but I need it to open an email in the default email application (such as Outlook) and have the link in the email. I am hoping to customize the code that is already there. There is a share.js file and a share.php file. Here are the excerpts from the share.js:

Option 1 - The form area that action contains the link that I'd like to include in the email:

        html += '<label for="linkText" class="hidden-visually">'+t('core', 'Link')+'</label>';
        html += '<input id="linkText" type="text" readonly="readonly" />';
        html += '<input type="checkbox" name="showPassword" id="showPassword" value="1" style="display:none;" /><label for="showPassword" style="display:none;">'+t('core', 'Password protect')+'</label>';
        html += '<div id="linkPass">';
        html += '<label for="linkPassText" class="hidden-visually">'+t('core', 'Password')+'</label>';
        html += '<input id="linkPassText" type="password" placeholder="'+t('core', 'Choose a password for the public link')+'" />';
        html += '<span class="icon-loading-small hidden"></span>';
        html += '</div>';

I tried changing it several ways, such as:

        html += '<form method="POST" action="mailto:[email protected]?body=Link">';
        html += '<label for="linkText" class="hidden-visually">'+t('core', 'Link')+'</label>';
        html += '<input id="linkText" type="text" readonly="readonly" />';
        html += '<input type="checkbox" name="showPassword" id="showPassword" value="1" style="display:none;" /><label for="showPassword" style="display:none;">'+t('core', 'Password protect')+'</label>';
        html += '<div id="linkPass">';
        html += '<label for="linkPassText" class="hidden-visually">'+t('core', 'Password')+'</label>';
        html += '<input id="linkPassText" type="password" placeholder="'+t('core', 'Choose a password for the public link')+'" />';
        html += '<span class="icon-loading-small hidden"></span>';
        html += '<br />';
        html += '<input type="submit" value="Send Link" />';
        html += '</form>';
        html += '</div>';

And then I do get an email to open up, but it does not contain the link.

Option 2 - This is the part of the form that sends a message from the application, but I need it to open up an email message that contains the same stuff it would send internally (or at the very least the link):

                            html += '<form id="emailPrivateLink">';
            html += '<input id="email" style="display:none; width:62%;" value="" placeholder="'+t('core', 'Email link to person')+'" type="text" />';
            html += '<input id="emailButton" style="display:none;" type="submit" value="Send Email" />';

And the part of the js file that that form references:

$(document).on('submit', '#dropdown #emailPrivateLink', function(event) {
    event.preventDefault();
    var link = $('#linkText').val();
    var itemType = $('#dropdown').data('item-type');
    var itemSource = $('#dropdown').data('item-source');
    var file = $('tr').filterAttr('data-id', String(itemSource)).data('file');
    var email = $('email').val();
    var expirationDate = '';
    if ( $('#expirationCheckbox').is(':checked') === true ) {
        expirationDate = $( "#expirationDate" ).val();
    }
    if (email !='') {
        $('#email').prop('disabled', true);
        $('#email').val(t('core', 'Sending ...'));
        $('#emailButton').prop('disabled', true);
        $.post(OC.filePath('core', 'ajax', 'share.php'), { action: 'email', toaddress: email, link: link, itemType: itemType, itemSource: itemSource, file: file, expiration: expirationDate},
            function(result) {
                $('#email').prop('disabled', false);
                $('#emailButton').prop('disabled', false);
            if (result && result.status == 'success') {
                $('#email').css('font-weight', 'bold');
                $('#email').animate({ fontWeight: 'normal' }, 2000, function() {
                    $(this).val('');
                }).val(t('core','Email Sent'));
            } else {
                OC.dialogs.alert(result.data.message, t('core', 'Error while sharing'));

Any and all help is GREATLY appreciated.

1

There are 1 answers

2
Thao Ngo On
function sendMail() {
    var link = "mailto:[email protected]"
             + "[email protected]"
             + "&subject=" + escape("This is my subject")
             + "&body=" + escape("This is content of email")
    ;

    window.location.href = link;
}

Sending emails with Javascript