I'm having a hard time finding a solution (almost 3 days) my code in incoming_controller.rb seems to be right, I tested it in the rails console and appears that the only problem is that when I send an email (from my gmail account) to my mailgun email, the routes I declare won't connect with my rails app, and I get warnings in my mailgun logs.
What I'm trying to do is to allow users to send an email and convert the body content into a bookmark, and save it in the database, and the subject of the email as a topic.
Here is my routes.rb file:
Rails.application.routes.draw do
devise_for :users
get 'welcome/index'
get 'welcome/about'
root to: 'welcome#index'
post :incoming, to: 'incoming#create'
end
My incoming_controller.rb file:
class IncomingController < ApplicationController
skip_before_action :verify_authenticity_token, only: [:create]
def create
@user = User.find_by(email: params[:sender])
@topic = Topic.find_by(title: params[:subject])
@url = params["body-plain"]
if @user.nil?
@user = User.new(email: params[:sender], password: "temp0rary_passw0rd")
@user.skip_confirmation!
@user.save!
end
if @topic.nil?
@topic = @user.topics.create(title: params[:subject])
end
@bookmark = @topic.bookmarks.create(url: @url)
head 200
end
end
Topic belongs to User and has many bookmarks, User has many topics, Bookmark belongs to Topic.
Also, here's my mail.rb file:
ActionMailer::Base.smtp_settings = {
port: 587,
address: 'smtp.mailgun.org',
user_name: ENV['MAILGUN_SMTP_LOGIN'],
password: ENV['MAILGUN_SMTP_PASSWORD'],
domain: 'appfc266c436eb04ebaae05a3f3f8ad7e49.mailgun.org',
authentication: :plain,
content_type: 'text/html'
}
ActionMailer::Base.delivery_method = :smtp
# Makes debugging *way* easier.
ActionMailer::Base.raise_delivery_errors = true
Note: mailgun works well with sending email confirmation instructions from Devise to the users, so it is configured correctly, what I can't do is to make mailgun to receive emails and store them in my rails db via parameters with the incoming_controller.
What am I doing wrong?
My mailgun route is as follows:
Filter Expression: catch_all()
Actions: forward(" http://bookmark-this.herokuapp.com/incoming/ ")
Here's the warning logs I get in mailgun when I send an email:
Here's the project repository on github: https://github.com/bntzio/bookmark-this
Many thanks!
Mailgun is receiving a 301
status code, being redirected to the https
endpoint instead to the plain one. It seems that you have activated SSL but did not update the full route on the mailgun config to use it. You should update it to read like:
Actions: forward("https://bookmark-this.herokuapp.com/incoming/")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.