在rails 0_o中设置邮件程序

时间:2016-04-14 03:27:51

标签: ruby-on-rails ruby-on-rails-4 gmail actionmailer mailer

我真的很挣扎。有谁能看到这里有什么问题?我似乎无法让邮件工作......我正在尝试在我的网站上建立一个联系表单,以便用户可以从网站内向我发送电子邮件。我创建了一个名称,电子邮件,主题和正文的“联系人”,然后将其作为电子邮件发送。

控制器/ contacts_controller.rb

class ContactsController < ApplicationController
  def index
  end

  def new
    @contact = Contact.new
  end

  def create
    @contact = Contact.new(contact_params)
    respond_to do |format|
      if @contact.save
        UserMailer.mail_jill(@contact).deliver

        format.html { redirect_to(@contact, :notice => 'User was successfully created.') }
      else
        format.html { render :action => "new" }
        format.json { render :json => @contact.errors, :status => :unprocessable_entity }
      end
    end
  end


  def contact_params
    params.require(:contact).permit(:name, :email, :subject, :body)
  end
end

寄件人/ user_mailer.rb

class UserMailer < ActionMailer::Base
  def mail_jill(message)
    @name = message.name
    @email = message.email
    @subject = message.subject
    @body = message.body

    mail(to: '<my_email_address>', subject: @subject, body: @body, from: @email)
  end
end

视图/ user_mailer文件/ mail_jill.html.erb:

<h1>From, <%= @contact.name %></h1>
<p>
@contact.body
</p>
<p>-<%= @contact.email %></p>

$ RAILS_ENV.rb:

ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.sendmail_settings = {
  :address              => "smtp.gmail.com",
  :port                 => "465",
  :domain               => "gmail.com",
  :user_name            => "<my_gmail_address>",
  :password             => "<my_gmail_password>",
  :authentication       => "plain",
  :enable_starttls_auto => true
}

配置/环境/

development.rb:

  # mailer
  config.active_support.deprecation = :log
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.perform_deliveries = true
  config.action_mailer.default_url_options = { :host => "localhost:3000" }
  config.action_mailer.delivery_method = :smtp

  config.action_mailer.smtp_settings = {
   :address              => ENV['address'],
   :port                 => ENV['port'],
   :user_name            => ENV['gmail_username'],
   :password             => ENV['gmail_password'],
   :authentication       => ENV['authentication'],
   :enable_starttls_auto => true

从实际站点提交电子邮件后从命令行输出完整错误:

2016-04-15T23:40:12.932560+00:00 app[web.1]: Sent mail to jho3292@gmail.com (348.5ms)
2016-04-15T23:40:12.932577+00:00 app[web.1]: Date: Fri, 15 Apr 2016 23:40:12 +0000
2016-04-15T23:40:12.932579+00:00 app[web.1]: From: jho3292@gmail.com
2016-04-15T23:40:12.932579+00:00 app[web.1]: To: jho3292@gmail.com
2016-04-15T23:40:12.932581+00:00 app[web.1]: Message-ID: <57117bdc8f495_33f9c165af25c8233d@effd0ada-665b-4e24-ba0a-1968e9127267.mail>
2016-04-15T23:40:12.932583+00:00 app[web.1]: Subject: hi
2016-04-15T23:40:12.932583+00:00 app[web.1]: Mime-Version: 1.0
2016-04-15T23:40:12.932584+00:00 app[web.1]: Content-Type: text/plain;
2016-04-15T23:40:12.932585+00:00 app[web.1]:  charset=UTF-8
2016-04-15T23:40:12.932585+00:00 app[web.1]: Content-Transfer-Encoding: 7bit
2016-04-15T23:40:12.932586+00:00 app[web.1]: 
2016-04-15T23:40:12.932587+00:00 app[web.1]: hiiiii
2016-04-15T23:40:12.932687+00:00 app[web.1]: Completed 500 Internal Server Error in 367ms
2016-04-15T23:40:12.933562+00:00 app[web.1]: 
2016-04-15T23:40:12.933579+00:00 app[web.1]: Net::SMTPAuthenticationError (530-5.5.1 Authentication Required. Learn more at
2016-04-15T23:40:12.933580+00:00 app[web.1]: ):
2016-04-15T23:40:12.933581+00:00 app[web.1]:   app/controllers/contacts_controller.rb:12:in `create'
2016-04-15T23:40:12.933582+00:00 app[web.1]: 
2016-04-15T23:40:12.933583+00:00 app[web.1]: 

2 个答案:

答案 0 :(得分:2)

试试这个

config.action_mailer.smtp_settings = {
  address:              'smtp.gmail.com',
  port:                 587,
  domain:               'gmail.com',
  user_name:            ENV['gmail_username'],
  password:             ENV['gmail_password'],
  authentication:       'plain',
  enable_starttls_auto: true
}

答案 1 :(得分:1)

让它发挥作用。谢谢你志良给你的答案,让我更接近。最后,我的$ RAILS_ENV文件出错了。我从环境文件夹中删除了它,并将smtp_settings.rb文件添加到初始化文件夹。以下是initializers / smtp_settings.rb的内容:

ActionMailer::Base.smtp_settings = {
    :address => "smtp.gmail.com",
    :port => 587,
    :domain => "gmail.com",
    :user_name => "<my_email_address>",
    :password => "<my_gmail_password>",
    :authentication => :plain,
    :enable_starttls_auto => true
}