title: Swift Send Email with MailGun
body: <p><strong>Problem</strong></p>
  
  <p>I would like to use the <a href="https://documentation.mailgun.com/quickstart-sending.html#how-to-start-sending-email" rel="nofollow noreferrer">MailGun</a> service to send emails from a pure Swift app.</p>
  
  <p><strong>Research So Far</strong></p>
  
  <p>As I understand it, <a href="https://documentation.mailgun.com/quickstart-sending.html#send-via-smtp" rel="nofollow noreferrer">there are two methods to send an email</a> via MailGun. One is to email MailGun with the emails, and MailGun will redirect it (See <em>Send via SMTP</em>). That will, as I understand it, not work, as iOS cannot programatically automatically send mail, and must use <a href="http://www.ioscreator.com/tutorials/send-email-tutorial-ios8-swift" rel="nofollow noreferrer">methods that require user intervention</a>. As such, I should use the API directly. As I understand it, I need to open a URL to do this, and so I should use some form of <code>NSURLSession</code>, as per <a href="https://stackoverflow.com/questions/24016142/how-to-make-an-http-request-in-swift">this SO answer</a></p>
  
  <p><strong>Code</strong></p>
  
  <p>MailGun provides documentation for Python, which is as follows:</p>
  
  <pre><code>def send_simple_message():
  return requests.post(
      "https://api.mailgun.net/v3/sandbox(Personal info).mailgun.org/messages",
      auth=("api", "key-(Personal info)"),
      data={"from": "Excited User &lt;(Personal info)&gt;",
            "to": ["bar@example.com", "(Personal info)"],
            "subject": "Hello",
            "text": "Testing some Mailgun awesomness!"})
  </code></pre>
  
  <p>with (Personal info) being substituted for keys/information/emails.</p>
  
  <p><strong>Question</strong></p>
  
  <p>How do I do that in Swift?</p>
  
  <p>Thanks!</p>