title: How to attach files to POST request to Mailgun's API using Node.js and the Request library
body: <p>I'm writing some code to send emails with attachments through the <a href="http://mailgun.net" rel="nofollow">Mailgun email service</a>. They give the following example in their <a href="http://documentation.mailgun.net/user_manual.html#sending-messages" rel="nofollow">API docs using CURL</a> and I need to figure out how to do the same thing in Node.js (preferably using the Request library).</p>
  
  <pre><code>curl -s -k --user api:key-3ax6xnjp29jd6fds4gc373sgvjxteol0 \
      https://api.mailgun.net/v2/samples.mailgun.org/messages \
      -F from='Excited User &lt;me@samples.mailgun.org&gt;' \
      -F to='obukhov.sergey.nickolayevich@yandex.ru' \
      -F cc='sergeyo@profista.com' \
      -F bcc='serobnic@mail.ru' \
      -F subject='Hello' \
      -F text='Testing some Mailgun awesomness!' \
      -F html='\&lt;html\&gt;HTML version of the body\&lt;\html&gt;' \
      -F attachment=@files/cartman.jpg \
      -F attachment=@files/cartman.png
  </code></pre>
  
  <p>My current code (Coffescript) looks like the following:</p>
  
  <pre><code>r = request(
    url: mailgun_uri
    method: 'POST'
    headers:
      'content-type': 'application/x-www-form-urlencoded'
    body: email
    (error, response, body) -&gt;
      console.log response.statusCode
      console.log body
  )
  form = r.form()
  for attachment in attachments
    form.append('attachment', fs.createReadStream(attachment.path))
  </code></pre>