← servoy magazine

[Tip] Email plugin: sending attachments

  • Tips

by Harjo Kompagnie
www.directict.nl

I have table: email and a related table: attachments—where I store all the attachments seperatly in a blob. The table: attachments has important columns: filename (TEXT) (where I store the filename!) and: attachment (MEDIA) (where I store the real attachment).

var msgText = "Hi there"
var subject = "this is a test"
var to = "test@youremail.com"
var from = "bingo@servoy.com"
var cc = ""
var bcc = ""
var attach = new Array()
if(email_to_attachments.getSize() != 0)
{
    for ( var i = 0 ; i < email_to_attachments.getSize() ; i++ )
    {
        var record = email_to_attachments.getRecord(i+1)
        attach[i] = plugins.mail.createBinaryAttachment(record.filename, record.attachment)
    }
}

var succes = plugins.mail.sendMail(to, from, subject, msgText, cc, bcc, attach);
if (!succes)
{
    plugins.dialogs.showWarningDialog('Alert','Failed to send mail','OK');
}

You can also send attachments that are stored on your local hard drive:

var msgText = "Hi there"
var subject = "this is a test"
var to = "test@youremail.com"
var from = "bingo@servoy.com"
var cc = ""
var bcc = ""
var attachment1 = plugins.mail.createBinaryAttachment('logo1.gif',application.readFile('c:/temp/a_logo.gif'));
var attachment2 = plugins.mail.createBinaryAttachment('logo2.gif',application.readFile('c:/temp/another_logo.gif'));

var succes = plugins.mail.sendMail(to, from, subject, msgText, cc, bcc, new Array(attachment1,attachment2));

if (!succes)
{
    plugins.dialogs.showWarningDialog('Alert','Failed to send mail','OK');
}

Hope this helps! SERVOY ROCKS!

Comments (1)

Graham Greensall
Harjo This is great tip and proof that you can also read minds as I was working on how to add multiple attachments to emails! I've also finally learnt what 'record.xxx' can be used for. Servoy really does rock and I'm still not out of 2nd gear with it. Thanks again Graham Greensall