Using the Mail Plugin

I’m a a complete Novice to Sevoy and JavaScript (and any other programming). I’m struggling to work out how to use the Mail plugin.

I am trying to build a rudimentary CRM system. I want to be able to go to a contacts record and see all the email that has been sent by that customer.

Can anyone help - or point me in the right direction?

Many thanks.

Apologies but just to avoid any confusion the second line should read

“I want to be able to go to a contacts record and see all the email that has been received from that contact”

Not

“… all the email that has been sent by that customer”.

Hi MerMer, welcome to Servoy!

Just build a relation between customer table and the table where you store the incoming mail.

The problem could be: what is the best way to associate these tables?. I would say: during incoming mail examination associate each mail with the PK of the customer… or… define an e-mail field for each customer and use the mail “from” field for the relation (in this case the problem arouses if the customers changes his mail settings)

Automazione,

Many thanks for the reply. My problem is more fundamental and shows my ignorance. How do I get the email into a table in the first place?

I am presuming my steps are.

  1. Get the email into some sort of table.
  2. Use the mail “from” field to make a relationship in the contact record.

My problem is getting the first bit done without really understanding how the plugin works.

Many thanks

MerMer

How do I get the email into a table in the first place?

Hello,

if you right click on any function offered in the method editor, you have the option to “move code” or “move sample”. Most of the times, “move sample” will help you to get going.

“Move code” of plugins.mail.receiveMail will give you this

plugins.mail.receiveMail( userName, password, leaveMsgsOnServer, [receiveMode], [onlyreceiveMsgWithSentDate], [overridePreferencePOP3Host/properties array])

This means, you have to supply

  • a userName for the account
  • a password for the account
  • a true/false to tell the plugin to either leave messages on the server or delete them

Parameters in brackets mean they are optional. In this case

  • receiveMode: 0=FULL, 1=HEADERS_ONLY, 2=NO_ATTACHMENTS (from the “move sample” option
  • onlyreceiveMsgWithSentDate: will allow you to receive a single message by its date (so you can receive headers first and then use the date from a header to receive a single message)
  • overridePreferencePOP3Host/properties array: allows you to change the Pop3 server to whatever you like (otherwise, the server set in the general seetings is used)

Once you are ready to use this function and it does return mails for you, the general approach is this (or see “move sample”):

var msgs = plugins.mail.receiveMail('me', 'test', true);  // receives mails for user "me", password "test", the server set in the general preferences, mails are left on the server; it returns an array of messages

// Let's suppose you have a table that looks like this (adapt to your database)

/*
CREATE TABLE emails (
	emails_id int,
	from varchar(4000),
	date datetime,
	subject varchar (255),
	msg varchar(4000));
	
*/

if (msgs != null) // if is null error occurred!
{
	for (var i = 0 ; i < msgs.length ; i++)  // looping over the length of the array (the number of messages); Arrays are zero based so start with 0
	{
		var msg = msgs[i] // getting message i into variable msg

		// creating a new record in table "emails"
		
		controller.newRecord();
		
		// now you have a single message in the variable msg
		// you can then use all functions available under "MailMessage" in the plugin
		// for example:
		from = msg.getFromAddresses(); // setting your database field "from" to the from addresses from the plugin (might be several!)
		date = msg.getReceivedDate(); // etc.
		subject = msg.getSubject();
		msg = msg.getPlainMsg();

		controller.saveData();
	}
}

You will see, after you get used to JavaScript, this is very easy to do.

A good starting point for JavaScript might be

http://www.w3schools.com/js/default.asp or
http://developer.mozilla.org/en/docs/Co … _Reference

I hope this helps to get you going…

Patrick

Patrick,

Very helpful, many thanks. I hadn’t spotted the “controller.Savedata()” function. I will work through your code and see if I can get thing to work.

I’ll also check out the JavaScript references. I’ve signed up to VTC.com where they have several hours of on-line videos but these other sources might be very useful for me.

Merric

just in case… have a look at www.servoymagazine.com, lot of nice articles & movies about servoy…

just in case … have a look at the tutorials on developers section of www.servoy.com...

Patrick,

Your code sample has been a great help. However, one snag - I am getting the full email address such as “John Smith Jsmith@aol.com” when I use the code, emailfrom = msg.getFromAddresses().

How do I just get jsmith@aol.com?

My first instinct was to try and use the PlainMail address helper.

I tried ammending the code byreplacing “emailfrom=msg.getFromAddresses()” with “emailfrom = plugins.mail.getPlainMailAddresses(‘msg’)”

Now in the emailfrom field I get [Ljava.Lang.String@1838938 returned or something similar.

Where am I going wrong? Many thanks.

Automazione, thank you for your links. I’ve steadily ploughed through most of the video tutorials on the Servoy site. I think they are excellent - they really helped me get an appreciation of the capabilities of Servoy. I wish there were several more. What would be good for a newbie like me is to see a series of videos working through building an application from scratch and working through the common problems on the way.

MerMer:
Patrick,

Your code sample has been a great help. However, one snag - I am getting the full email address such as “John Smith Jsmith@aol.com” when I use the code, emailfrom = msg.getFromAddresses().

How do I just get jsmith@aol.com?

that’s one of the things that made me suggest before to manually associate each incoming message to a costumer ;)

Anyway: email address cannot have spaces so you can catch the last “word” of the address with the following line:

emailfrom = utils.stringRightWords(msg.getFromAddresses(), 1)

Automazione,

Thanks for the suggestion. It almost works - but I made an error in my last email and forgot to show the brackets around the email address. For example, I’ve been getting
"John Smith John.smith@aol.com

Your solution leaves me with John.smith@aol.com which is close but now I need to get rid of the brackets.

Also, I still don’t understand why the plugins.mail.getPlainMailAddresses function did not work for me.

MerMer

I got it to work by using the additional two lines

emailfrom = utils.stringReplace(emaifrom, ‘<’, ‘’)
emailfrom = utils.stringReplace(emaifrom, ‘>’, ‘’)

… but still not sure where I went wrong with the plugins.mail.getPlainMailAddresses function.

MerMer

ooops :oops:

Sorry I missed that nice helper function :oops:

How did you implement it?
Look at the move sample code?

var xx = plugins.mail.getPlainMailAddresses(msg.getFromAddresses());
emailfrom = xx[0];

should work