0

我正在尝试使用 mailform gem 在我的 rails 5 应用程序中建立联系我们页面。我跟着这个 youtube 视频解释了这个过程:https ://www.youtube.com/watch?v=QIoORYeBdhs

但是,我无法让电子邮件在开发或生产模式下实际交付,我也不确定为什么。任何帮助将非常感激!

这是我所拥有的:

欢迎.rb:

class Welcome < MailForm::Base
    attribute :name, :validate => true
    attribute :email, :validate => /\A([\w+\-].?)+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i
    attribute :message, :validate => true
    attribute :nickname, :captcha => true

    def headers 
        {
            :subject => "Contact from About Site",
            :to => "my_email@gmail.com", 
            :from => %("#{name}" <#{email}>)
        }
    end
end

欢迎控制器:

class WelcomeController < ApplicationController
  def index
    @welcome = Welcome.new
  end

  def create
    @welcome = Welcome.new(params[:welcome])
    @welcome.request = request
    if @welcome.deliver
        flash.now[:error] = nil
    else
        flash.now[:error] = 'Cannot send message.'
        render :index
    end
  end
end

表单本身:(index.html.erb)

<section id="contact">
    <div class="container">
        <%= form_for welcome_index_path do |f| %>
            <%= f.label :name %>
            <%= f.text_field :name, required: true %>
            <br>
            <%= f.label :email %>
            <%= f.email_field :email, required: true %>
            <br>
            <%= f.label :message %>
            <%= f.text_area :message, as: :text %>
            <div class="hidden">
                <%= f.label :nickname %>
                <%= f.text_field :nickname, hint: "Leave this field blank" %>
            </div>
            <br>
            <%= f.submit 'Send Message', class: "btn btn-primary btn-large" %>
        <% end %>
    </div>
</section>

现在我的配置文件:

production.rb,将heroku与sendgrid一起使用:

# email
config.action_mailer.default_url_options = {host: 'https://my_heroku_app.herokuapp.com'}
  config.action_mailer.delivery_method = :smtp
  ActionMailer::Base.smtp_settings = {
    :address => 'smtp.sendgrid.net',
    :port => '587',
    :authentication => :plain, 
    :user_name => ENV["SENDGRID_USERNAME"],
    :password => ENV["SENDGRID_PASSWORD"],
    :domain => 'heroku.com',
    :enable_starttls_auto => true 
  }

development.rb,使用 gem mailcatcher:

config.action_mailer.default_url_options = { :host => 'localhost:3000' }
  config.action_mailer.perform_deliveries = true
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.default :charset => "utf-8"
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {:address => "localhost", :port => 1025}
4

1 回答 1

0

我自己的答案 实际上上面的代码似乎工作得很好,并且 sendgrid 启动只是有一个延迟。花了大约两天时间,我不知道为什么,但现在它工作正常,无需真正修改代码

于 2017-01-19T13:09:20.673 回答