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