Getting Attachments with Email.

I am trying to get attachments from incoming emails and store these in a table called “email_attachments”.

I am using the code below as part of the method that pick up the email. The code seems to run through the deugger fine.

However, O can’t get the attachment to upload into the table and the filesize keeps getting returned as 0. The emailid and the name of the file get added to the record without any problem.

The ‘attachment’ column in the tables is set up to accept Media files. I can’t see where I am going wrong. Can anybody point me in the right direction.

Many thanks

MerMer

if (attachments != null)
{
//e_attachmentcount = attachments.length
//email_attach_num = e_attachmentcount

for (var j = 0 ; j < attachments.length ; j++)
{
forms.email_attachments.controller.newRecord()
var attachment = attachments[j]
var attachmentDataByteArray = attachment.getData();
application.output (plugins.file.getFileSize(attachment));
forms.email_attachments.emailid = emailID; //enter emailID of email
forms.email_attachments.filename = attachment.getName();
forms.email_attachments.attachment = attachmentDataByteArray ; //put in blob
forms.email_attachments.filesize = forms.email_attachments.length;

}
}
forms.email_attachments.controller.saveData()

The method plugins.file.getFileSize(your are using is meant to work for files, not variables. Second, this

forms.email_attachments.length

should actually give you an error, because a form doesn’t have a length.

I am not 100% sure (without investigating), but I would suppose that attachments[j] will return a byte, so you could write

forms.email_attachments.controller.newRecord() 
forms.email_attachments.emailid = emailID; //enter emailID of email
forms.email_attachments.filename = attachment.getName();
forms.email_attachments.attachment = attachments[j].getData(); //put in blob
forms.email_attachments.filesize = attachments[j].getData().length;

What happens if you try that way?

Another problem is this:

var attachment = ...

You declare a variable with the same name as one of your fields. I suppose whenever you address attachment, you get the value from the record.

Following code works successfully over here - might be useful starting point for you:

if (attachments != null)
{
e_attachmentcount = attachments.length
email_attach_num = e_attachmentcount

for (var j = 0 ; j < attachments.length ; j++)
{
forms.wx_media.controller.newRecord()
var attachment = attachments[j]
forms.wx_media.me_doc_id = emailID // mail_itemsid
forms.wx_media.me_filename = attachment.getName()
forms.wx_media.me_blob = attachment.getData() //put in blob
forms.wx_media.me_filesize = forms.wx_media.me_blob.length
}
}

Graham Greensall
Worxinfo Ltd

Many thanks for the responses.

I had stupidly missed out ‘attachment’ to the following

forms.email_attachments.filesize = forms.email_attachments.length;

Once I altered this to

forms.email_attachments.filesize = forms.email_attachments.attachment.length;

The code worked fine. It showed me that I was in fact succesfully saving the file into table, just not reading file size correctly.

Now, I done that - I have another obvious question. How do I actually view the files that have been uploaded to the table?

Thanks

MerMer

Try this

I have a relationship Document_ID_to_Media_fk_DocumentID and list all Attachments related to the current Document/Email on a Tab. Then attach following Method to the Attachments list.

I believe credit for the coding goes to Harjo from a Tip on Servoy Magazine

if ( me_filename) // check if filename exists
{
// Creates a temporary file (will be deleted after application shutdown)
// get the position of the last “.” in the file name
var a = me_filename.lastIndexOf(“.”)
var b = “temp”

//get the 3 letter extension - INCLUDING the “.”
var c = me_filename.substring(a)

//create a temporary file that will be auto-deleted by Servoy when client is exited
var filename = application.createTempFile(b,c)

//write the binary data out to disk at the temporary file location
application.writeFile(filename,me_blob);

//launch the document with external application
if(utils.stringMiddle(application.getOSName(),1,7) == “Windows”)
{
application.executeProgramInBackground(‘rundll32’, ‘url.dll,FileProtocolHandler’,filename)
}
else
{
application.executeProgram(‘open’, filename);
}

}
else
{
plugins.dialogs.showErrorDialog( ’ W a r n i n g ', ‘Action failed due to incomplete filename’ , ‘OK’)
}

Best of luck

Graham Greensall
Worxinfo Ltd

Graham,

Many thanks. I’ve found almost exactly the same thing searching through these forums. The only difference is this also allows for MACS. The forum message also provided another way for it to work with Linux - though this was less clear.

var filename = forms.email_attachments.filename // Get the name of the file
application.output (filename)
var a = filename.lastIndexOf(“.”)//returns the number from beginning of file name to the dot.
application.output (a)
var b = filename.substr(0,a) //returns the file name
application.output(b)
var c = filename.substring(a) //returns the file extension.
application.output(c)

var filename = application.createTempFile(b,c) // creates a temp file using the file name and extension
application.writeFile(filename,attachment);

if(utils.stringMiddle(application.getOSName(),1,7) == “Windows”)
{
application.executeProgramInBackground(‘rundll32’, ‘url.dll,FileProtocolHandler’,filename)
}
else
{
if (utils.stringLeftWords(application.getOSName(), 1) == “Mac”)
{
application.executeProgram(‘open’, filename);
}
}

So, on to my next question (more because I want to understand and learn the capabilities of Servoy then a real need) Once you’ve opened a file on the local machine, is there a way to make an ammendment to that file and save it back to the same record on the server automatically?

Cheers

MerMer