Email Attachments

Can anyone give me some clues on how to store in bound email attachments.

I would like them to be stored in a file system rather than directly into the database.

Also, what is the best way to link incoming emails to a contact in my contact database?

Many thanks

MerMer

Hi MerMer

At present we are just saving attachments with the database system. Clients can open up and save locally but always have a backup if they delete/mess up the local copy.

Following is part of GetMail method which strips out the actual email address ie xxx@yyy.com - runs a search on all three email fields in this particular system - if match found it gets the customercode & sets as FK in the email document & sets up a link/join table between the Contact and Document.

I

var FullEmail = email_from
if ( !FullEmail )
{
FullEmail = ’ ’
}
var pos1 = 0
var pos2 = FullEmail.length

if ( utils.stringPatternCount( FullEmail,‘<’)>0 )
{ pos1 = utils.stringPosition( FullEmail,‘<’,1,1) }

if ( utils.stringPatternCount( FullEmail,‘>’)>0 )
{ pos2 = utils.stringPosition( FullEmail,‘>’,1,1) }
else { pos2 += 1 }

globals.gtext01 = utils.stringMiddle( FullEmail, (pos1 + 1), ((pos2-1)-pos1));

forms.wx_contacts.controller.find()
forms.wx_contacts.comp_email = ‘%’ + globals.gtext01 + ‘%’

forms.wx_contacts.controller.newRecord()
forms.wx_contacts.comp_email2 = ‘%’ + globals.gtext01 + ‘%’

forms.wx_contacts.controller.newRecord()
forms.wx_contacts.comp_email3 = ‘%’ + globals.gtext01 + ‘%’

forms.wx_contacts.controller.search()

// force reset of REL: to current Contact
globals.gcurr_link_ctcode = null
globals.gcurr_link_ctcode = forms.wx_contacts.customercode

// if
if ( globals.gcurr_link_ctcode )
{
ctcode = forms.wx_contacts.customercode // set CTCODE in wx_documents from the found Contact

// setup CONTACT → DOC link/join table
forms.wx_link_ct_doc.controller.newRecord(true)
forms.wx_link_ct_doc.zr_ctcode = globals.gcurr_link_ctcode // uses ct_code
forms.wx_link_ct_doc.zr_doc_id = globals.gcurr_link_docd // globals.gcurr_link_docd passed from wx_EM_GetEmails
forms.wx_link_ct_doc.controller.saveData()

}
controller.saveData()

Hope this useful

Graham Greensall
Worxinfo Ltd

Graham,

Many thanks indeed. I’m very much a novice and it really is invaluable to get such good feedback from these forums. I do have a few questions.

I don’t understand the purpose of using newRecord() in the wx_contacts. I can’t see how this helps find existing records that have the email string. Doesn’t this function simply add a new record? If so why is this neccessary?

I also don’t quite understand the purpose of the line “globals.gcurr_link_ctcode = null” as this variable seems to be immediately altered in the next line by assigning it the customercode from wx_contacts. Am I right in guessing that it is there just in case there is no matching email record in wx_contacts?

Also, I would be very interested to see how you save attachments with the database system. I would like to see the alternatives and the more code I look at the more I feel like I’m starting to get a handle on how to use Servoy.

Many thanks

MerMer

You’re welcome - much of my knowledge - and all the really clever code - has come from this Forum.

I don’t understand the purpose of using newRecord() in the wx_contacts. I can’t see how this helps find existing records that have the email string. Doesn’t this function simply add a new record? If so why is this neccessary?

This enables you to search in multiple fields in the same Search operation

I also don’t quite understand the purpose of the line “globals.gcurr_link_ctcode = null” as this variable seems to be immediately altered in the next line by assigning it the customercode from wx_contacts. Am I right in guessing that it is there just in case there is no matching email record in wx_contacts?

Ensures that gcurr_link_ctcode is null before assigning it to the found record - if any. Belt & braces!

Adding attachments:

var emailID = iddc
var attachments = msg.getAttachments()

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
}
}

Regards

Graham Greensall
Worxinfo Ltd

I don’t understand the purpose of using newRecord() in the wx_contacts. I can’t see how this helps find existing records that have the email string. Doesn’t this function simply add a new record? If so why is this neccessary?

Graham is just adding new FIND requests with the new record. You can tell because they come after the controller.find but before the controller.search

HTH

Graham is just adding new FIND requests with the new record. You can tell because they come after the controller.find but before the controller.search

Thanks for this… big help in making me understand the code fully.

I have another question - I am trying to check the Sent date of the message headers and then only create create records for messages with a unqiue sent date. The code I am using is below. It seems to work fine until the
"var msg = … " even though I have new messages waiting. Where am I going wrong?

var receiveMode = 1;

var headers = plugins.mail.receiveMail(‘myemailaccount’, ‘username’, true ,receiveMode);

if (headers )
{
for ( var k = 0 ; k < headers.length ; k++ ) //look at each header, one at a time.
{
var header = headers[k];
globals.email_message_date = header.getSentDate();
application.output(globals.email_message_date);
application.output(gcheckemail_to_email.getSize())
if (gcheckemail_to_email.getSize() == 0) // check to see if message is already there
{
application.output(header.getSentDate())
var msg = plugins.mail.recieveMail(‘myemailaccount’, ‘username’, true , 0 , header.getSentDate());

I’m not that much into emailing myself but this

var msg = plugins.mail.recieveMail(‘myemailaccount’, ‘username’, true , 0 , header.getSentDate());

seems odd, unless it’s a typo in the transcript:

I would expect “receiveMail” not “recieveMail”

hth

Yes, just a typo in the transcript - I wish it was that simple. Still can’t work out why the

"var msg = plugins.mail.receiveMail(‘myemailaccount’, ‘username’, true , 0 , header.getSentDate()); " is turning up Null.

I tested it by taking out the header.getSentDate() and it still returned a null.

Can’t figure it out at all.

MerMer