简体   繁体   English

使用Mailgun和Rails处理传入电子邮件

[英]Processing Incoming Email using Mailgun and Rails

I am trying to have incoming email using malign and ruby on rails to take params and parse the correct data out of it to create a bookmark. 我正在尝试使用malign和ruby on rails接收传入的电子邮件以获取参数并从中解析出正确的数据以创建书签。 Mailgun is processing the incoming emails successfully, but rails is not creating the bookmark. Mailgun正在成功处理传入的电子邮件,但是Rails并未创建书签。 Could you please help me find the bug? 您能帮我找到错误吗?

I added a POST in routes.rb 我在routes.rb中添加了一个POST

post :incoming, to: 'incoming#create'

Rails.application.routes.draw do

  post :incoming, to: 'incoming#create'

  resources :topics do
    resources :bookmarks, except: [:index]
  end

  devise_for :users

  get 'about' => 'welcome#about'

  root 'welcome#index'

end

I created the incoming_controller.rb with a create method. 我用创建方法创建了committing_controller.rb。

class IncomingController < ApplicationController
  skip_before_action :verify_authenticity_token, only: [:create]

  def create
     user = User.find(params[:sender])

     topic = Topic.find(params[:subject])

     url = params["body-plain"]

     if @user.nil?
       @user = User.new(email: user, password: "password")
       @user.save!
     end

      if @topic.nil?
        @topic = Topic.new(title: topic)
        @topic.save!
      end

      @bookmark = @topic.bookmarks.build(user: user, url: url)

      @bookmark.save!


    head 200
  end
end

I send a sample email with this info: 我发送带有以下信息的示例电子邮件

to : postmaster@appd5fb3842ce6a4e52a5acb511e854e06b.mailgun.org :postmaster@appd5fb3842ce6a4e52a5acb511e854e06b.mailgun.org

subject : Ruby on Rails 主题 :Ruby on Rails

body : www.amazon.com 正文 :www.amazon.com

Mailgun processes the incoming email successfully. Mailgun成功处理传入的电子邮件。 The app is deployed in heroku and then I check the logs after sending the sample email: 该应用程序已部署在heroku中,然后在发送示例电子邮件后检查日志:

