How to write your webhook...
Step 1. Create your controller file app/controllers/incoming_emails_controller.rb
and paste the following code inside
require 'mail' class IncomingEmailsController < ActionController::Base def create mail = Mail.new(params[:message]) # TODO: use 'mail' object # see API for https://github.com/mikel/mail render text: "OK" end end
Step 2. Add the following line of code into your config/routes.rb
post 'incoming_emails' => 'incoming_emails#create'
Step 3. Configure url
in your mail2webhook account to point to your server, e.g. http://your.app.com/incoming_emails
You can test sending to your app on your laptop with the following curl command
curl -X POST 'http://127.0.0.1:3000/incoming_emails' -d message='From: sender@example.com To: you@example.com Subject: Hello How are you? '
Step 1. Install the mailparser
npm package
$ npm install mailparser
Step 2. And execute the following Node.js script
var querystring = require('querystring'); var MailParser = require("mailparser").MailParser; var server = require('http').createServer(); server.addListener('request', function(req, res) { var chunks = []; req.on('data', chunks.push.bind(chunks)); req.on('end', function() { var mailparser = new MailParser(); mailparser.on("end", function(mail_object) { // TODO: use 'mail_object' // see API for https://github.com/andris9/mailparser res.writeHead(200, {'content-type': 'text/plain'}); res.end(); }); var params = querystring.parse(chunks.join("").toString()); mailparser.write(params['message']); mailparser.end(); }); }); var port = process.env.PORT || 3000; console.log(' [*] Listening on 0.0.0.0:' + port); server.listen(port, '0.0.0.0');
Step 3. Configure url
in your mail2webhook account to point to your server, e.g. http://your.app.com:3000/
You can test sending to your app on your laptop with the following curl command
curl -X POST 'http://127.0.0.1:3000/incoming_emails' -d message='From: sender@example.com To: you@example.com Subject: Hello How are you? '
Step 1. Create a app.py
python script
import email from flask import Flask, request app = Flask(__name__) # uncomment for helpful stacktrace: # # class DebugConfig(object): # DEBUG = True # TRAP_HTTP_EXCEPTIONS = True # app.config.from_object(DebugConfig) @app.route('/incoming_emails', methods = ['POST']) def incoming_emails(): m = email.message_from_string(request.form['message']) # checkout the email module https://docs.python.org/2/library/email.html # e.g. # print m['Subject'] return "OK"
Step 2. And execute the following command
$ gunicorn app:app
Refer to Flash documentation for more information.
You can test sending to your app on your laptop with the following curl command
curl -X POST 'http://127.0.0.1:8000/incoming_emails' -d message='From: sender@example.com To: you@example.com Subject: Hello How are you? '