Parsing an email

Posted on 2009-02-19
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, 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();"", "", 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

Question by:adlikon
    LVL 75

    Accepted Solution


    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)

    Author Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Suggested Solutions

    I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
    Article by: DanRollins
    This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (, we'll extend the program by adding a depth-…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now