如何制作通过Mandrill on Rails向我发送邮件的联系表单

时间:2015-03-12 05:04:05

标签: ruby-on-rails email contact-form mandrill

我有联系表格

<div align="center">
  <h3>Send A message to Us</h3>

  <%= simple_form_for @contact, :html => {:class => 'form-horizontal' } do |f| %>
    <%= f.input :name, :required => true %>
    <%= f.input :email, :required => true %>
    <%= f.input :message, :as => :text, :required => true %>
    <div class= "hidden">
      <%= f.input :nickname, :hint => 'Leave this field blank!' %>
    </div>
    <div>
      </br>
      <%= f.button :submit, 'Send message', :class=> "btn btn-primary" %>
    </div>
  <% end %>
</div>

连接到控制器

class ContactsController < ApplicationController
  def new
    @contact = Contact.new
  end

  def create
    @contact = Contact.new(params[:contact])
    @contact.request = request
    if @contact.deliver
      flash.now[:notice] = 'Thank you for your message. We will contact you soon!'
    else
      flash.now[:error] = 'Cannot send message.'
      render :new
    end
  end
end

和模型

class Contact < MailForm::Base
  #include MailForm::Delivery

  attribute :name,      :validate => true
  attribute :email,     :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
  attribute :message
  attribute :nickname,  :captcha  => true

  # Declare the e-mail headers. It accepts anything the mail method
  # in ActionMailer accepts.
  def headers
    {
      :subject => "Message from visitor at mydomain.com",
      :to => "charlie@mydomain.com",
      :from => %("#{name}" <#{email}>)
    }
  end
end

应该从联系表单发送邮件给我。这是我的生产和开发.rb(它们是相同的):

config.action_mailer.smtp_settings = {
    :address   => "smtp.mandrillapp.com",
    :port      => 25, # ports 587 and 2525 are also supported with STARTTLS
    :enable_starttls_auto => true, # detects and uses STARTTLS
    :user_name => "myusername",
    :password  => "mypassword", # SMTP password is any valid API key
    :authentication => 'login', # Mandrill supports 'plain' or 'login'
    :domain => 'mydomain.com', # your domain to identify your server when connecting
  }

当然,除了我在适当的地方有我的真实凭据和域名。当我尝试通过我的localhost:3000 / contacts表单创建联系人时,我收到此错误: ContactsController中的Net :: SMTPAuthenticationError #create 535-5.7.8不接受用户名和密码。了解更多信息 第9行@ contact.deliver

我已经用Google搜索了但我无法弄明白。我已经按照here的指示和其他各种我无法记住的地方。如何修复联系表格?提前谢谢!

1 个答案:

答案 0 :(得分:0)

authentication不应该是字符串。试试这个:

config.action_mailer.smtp_settings = {
    :address        => "smtp.mandrillapp.com",
    :port           => "587",
    :authentication => :plain,
    :user_name      => "myusername",
    :password       => "mypassword",
    :enable_starttls_auto => true
  }