• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 221
  • Last Modified:

Parsing an email

Hi, I am pusing an email into my application using SOAP, I then parse the email to populate the database.


I am able to scan the email  by looking for a colon delimiter and get some of the fields, thats easy. What I do not know is how to do is pick out just the text of the mail. An example is attached, I need to parse from "Content-Transfer-Encoding: quoted-printable" and take everything until first occurence of "------". How can I do this in javascript ? This code I did not write , I am pickuping up the project and am a novice with java script ? Thanks for your help
var EmailParser = Class.create();
 
EmailParser.prototype = {
  initialize : function() {
    this.headers = "";
    this.body = "";
    this.recipients = "";
    this.subject = "";
    this.user = "";
    //added
    this.body_text = "";
    this.copied = "";
  },
 
  parse: function(emailString) {
    var delim = "\n";
   
    var bodyStart = false;
    var stok = new Packages.java.util.StringTokenizer(emailString, delim, true);
    while (stok.hasMoreTokens()) {
      var tok = stok.nextToken();
      if (tok == delim) {
        if (!stok.hasMoreTokens()) {
          break;
        }
        tok = stok.nextToken();
        if (tok == delim) {
          bodyStart = true;
          continue;
        }
      }
 
      if (bodyStart) {
        this.body += tok + delim;
      } else {
        var header = tok.split(":");
        var name = header[0].trim().toLowerCase();
        var value = header[1].trim();
        if (name == "to") {
          this.recipients = this.findEmailAddresses(value);
        }
        if (name == "subject") {
          this.subject = value.trim();
        }
        if (name == "from") {
          this.user = this.findEmailAddresses(value);
        }
         //added
        if (name == "cc") {
            this.copied = this.findEmailAddresses(value);
        }
       if (name == "content-transfer-encoding") {
            this.body_text = value.trim();
        }
         
        this.headers += tok + delim;
      }
 
 
    }
  },
 
  save: function() {
    var egr = new GlideRecord("sys_email");
    egr.initialize();
    egr.mailbox = "received";
    egr.recipients = this.recipients;
    egr.user = this.user;
    egr.subject = this.subject;
    egr.headers = this.headers;
    egr.body = this.body;
    //added
    egr.body_text = this.body;
    egr.copied = this.copied;
    var sysid = egr.insert();
 
    Packages.com.glide.policy.EventManager.queue("email.read", "", sysid, "");
  },
 
  findEmailAddresses: function(StrObj) {
    var separateEmailsBy = ", ";
    var email = "<none>"; // if no match, use this
    var emailsArray = StrObj.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
    if (emailsArray) {
      email = "";
      for (var i = 0; i < emailsArray.length; i++) {
        if (i != 0)  
          email += separateEmailsBy;
          email += emailsArray[i];
        }
      }
    return email;
    }
 
};

Open in new window

a.txt
0
adlikon
Asked:
adlikon
1 Solution
 
Michel PlungjanIT ExpertCommented:
Erm...

Javascript novice, soap and prototype.js library

Does not sound like a great mix.
According to what I can see, you get a body of "quoted printable" since that is the value of the part you extract (which is line by line)

So why not just do (not heavily tested and without much safetynetting)

var bodyStart=0;
var start = emailString.toLowerCase().indexOf('content-transfer-encoding')
if (start!=-1) start = emailString.indexOf('\n',start)+1;  // skip that line
var end = emailString.indexOf('------_=_NextPart');
if (end==-1) end = emailString.length;
var myBody = emailString.substring(start,end)
0
 
adlikonAuthor Commented:
thanks for your input, I'll give you the points as you answered my query but I have decided the best place is to decode this at Perl level on the unix box before handing the mail to the app.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now