title: Twilio Function to send VoiceMail or Recording to Email
body: <p>Twilio Stuido does not provide a native way to email a recording therefore this needs to be coded. In this example, a voicemail has been taken of a users call. Now that we have the recording a Twilio Function is created (NodeJS) to send this recording to email.</p>
  
  <p>In the sample code below the function is failing however there is no available debug tool inside the Twilio Console for dealing with functions. NodeJS is fairly new to us so this maybe easy for someone to spot.</p>
  
  <p>Possible other errors may be however:</p>
  
  <ol>
  <li>Incorrect gmail authentication (although the details are correct in terms of username and password, perhaps this is not how you configure google apps to send the email)</li>
  <li>Inability to import or incorrectly importation of the nodemailer dependency into Twilio</li>
  <li>Bad NodeJS skills</li>
  </ol>
  
  <p>The input variables are:</p>
  
  <p><strong>attachment</strong>
      <em>{{widgets.Voicemail_Recording.RecordingUrl}}</em>
      - which contains the URL to the voicemail recording.</p>
  
  <p><strong>lang</strong>
      - the language of the caller (based on IVR choices earlier).</p>
  
  <p><strong>phone_number</strong>
  <em>{{trigger.call.From}}</em></p>
  
  <p>NodeJS Twilio Function</p>
  
  <pre><code>var mailer = require('nodemailer');
  mailer.SMTP = {
      host: 'smtp.gmail.com', 
      port:587,
      use_authentication: true, 
      user: 'info@example.com',
      pass: '*********'
  };
  
  exports.handler = function(context, event, callback) {
  
      mailer.send_mail({       
          sender: event.phone_number + '&lt;info@example.com&gt;',
          to: 'info@example.com',
          subject: 'Voicemail (' + event.lang + ')',
          body: 'mail content...',
          attachments: [{'filename': event.attachment, 'content': data}]
      }), function(err, success) {
          if (err) {
  
          }
  
      }
  };
  </code></pre>