Search code examples
angularjsmeteorsendmailmailgunangular-meteor

How to reply to the chat conversation from an email


I have to create a functionality where an email is generated to other chat users when any user make any conversation inside chat. The other user can check the email and can be able to reply from same email into the chat conversation. I am using angular meteor. How can I make this functionality? is there any API in sendgrid or mailgun to handle call from email and add conversation ? or I have to create POST/GET method to accept call on button click in mail and save replied text ?


Solution

  • You can tell sendgrid to do a REST api call to your server when it receives an incoming email.

    When you send an email, set the reply-to email to be something like @chat-reply.myserver.com

    You then set up an end point in your server code to handle these requests. Your code will need to lookup the conversation from the incoming to address, and then it can save a record in the chat.

    Here is some code...

    import { Meteor } from 'meteor/meteor'
    formidable = require('formidable');     // Formidable does upload form/file parsing
    import { Profiles } from '../imports/api/collections';
    import { inboundReply } from '../imports/api/inbound/methods.js';
    
    const debug = require('debug')('myapp:inbound')
    
    // Needs to run on the server and client, why this is not in the routing.js file
    //   which is only only runs on the client.
    //   Inbound emails, for loop reply
    //
    // This is a RESTAPI end point which is called by sendgrid,
    //   any email to [email protected] will come here. Our job
    //   is to parse it , work out which loop it relates to, and save it as a message
    //   in the database
    //
    Router.route('/inbound', function () {
    
        // Need to use formidable because SendGrid inbound data is encoded as a multipart/form-data
        const form = new formidable.IncomingForm();
        // Meteor bind eviron. to get callback
        debug(this.request.body)
        let r = this.response
        form.parse(this.request, Meteor.bindEnvironment(function (error, fields, files) {
          if (error)
            console.error(error);
          let errs = []
    
          // Gets the to field
          const toField = _.find(fields, function(value, key) { if (key === 'to') { return value; }});
    
          // Gets the from field
          const fromField = _.find(fields, function(value, key) { if (key === 'from') { return value; }});
    
          // Gets the html content, email
          const content = _.find(fields, function(value, key) { if (key === 'text') { return value; }});
    
          let cleanContent;
          if (content){
            // Logger.trace({content: content});
            // Regex removes html
            // cleanContent  = content.replace(/<br>/ig, "\n");
            // const regex = /(<([^>]+)>)/ig
            // cleanContent  = cleanContent.replace(regex, "");
            // Logger.trace({cleanContent: cleanContent});
            let lines = content.split(/\n/);
            debug("Incoming body",lines);