{"metadata":{"image":[],"title":"","description":""},"api":{"url":"","auth":"required","settings":"","params":[],"results":{"codes":[]}},"next":{"description":"","pages":[]},"title":"Quickstart - Sending emails from custom domain","type":"basic","slug":"quickstart-sending-emails-from-custom-domain","excerpt":"How to have UserKit send emails from your own domain","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': '<[email protected]_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 // <[email protected]_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 ' . '<[email protected]_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.","updates":[],"order":1,"isReference":true,"hidden":false,"sync_unique":"","link_url":"","link_external":false,"_id":"581915998936870f0022efdd","__v":0,"githubsync":"","parentDoc":null,"category":{"sync":{"isSync":false,"url":""},"pages":[],"title":"Sending emails","slug":"sending-emails","order":4,"from_sync":false,"reference":true,"_id":"5819154bf62fee0f00949855","createdAt":"2016-11-01T22:20:59.634Z","project":"5589ceae9883a40d00c433f3","version":"5589ceae9883a40d00c433f6","__v":0},"createdAt":"2016-11-01T22:22:17.236Z","project":"5589ceae9883a40d00c433f3","user":"5542d87d795b590d001dc7ff","version":{"version":"1.0","version_clean":"1.0.0","codename":"","is_stable":true,"is_beta":true,"is_hidden":false,"is_deprecated":false,"categories":["5589ceaf9883a40d00c433f7","559ab19d2100d117005f1269","57d4a754899ab90e00105e5d","5807813b6d24211900953b99","5819154bf62fee0f00949855","5841d27cae05ac2500ba2680","5846c4ee5d064323007b1774","589e29c72793e937001c15c5","5cf0460e272f2c0014a80d17","5cf047bde14258005d7a374e","5db6fa2c7f86fa004ff2c35b","5e349a6a02520b006458b0ae","5e4c89eafd907100654072f2","5e4d7a1feca7f90018b15b9b","5e4eff0ec3399b005118d83a"],"_id":"5589ceae9883a40d00c433f6","releaseDate":"2015-06-23T21:25:02.865Z","__v":15,"createdAt":"2015-06-23T21:25:02.865Z","project":"5589ceae9883a40d00c433f3"}}
Quickstart - Sending emails from custom domain
How to have UserKit send emails from your own domain