016-07-19T19:34:54.444997+00:00 app[web.1]: Started POST "/incoming" for 173.203.37.61 at 2016-07-19 19:34:54 +0000
2016-07-19T19:34:54.450111+00:00 app[web.1]: Processing by IncomingController#create as */*
2016-07-19T19:34:54.452049+00:00 app[web.1]: Completed 401 Unauthorized in 2ms (ActiveRecord: 0.0ms)
2016-07-19T19:34:54.450405+00:00 app[web.1]:   Parameters: {"recipient"=>"postmaster@appd5fb3842ce6a4e52a5acb511e854e06b.mailgun.org", "sender"=>"davefogo@gmail.com", "subject"=>"Fwd: Ruby on Rails", "from"=>"Davefogo <davefogo@gmail.com>", "X-Mailgun-Incoming"=>"Yes", "X-Envelope-From"=>"<davefogo@gmail.com>", "Received"=>"by 10.31.54.67 with HTTP; Tue, 19 Jul 2016 12:34:33 -0700 (PDT)", "Dkim-Signature"=>"v=1; a=rsa-sha256; c=relaxed/relaxed;        d=gmail.com; s=20120113;        h=mime-version:in-reply-to:references:from:date:message-id:subject:to;        bh=/GqYYnS19tzrvCvJfrBdDl6MnpgkPapnQaEWEQ5bjxo=;        b=IGVDlfbjQxBnPDgDKds2xTU0wZgS80r8/qaH3HxgvbSA/yxrzBrC403Zl9hDldBnw3         ttdGYaW1A333eqMN/O8QRrG9tm0twYonKA1/D3qaWVx6DgnOHc5R1pfxpQuGFU+KlSNb         zwNoU33lSVSIooZuoWkSp2xMoonK7PF0QBUck7/OMeh1acV0pN9FSNKypTRyxXAa3hVW         8j8+6uSnRMH6qfyb9JgRDx65ar6McJ1zPTnUoRry15zEzGVoJbtzIWF8t4s9yJSjqt55         RwMtikD+12NAL39b4Gnq35EsuoNFDB39P/3jrYd+e/LDbvdZ+KpMguNt5hOsxGU6EGSL         4mtA==", "X-Google-Dkim-Signature"=>"v=1; a=rsa-sha256; c=relaxed/relaxed;        d=1e100.net; s=20130820;        h=x-gm-message-state:mime-version:in-reply-to:references:from:date         :message-id:subject:to;        bh=/GqYYnS19tzrvCvJfrBdDl6MnpgkPapnQaEWEQ5bjxo=;        b=JUVYyBwmTVhxne5e+T/avE5Zw8d9BQxqXEohI24RDeJfgn7cHsl8kwebPFHFETL+BL         cMSTR4GE+AlbvogjPgE3oQYpo51mbFSH4tPmOLl3eF72K4E4jYaLRKQEFHNb+OXC+mLJ         w687MluK367IK3GAoK8S+f4AIAuirAFNiUP+jqB5rLC2twjAzu5K4VNFIazharMXMMOP         ts4M4HpzKZ/KvWznbwjmLh5m0Mh6n6mbgmya4nisgbgGq49u68VvceQIv+HQEjo9hMDG         Ujn0z7GAwo9aHR1dLcjgKh9DDR7xPQabBd42JzrS9w03qkKISqo0+Ari5BOqNclwAz/y         cH7g==", "X-Gm-Message-State"=>"ALyK8tKGA4dQBPYYeMhpAaxb2WJMsQanxVpTNLNlmJVonUtlUECi9H9lqwH3smCcwZgC2WEwTOkBLqBQfyZ/hQ==", "X-Received"=>"by 10.31.181.81 with SMTP id e78mr10214430vkf.154.1468956892490; Tue, 19 Jul 2016 12:34:52 -0700 (PDT)", "Mime-Version"=>"1.0", "In-Reply-To"=>"<C179685D-46B8-4120-A9E9-0631DF1F774C@gmail.com>", "References"=>"<C179685D-46B8-4120-A9E9-0631DF1F774C@gmail.com>", "From"=>"David Forero Gomez <davefogo@gmail.com>", "Date"=>"Tue, 19 Jul 2016 14:34:33 -0500", "Message-Id"=>"<CACkRELFAYA_8-EcQxP8yXYuWe+wV-Zyvo8DJ0C5jxA7hF=qG7g@mail.gmail.com>", "Subject"=>"Fwd: Ruby on Rails", "To"=>"postmaster@appd5fb3842ce6a4e52a5acb511e854e06b.mailgun.org", "Content-Type"=>"multipart/alternative; boundary=\"001a114394cec470160538022db5\"", "message-headers"=>"[[\"X-Mailgun-Incoming\", \"Yes\"], [\"X-Envelope-From\", \"<davefogo@gmail.com>\"], [\"Received\", \"from mail-vk0-f50.google.com (mail-vk0-f50.google.com [209.85.213.50]) by mxa.mailgun.org with ESMTP id 578e80dd.7f15044ad6f0-in3; Tue, 19 Jul 2016 19:34:53 -0000 (UTC)\"], [\"Received\", \"by mail-vk0-f50.google.com with SMTP id w127so39074349vkh.2        for <postmaster@appd5fb3842ce6a4e52a5acb511e854e06b.mailgun.org>; Tue, 19 Jul 2016 12:34:53 -0700 (PDT)\"], [\"Dkim-Signature\", \"v=1; a=rsa-sha256; c=relaxed/relaxed;        d=gmail.com; s=20120113;        h=mime-version:in-reply-to:references:from:date:message-id:subject:to;        bh=/GqYYnS19tzrvCvJfrBdDl6MnpgkPapnQaEWEQ5bjxo=;        b=IGVDlfbjQxBnPDgDKds2xTU0wZgS80r8/qaH3HxgvbSA/yxrzBrC403Zl9hDldBnw3         ttdGYaW1A333eqMN/O8QRrG9tm0twYonKA1/D3qaWVx6DgnOHc5R1pfxpQuGFU+KlSNb         zwNoU33lSVSIooZuoWkSp2xMoonK7PF0QBUck7/OMeh1acV0pN9FSNKypTRyxXAa3hVW         8j8+6uSnRMH6qfyb9JgRDx65ar6McJ1zPTnUoRry15zEzGVoJbtzIWF8t4s9yJSjqt55         RwMtikD+12NAL39b4Gnq35EsuoNFDB39P/3jrYd+e/LDbvdZ+KpMguNt5hOsxGU6EGSL         4mtA==\"], [\"X-Google-Dkim-Signature\", \"v=1; a=rsa-sha256; c=relaxed/relaxed;        d=1e100.net; s=20130820;        h=x-gm-message-state:mime-version:in-reply-to:references:from:date         :message-id:subject:to;        bh=/GqYYnS19tzrvCvJfrBdDl6MnpgkPapnQaEWEQ5bjxo=;        b=JUVYyBwmTVhxne5e+T/avE5Zw8d9BQxqXEohI24RDeJfgn7cHsl8kwebPFHFETL+BL         cMSTR4GE+AlbvogjPgE3oQYpo51mbFSH4tPmOLl3eF72K4E4jYaLRKQEFHNb+OXC+mLJ         w687MluK367IK3GAoK8S+f4AIAuirAFNiUP+jqB5rLC2twjAzu5K4VNFIazharMXMMOP         ts4M4HpzKZ/KvWznbwjmLh5m0Mh6n6mbgmya4nisgbgGq49u68VvceQIv+HQEjo9hMDG         Ujn0z7GAwo9aHR1dLcjgKh9DDR7xPQabBd42JzrS9w03qkKISqo0+Ari5BOqNclwAz/y         cH7g==\"], [\"X-Gm-Message-State\", \"ALyK8tKGA4dQBPYYeMhpAaxb2WJMsQanxVpTNLNlmJVonUtlUECi9H9lqwH3smCcwZgC2WEwTOkBLqBQfyZ/hQ==\"], [\"X-Received\", \"by 10.31.181.81 with SMTP id e78mr10214430vkf.154.1468956892490; Tue, 19 Jul 2016 12:34:52 -0700 (PDT)\"], [\"Mime-Version\", \"1.0\"], [\"Received\", \"by 10.31.54.67 with HTTP; Tue, 19 Jul 2016 12:34:33 -0700 (PDT)\"], [\"In-Reply-To\", \"<C179685D-46B8-4120-A9E9-0631DF1F774C@gmail.com>\"], [\"References\", \"<C179685D-46B8-4120-A9E9-0631DF1F774C@gmail.com>\"], [\"From\", \"David Forero Gomez <davefogo@gmail.com>\"], [\"Date\", \"Tue, 19 Jul 2016 14:34:33 -0500\"], [\"Message-Id\", \"<CACkRELFAYA_8-EcQxP8yXYuWe+wV-Zyvo8DJ0C5jxA7hF=qG7g@mail.gmail.com>\"], [\"Subject\", \"Fwd: Ruby on Rails\"], [\"To\", \"postmaster@appd5fb3842ce6a4e52a5acb511e854e06b.mailgun.org\"], [\"Content-Type\", \"multipart/alternative; boundary=\\\"001a114394cec470160538022db5\\\"\"]]", "timestamp"=>"1468956894", "token"=>"0e3d93f6085cc41f8b5e13cc37b5ff6fb6d93b96f00b51808a", "signature"=>"4548f253e920556cd514d0a4342c7e1121a47b28a91f4e890bf5a80449001bc7", "body-plain"=>"http://www.amazon.com\r\n", "body-html"=>"<div dir=\"ltr\"><br><div class=\"gmail_quote\"><br><div style=\"word-wrap:break-word\"><a href=\"http://www.amazon.com\" target=\"_blank\">http://www.amazon.com</a>&nbsp;</div></div><br></div>\r\n", "stripped-html"=>"<html><body><div dir=\"ltr\"><br><br></div></body></html>", "stripped-text"=>"http://www.amazon.com", "stripped-signature"=>""}
2016-07-19T19:34:54.439193+00:00 heroku[router]: at=info method=POST path="/incoming" host=nameless-oasis-20950.herokuapp.com request_id=1c9d12fb-1ae7-4394-b8fc-a0f209c4dbb8 fwd="173.203.37.61" dyno=web.1 connect=1ms service=32ms status=302 bytes=989

After hours of struggling with this bug the answer for this question is quite easy. 经过数小时的时间与该bug的斗争后,这个问题的答案很容易。

Add to incominig_controller.rb : 添加到incominig_controller.rb

skip_before_action :authenticate_user!, only: [:create]

The error was coming up as the user sent the email and the app expected that user to be logged in (obviously shouldn't be like that). 由于用户发送了电子邮件,并且应用程序希望该用户登录,因此出现了错误(显然不应该这样)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM