title: Modify Twilio sample code to attach voicemail recoding and send by email
body: <p>The following post from Twilio explains how to write and implement a function for sending the link of a recorded message to email.</p>
  <p><a href="https://www.twilio.com/blog/forward-voicemail-recordings-to-email" rel="nofollow noreferrer">https://www.twilio.com/blog/forward-voicemail-recordings-to-email</a></p>
  <p>I want to attach to the voicemail recording rather than just send the link. Here is my attempt at modifying their code. Unfortunately it's causing a 500 error. I'm not very experience with NodeJS so perhaps you can help.</p>
  <p>Dependencies for <code>SendGrid</code> and <code>FS</code> are included and this function worked perfectly until I modified it with <code>FS</code>, <code>pathToAttachment</code>, attachment variables and the attachments array.</p>
  <pre><code>//Initialize SendGrid Mail Client
  const sgMail = require('@sendgrid/mail');
  
  //FileSystem
  const fs = require(&quot;fs&quot;);
  
  // Define Handler function required for all Twilio Functions
  exports.handler = function(context, event, callback) {
      
  pathToAttachment = `${event.RecordingUrl}`;
  attachment = fs.readFileSync(pathToAttachment).toString(&quot;base64&quot;);
  
  // Build SG mail request
  sgMail.setApiKey(context.SENDGRID_API_SECRET);
          // Define message params
          const msg = {
            to: context.TO_EMAIL_ADDRESS,
            from: context.FROM_EMAIL_ADDRESS,
            fromname: 'Voicemail',
            text: `URL is: ${event.RecordingUrl}`,
            subject: `${event.From} (${event.country} / ${event.lang})`,
            attachments: [
              {
                  content: attachment,
                  filename: &quot;Voicemail.wav&quot;,
                  disposition: &quot;attachment&quot;
              }
            ]
          };
          // Send message
          sgMail.send(msg)
          .then(response =&gt; {
              console.log(&quot;Neat.&quot;)
              callback();
          })
          .catch(err =&gt; {
              console.log(&quot;Not neat.&quot;)
              callback(err);
          });
  };
  </code></pre>