【问题标题】:receiving email to my gmail account using actionmailer使用 actionmailer 接收电子邮件到我的 gmail 帐户
【发布时间】:2014-12-18 15:44:08
【问题描述】:

请原谅新手的问题。我正在构建一个包含与我们联系页面的表单,用户应该能够在其中填写表单(user_full_name、user_email、主题、消息),提交时应该作为电子邮件发送到我的 gmail 地址。当我尝试访问带有表单的视图时,我目前收到以下错误。

# 的未定义方法 `feedbacks_path'

下面是我的代码,有谁知道我在这里做错了什么?如果可能的话,我宁愿更改路线也不愿在表单中添加 url。此外,希望在路由中使用“匹配”以使 url 路径为“localhost/contact”而不是“localhost/feedback”。有人有什么想法吗?

routes.rb

Sample::Application.routes.draw do

  resources :feedback, only: [:new, :create]

  root to: "home#index"
  match '/about',   to: 'static_pages#about',   via: 'get'
  match '/help',    to: 'static_pages#help',    via: 'get'
  match '/privacy_policy', to: 'static_pages#privacy_policy', via: 'get' 
  match '/terms_and_conditions',   to: 'static_pages#terms_and_conditions',   via: 'get'
end

Feedback_Controller.rb

class FeedbackController < ApplicationController

  def new
    @feedback = Feedback.new
  end

  def create
    @feedback = Feedback.new(params[:feedback])

    if @feedback.valid?
      FeedbackMailer.new_feedback(@feedback).deliver
      flash[:success] = "We have receieved your message!"
      redirect_to users_path
    else
      flash.now.alert = "Please fill in all fields."
      render :new
    end
  end
end

反馈.rb

class Feedback < ActiveRecord::Base
  def self.columns() @columns ||= []; end

  def self.column(name, sql_type = nil, default = nil, null = true)
    columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
  end

  column :user_full_name, :string
  column :user_email, :string
  column :subject, :text
  column :body, :text

  belongs_to :user
end

查看/feedback.new.html.erb

<div class="feedback">
  <div class="container">
    <%= form_for @feedback do |f| %>

      <%= f.hidden_field :user_full_name %>
      <%= f.hidden_field :user_email %>

      <%= f.label :subject %>
      <%= f.text_field :subject %>

      <%= f.label :feedback %>
      <%= f.text_area :body %>

      <%= f.submit "Send", class: "btn btn-inverse" %>
    <% end %>
  </div>
</div>

feedback_mailer

<!DOCTYPE html>
<html>
  <head>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
  </head>
  <body>
    Name: <%= @feedback.user_full_name %>

    Email: <%= @feedback.user_email %>

    Subject: <%= @feedback.subject %>

    Comments/Question: <%= @feedback.body %>
  </body>
</html>

feedback_mailer.rb

class FeedbackMailer < ActionMailer::Base
  default to:   "railstraining09@gmail.com"

  def new_feedback(feedback)
    @feedback = feedback
    mail(from: @user.email, subject: 'feedback.subject')
  end

end

【问题讨论】:

  • 通过Railscasts 进行介绍。
  • 您需要一个消息表单(模型+视图+控制器)来捕获用户的消息,并在创建后使用某种后台作业队列发送电子邮件。
  • 我建议您在这里阅读官方文档/指南:guides.rubyonrails.org/action_mailer_basics.html 它会指导您完成整个过程,逐步进行设置。

标签: ruby-on-rails actionmailer


【解决方案1】:

如果你在开发中,你可以添加 app/config/environments/development.rb

   config.action_mailer.default_url_options = { :host => 'localhost:3000' }
  # Gmail SMTP server setup
  ActionMailer::Base.smtp_settings = {
  :address => "smtp.gmail.com",
  :enable_starttls_auto => true,
  :port => 587,
  :authentication => :plain,
  :user_name => ENV['GMAIL_USERNAME'],
  :password => ENV['GMAIL_PASSWORD']
  }

试一试。 您可以在 config/environments/ 中创建一个文件 mail.rb,或者如果您想为每个环境使用特定的电子邮件帐户,而不是您需要在每个“环境”文件中写入(production.rb;test.rb,development.rb)。 rb) 你的 ActionMailer::Base.smtp_settings { options .... }

附:我建议你改用 mandrill/sendgrid 等 video

【讨论】:

    猜你喜欢
    • 2018-02-03
    • 2012-03-30
    • 1970-01-01
    • 2023-03-08
    • 2012-09-19
    • 2021-09-01
    • 2017-04-12
    • 1970-01-01
    • 2016-04-20
    相关资源
    最近更新 更多