We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Parsing an email

adlikon
adlikon asked
on
Medium Priority
235 Views
Last Modified: 2012-05-06
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
Comment
Watch Question

IT Expert
CERTIFIED EXPERT
Top Expert 2009
Commented:
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)

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.