Code Monkey home page Code Monkey logo

emailtosms-gateway's People

Contributors

bdm1981 avatar

Stargazers

 avatar  avatar

Watchers

 avatar

emailtosms-gateway's Issues

Regex doesn't extract phone number from email "to" when it contains recipient name

When an email is sent with a recipient name, it shows as the following:

Jason <[email protected]>

The current regex code in index.js does not correctly extract the phone number and hence sending the SMS fails.

When I replace the regex code which extracts the phone number with the following seems to do the trick:

/\d+(?=@)/g
I am very much a beginner at this stuff, but so far it is working in my tests.

For convenience, I have copied the NEW index.js below

Thanks!

  • Jason

const parser = require("lambda-multipart-parser");
const client = require("twilio")(
  process.env.ACCOUNT_SID,
  process.env.AUTH_TOKEN
);

/**
 *
 * @param {String} from - FROM parameter passed from SendGrid
 * @returns {String} Containing email address
 */
const extractEmails = from => {
  return from.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gi);
};

/**
 *
 * @param {String} from - FROM parameter passed from SendGrid
 * @returns {Boolean}
 */
const validateSender = from => {
  let emails = extractEmails(from);
  let isValid = false;
  let domains = process.env.DOMAINS.split(",");
  domains.forEach(domain => {
    if (emails[0].indexOf(domain) >= 0) {
      isValid = true;
    }
  });

  return isValid;
};

exports.handler = async event => {
  let data = await parser.parse(event);
  console.log("input data: ", data);

  if (validateSender(data.from)) {
    // parse the to number from the left-hand side of the email address
    let regexp = /\d+(?=@)/g;
    let to = data.to.match(regexp)[0];
    let body = `Subject: ${data.subject}\nMessage: ${data.text}`;

    let message;
    
    try {
      message = await client.messages.create({
        body: body,
        from: process.env.FROM,
        to: to
      });
    } catch(err) {
      console.log(err)
    }

    console.log("Message Output: ", message);
    const response = {
      statusCode: 200,
      body: JSON.stringify({ input: data, output: message })
    };
    return response;
  } else {
    const response = {
      statusCode: 403,
      body: "Unauthorized Sender"
    };
    return response;
  }
};

Error - Cannot read property 'content-type' of undefined

Dear Sir,

I am trying to test this code but am getting the following error below. Can you assist?

{
"errorType": "TypeError",
"errorMessage": "Cannot read property 'content-type' of undefined",
"trace": [
"TypeError: Cannot read property 'content-type' of undefined",
" at /var/task/node_modules/lambda-multipart-parser/index.js:26:42",
" at new Promise ()",
" at Object.parse (/var/task/node_modules/lambda-multipart-parser/index.js:23:26)",
" at Runtime.exports.handler (/var/task/index.js:35:27)",
" at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"
]
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.