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


Parsing an email

adlikon asked
Medium Priority
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 = "";
    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()) {
        tok = stok.nextToken();
        if (tok == delim) {
          bodyStart = true;
      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);
        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.mailbox = "received";
    egr.recipients = this.recipients;
    egr.user = this.user;
    egr.subject = this.subject;
    egr.headers = this.headers;
    egr.body = this.body;
    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

Watch Question

IT Expert
Top Expert 2009

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


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.


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.