{"_id":"581915998936870f0022efdd","__v":0,"githubsync":"","parentDoc":null,"category":{"_id":"5819154bf62fee0f00949855","project":"5589ceae9883a40d00c433f3","version":"5589ceae9883a40d00c433f6","__v":0,"sync":{"url":"","isSync":false},"reference":true,"createdAt":"2016-11-01T22:20:59.634Z","from_sync":false,"order":4,"slug":"sending-emails","title":"Sending emails"},"project":"5589ceae9883a40d00c433f3","user":"5542d87d795b590d001dc7ff","version":{"_id":"5589ceae9883a40d00c433f6","__v":8,"project":"5589ceae9883a40d00c433f3","createdAt":"2015-06-23T21:25:02.865Z","releaseDate":"2015-06-23T21:25:02.865Z","categories":["5589ceaf9883a40d00c433f7","559ab19d2100d117005f1269","57d4a754899ab90e00105e5d","5807813b6d24211900953b99","5819154bf62fee0f00949855","5841d27cae05ac2500ba2680","5846c4ee5d064323007b1774","589e29c72793e937001c15c5"],"is_deprecated":false,"is_hidden":false,"is_beta":true,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-11-01T22:22:17.236Z","link_external":false,"link_url":"","sync_unique":"","hidden":false,"api":{"settings":"","results":{"codes":[]},"auth":"required","params":[],"url":""},"isReference":true,"order":1,"body":"In this quickstart you'll create an endpoint on your web server at `<yourdomain>.com/email_webhook`. When you're finished UserKit will be able to make requests to your server at that endpoint whenever it needs to have an email sent out.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"1. Create a webhook endpoint on your server\"\n}\n[/block]\nOn your server, create an endpoint for the following url: `/email_webhook`. Whenever UserKit needs to send an email it will make a POST request to that endpoint containing the following JSON in the request body:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"{\\n\\t\\\"email_key\\\": \\\"eml_MgXuRwRr0hIX-JSHgzKetRJGOv5qB5\\\"\\n}\",\n      \"language\": \"json\",\n      \"name\": null\n    }\n  ]\n}\n[/block]\nIn this example we'll be using [mailgun](https://www.mailgun.com) to send the email. Here's a function which handles requests to the `/email_webhook` endpoint. It get's the email data using the `email_key` UserKit sends in the request body:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"import json\\nimport requests\\nimport userkit\\n\\nuk = userkit.UserKit(\\\"<YOUR_APP_SECRET_KEY>\\\")\\n\\n\\n# This function handles requests to /email_webhook\\ndef email_handler(request, response):\\n  json_body = json.loads(request.body)\\n  email_key = json_body['email_key']\\n  \\n  # Fetch the actual email data UserKit wants to have sent\\n  email = uk.emails.get_pending_email(email_key)\\n  if not email:\\n    # Something is wrong with the email (it may have been sent\\n    # already, or this may be a malicious request). Abort.\\n    response.set_status(400)\\n    return\\n\\n  # Send the email\\n  resp = send_email(email)\\n  \\n  if resp.status_code == 200:\\n    # Notify UserKit that everything went well\\n    response.set_status(200)\\n  else:\\n    # Something went wrong\\n    response.set_status(500)\\n\\n    \\ndef send_email(email):\\n  # Send the email using MailGun. Remember to replace\\n  # <YOUR_EMAIL_DOMAIN_NAME>, <YOUR_MAILGUN_API_KEY> and\\n  # <YOUR_EMAIL:::at:::YOUR_DOMAIN.COM>\\n  return requests.post(\\n    'https://api.mailgun.net/v3/<YOUR_EMAIL_DOMAIN_NAME>/messages',\\n    auth=('api', '<YOUR_MAILGUN_API_KEY>'),\\n    data={'from': '<YOUR_EMAIL@YOUR_DOMAIN.COM>,\\n      \\t\\t'to': email.to,\\n      \\t\\t'subject': email.subject,\\n      \\t\\t'text': email.body})\",\n      \"language\": \"python\"\n    },\n    {\n      \"code\": \"<?php\\n\\n$uk = new UserKit('<YOUR_APP_SECRET_KEY>');\\n\\n// Portions of following snippet are pseudo code\\n// This function handles requests to /email_webhook\\nfunction email_handler($request, $response)\\n{\\n  global $uk;\\n\\n  // convert the body response to a json array type \\n  $json_body = json_decode($response->body, true);\\n\\n  $email_key = $json_body['email_key'];\\n  \\n  // Fetch the actual email data UserKit wants to have sent\\n  $email = $uk->emails->getPendingEmail($email_key);\\n  if (!$email)\\n  {\\n    // Something is wrong with the email (it may have been sent\\n    // already, or this may be a malicious request). Abort.\\n    $response->set_status(400);\\n    return;\\n  }\\n  \\n  // Send the email\\n  $resp = send_email($email);\\n  \\n  if($resp->status_code == 200)\\n  {\\n    // Notify UserKit that everything went well\\n    $response->set_status(200);\\n  }\\n  else\\n  {\\n    // Something went wrong\\n    $response->set_status(500);\\n  }\\n}\\n\\nfunction send_mail($email)\\n{\\n  // Send the email using MailGun. Remember to replace\\n  // <YOUR_EMAIL_DOMAIN_NAME>, <YOUR_MAILGUN_API_KEY> and\\n  // <YOUR_EMAIL@YOUR_DOMAIN.COM>\\n\\n  $ch = curl_init();\\n  curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);\\n  curl_setopt($ch, CURLOPT_USERPWD, 'api:' . <YOUR_MAILGUN_API_KEY>);\\n  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);\\n  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');\\n  curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v2/' . <YOUR_EMAIL_DOMAIN_NAME> .'/messages');\\n  curl_setopt($ch, CURLOPT_POSTFIELDS, array(\\n    'from' => 'Open ' . '<YOUR_EMAIL@YOUR_DOMAIN.COM>',\\n    'to' => $email,\\n    'subject' => $email->subject,\\n    'html' => $email->body\\n  ));\\n\\n  $result = curl_exec($ch);\\n  curl_close($ch);\\n  return $result;\\n}\\n\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"2. Tell UserKit where your email endpoint is\"\n}\n[/block]\nIn the UserKit [dashboard](https://dashboard.userkit.io) select your app, then go to *settings* and in *Email webhook* enter the full URL to the endpoint you created in step 1. For example, https://<yourdomain>.com/email_webhook.","excerpt":"How to have UserKit send emails from your own domain","slug":"quickstart-sending-emails-from-custom-domain","type":"basic","title":"Quickstart - Sending emails from custom domain"}

Quickstart - Sending emails from custom domain

How to have UserKit send emails from your own domain

In this quickstart you'll create an endpoint on your web server at `<yourdomain>.com/email_webhook`. When you're finished UserKit will be able to make requests to your server at that endpoint whenever it needs to have an email sent out. [block:api-header] { "type": "basic", "title": "1. Create a webhook endpoint on your server" } [/block] On your server, create an endpoint for the following url: `/email_webhook`. Whenever UserKit needs to send an email it will make a POST request to that endpoint containing the following JSON in the request body: [block:code] { "codes": [ { "code": "{\n\t\"email_key\": \"eml_MgXuRwRr0hIX-JSHgzKetRJGOv5qB5\"\n}", "language": "json", "name": null } ] } [/block] In this example we'll be using [mailgun](https://www.mailgun.com) to send the email. Here's a function which handles requests to the `/email_webhook` endpoint. It get's the email data using the `email_key` UserKit sends in the request body: [block:code] { "codes": [ { "code": "import json\nimport requests\nimport userkit\n\nuk = userkit.UserKit(\"<YOUR_APP_SECRET_KEY>\")\n\n\n# This function handles requests to /email_webhook\ndef email_handler(request, response):\n json_body = json.loads(request.body)\n email_key = json_body['email_key']\n \n # Fetch the actual email data UserKit wants to have sent\n email = uk.emails.get_pending_email(email_key)\n if not email:\n # Something is wrong with the email (it may have been sent\n # already, or this may be a malicious request). Abort.\n response.set_status(400)\n return\n\n # Send the email\n resp = send_email(email)\n \n if resp.status_code == 200:\n # Notify UserKit that everything went well\n response.set_status(200)\n else:\n # Something went wrong\n response.set_status(500)\n\n \ndef send_email(email):\n # Send the email using MailGun. Remember to replace\n # <YOUR_EMAIL_DOMAIN_NAME>, <YOUR_MAILGUN_API_KEY> and\n # <YOUR_EMAIL@YOUR_DOMAIN.COM>\n return requests.post(\n 'https://api.mailgun.net/v3/<YOUR_EMAIL_DOMAIN_NAME>/messages',\n auth=('api', '<YOUR_MAILGUN_API_KEY>'),\n data={'from': '<YOUR_EMAIL@YOUR_DOMAIN.COM>,\n \t\t'to': email.to,\n \t\t'subject': email.subject,\n \t\t'text': email.body})", "language": "python" }, { "code": "<?php\n\n$uk = new UserKit('<YOUR_APP_SECRET_KEY>');\n\n// Portions of following snippet are pseudo code\n// This function handles requests to /email_webhook\nfunction email_handler($request, $response)\n{\n global $uk;\n\n // convert the body response to a json array type \n $json_body = json_decode($response->body, true);\n\n $email_key = $json_body['email_key'];\n \n // Fetch the actual email data UserKit wants to have sent\n $email = $uk->emails->getPendingEmail($email_key);\n if (!$email)\n {\n // Something is wrong with the email (it may have been sent\n // already, or this may be a malicious request). Abort.\n $response->set_status(400);\n return;\n }\n \n // Send the email\n $resp = send_email($email);\n \n if($resp->status_code == 200)\n {\n // Notify UserKit that everything went well\n $response->set_status(200);\n }\n else\n {\n // Something went wrong\n $response->set_status(500);\n }\n}\n\nfunction send_mail($email)\n{\n // Send the email using MailGun. Remember to replace\n // <YOUR_EMAIL_DOMAIN_NAME>, <YOUR_MAILGUN_API_KEY> and\n // <YOUR_EMAIL@YOUR_DOMAIN.COM>\n\n $ch = curl_init();\n curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);\n curl_setopt($ch, CURLOPT_USERPWD, 'api:' . <YOUR_MAILGUN_API_KEY>);\n curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);\n curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');\n curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v2/' . <YOUR_EMAIL_DOMAIN_NAME> .'/messages');\n curl_setopt($ch, CURLOPT_POSTFIELDS, array(\n 'from' => 'Open ' . '<YOUR_EMAIL@YOUR_DOMAIN.COM>',\n 'to' => $email,\n 'subject' => $email->subject,\n 'html' => $email->body\n ));\n\n $result = curl_exec($ch);\n curl_close($ch);\n return $result;\n}\n", "language": "php" } ] } [/block] [block:api-header] { "type": "basic", "title": "2. Tell UserKit where your email endpoint is" } [/block] In the UserKit [dashboard](https://dashboard.userkit.io) select your app, then go to *settings* and in *Email webhook* enter the full URL to the endpoint you created in step 1. For example, https://<yourdomain>.com/email_webhook.