├── README.md └── api_server.js /README.md: -------------------------------------------------------------------------------- 1 | # Haraka API Server 2 | 3 | Plugin for Haraka - https://github.com/baudehlo/Haraka 4 | 5 | Code pieced together using multiple resources around the web... I'm sorry if i did not honor the use of your function (person who wrote POST variables parser) I forgot the URL's. A big thank you to baudehlo for an amazing smtp server. I cannot wait to see this develop more. 6 | 7 | # Description 8 | 9 | Have you ever wanted to send out emails from your web application but have issues with the server getting clogged up, or having a great hosting provider that throttles your email usage. Well forget PostmarkApp, and their finicky policies, about bulk sending, or the price of it. With Haraka API Server you get instant access to Haraka's Outbound mail class via HTTP access, via the POST server variables. Perfect for building a simple curl request to send emails from your web app. Just set an API Key, and a port to run your server on load it in your Haraka config and your good to go. Because of Haraka's amazing scaleability, and speed this is the perfect solution for low to mid range activity web app servers. There is even a html form available on the http server so you can test it and make sure everything is up and running. 10 | 11 | # Use 12 | 13 | Send data via POST to http://server:port/api the available variables are to, from, subject, body, and of course key (for authorization purposes). 14 | Make sure to set your key, port, and default from address in the top of the plugin file. 15 | 16 | You may also access an HTML form to test the plugin at http://server:port/ 17 | 18 | # Disclaimer 19 | 20 | Be wise don't send emails from accounts you don't own or have the possibility to create. 21 | I will not be held responsible for abuse of this plugin. -------------------------------------------------------------------------------- /api_server.js: -------------------------------------------------------------------------------- 1 | var http = require('http'), 2 | sys = require('sys'), 3 | querystring = require('querystring'); 4 | 5 | // Set listening port 6 | var port = "8079"; 7 | 8 | // Set API Key 9 | var api_key = "XXXXX-XXXXX-XXXXX-XXXXX-XXXXX"; 10 | 11 | // Set Default From Email Address 12 | var from = "noreply@yenn.co"; 13 | 14 | // Create the listening server 15 | http.createServer(function(request, response) 16 | { 17 | sys.puts('Request for ' + request.url); 18 | 19 | switch (request.url) 20 | { 21 | case '/': 22 | response.writeHead(200, { 23 | 'Content-Type': 'text/html' 24 | }); 25 | response.write( 26 | '
You you can use this server by calling http://localhost:' + port + '/api and passing key, ' + 28 | 'to, from (optional), subject, and body as the POST variables.
' + 29 | '' 37 | ); 38 | response.end(); 39 | break; 40 | case '/api': 41 | response.writeHead(200, { 42 | 'Content-Type': 'application/json' 43 | }); 44 | 45 | post_handler(request, api_key, from, 46 | function(post, api_key, from) 47 | { 48 | // Begin send mail 49 | var outbound = require('./outbound'); 50 | 51 | // Grab Parent object 52 | var plugin = this; 53 | 54 | // Allow overriding of from address 55 | if (post.from) var from = post.from; 56 | 57 | var contents = [ 58 | "From: " + from, 59 | "To: " + post.to, 60 | "MIME-Version: 1.0", 61 | "Content-Type: text/html; charset=ISO-8859-1", 62 | "Subject: " + post.subject, 63 | "", 64 | post.body, 65 | ""].join("\n"); 66 | 67 | var outnext = function(code, msg) { 68 | switch (code) { 69 | case DENY: 70 | var output = "Sending mail failed: " + msg; 71 | break; 72 | case OK: 73 | var output = "mail sent"; 74 | break; 75 | default: 76 | var output = "Unrecognised return code from sending email: " + msg; 77 | break; 78 | } 79 | 80 | response.write(output); 81 | response.end(); 82 | }; 83 | 84 | if (post.key === api_key) 85 | outbound.send_email(from, post.to, contents, outnext); 86 | else response.write(sys.inspect("API Key was invalid.")); 87 | 88 | }); 89 | 90 | break; 91 | }; 92 | }).listen(port); 93 | 94 | // Grab post data 95 | function post_handler(request, api_key, from, callback) 96 | { 97 | var _REQUEST = {}; 98 | var _CONTENT = ''; 99 | 100 | if (request.method == 'POST') 101 | { 102 | request.addListener('data', 103 | function(chunk) 104 | { 105 | _CONTENT += chunk; 106 | }); 107 | 108 | request.addListener('end', 109 | function() 110 | { 111 | _REQUEST = querystring.parse(_CONTENT); 112 | callback(_REQUEST, api_key, from); 113 | }); 114 | }; 115 | }; 116 | --------------------------------------------------------------------------------