【问题标题】:Contact form in Ruby on Rails mailer not workingRuby on Rails 邮件程序中的联系表单不起作用
【发布时间】:2015-05-05 21:10:12
【问题描述】:

我正在制作简单的联系表格,没有任何花哨的宝石,我想我已经完成了,除了在生产中没有任何效果。

我在Cloud9 IDE 中使用rails 4.2.0。对于生产,我使用Heroku,对于邮件程序Mailgun,Heroku 中启用了服务女巫。

当我尝试在开发环境中发送邮件时,我看到电子邮件正在服务器控制台中发送,但是当我尝试在生产环境中发送邮件时,它不会发送电子邮件,也不会将我重定向回联系表单页面(就像在开发环境中一样)。也许我只是没有正确使用Mailgun,如果是这样,您能否为邮件程序提供一些好的gmail 教程,以便我可以使其在Heroku 和Digital Ocean 托管平台上运行。

messages_controller.rb

class MessagesController < ApplicationController 

  def new
    @message = Message.new
  end

  def create
    @message = Message.new(message_params)

    if @message.valid?
      MessageMailer.message_me(@message).deliver_now
      redirect_to new_message_path, notice: "Thankyou for your message."
    else
      render :new
    end
  end

  private
    def message_params
      params.require(:message).permit(:name, :email, :subject, :content)
    end
end

message_mailer.rb

class MessageMailer < ApplicationMailer
  # use your own email address here
  default :to => "MYMAIL@gmail.com"

  def message_me(msg)
  @msg = msg

    mail(from: @msg.email, subject: @msg.subject, body: @msg.content)
  end
end

config/environments/production.rb

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    :port           => ENV['587'],
    :address        => ENV['smtp.mailgun.org'],
    :user_name      => ENV['SANDBOX USERNAME GIVEN BY MAILGUN'],
    :password       => ENV['PASWORD'],
    :domain         => 'MYAPP.herokuapp.com', #eg: 'yourappname.herokuapp.com'
    :authentication => :plain,
  }

models/message.rb

class Message
  include ActiveModel::Model
  attr_accessor :name, :email, :subject, :content

  validates :name, :email, :subject, :content, presence: true
end

视图/消息/new.html.erb

<%= form_for @message do |f| %>
  <% if @message.errors.any? %>
    <div id="error_explanation">
      <h2><%= "#{pluralize(@message.errors.count, "error")} prohibited this message from being sent:" %></h2>
      <ul>
        <% @message.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
      <ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %>
    <%= f.text_field :name %>
  </div>

  <div class="field">
    <%= f.label :email %>
    <%= f.email_field :email %>
  </div>

  <div class="field">
    <%= f.label :subject %>
    <%= f.text_field :subject %>
  </div>

  <div class="field">
    <%= f.label :content %>
    <%= f.text_area :content %>
  </div>

  <div class="actions">
    <%= f.submit 'Send', class: 'button' %>
  </div>
<% end %>

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 model-view-controller heroku mailgun


    【解决方案1】:

    SMTP 配置看起来错误。它应该从环境变量中加载配置值,但您似乎正试图将这些值放在那里:

    config.action_mailer.smtp_settings = {
      :port           => ENV['587'],
      :address        => ENV['smtp.mailgun.org']
      :user_name      => ENV['SANDBOX USERNAME GIVEN BY MAILGUN'],
      :password       => ENV['PASWORD'],
      :domain         => 'MYAPP.herokuapp.com', #eg: 'yourappname.herokuapp.com'
      :authentication => :plain,
    }
    

    这实际上应该像这样设置:

    config.action_mailer.smtp_settings = {
      :port           => 587,
      :address        => 'smtp.mailgun.org'
      :user_name      => ENV['MAILGUN_USERNAME'],
      :password       => ENV['MAILGUN_PASSWORD'],
      :domain         => 'MYAPP.herokuapp.com', #eg: 'yourappname.herokuapp.com'
      :authentication => :plain,
    }
    

    然后您需要在 Cloud 9 运行面板中设置这些 MAILGUN_USERNAME 和 MAILGUN_PASSWORD 环境变量。

    【讨论】:

    • 如何在 Cloud 9 和 Heroku 中设置这些变量?
    • Cloud 9 面板上有一个 ENV 按钮,您可以在其中设置它们。对于 Heroku,请使用 heroku config:set MAILGUN_USERNAME=myvalue
    • 天啊。最后一切正常。如果我可以放弃投票,我会这样做:D,因为我的声誉。我将标记为已回答。非常感谢。
    • 太棒了,很高兴我能帮上忙!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-13
    • 1970-01-01
    • 2014-04-06
    • 1970-01-01
    • 1970-01-01
    • 2015-06-07
    相关资源
    最近更新 更多