【问题标题】:ActionMailer and ActiveModelActionMailer 和 ActiveModel
【发布时间】:2013-07-06 15:40:09
【问题描述】:

我正在尝试制作一个简单的联系我们表单,因此我需要实现邮件程序。

我创建了一个控制器:

class ContactusController < ApplicationController
   def index
      @contact_us = Contactus.new
   end

   def new
      redirect_to(action: 'index')
   end

   def create
      @contact_us = Contactus.new(params[:contactus])
      if @contact_us.deliver
         flash[:notice] = "Thank-you, We will contact you shortly."
      else
         flash[:error] = "Oops!!! Something went wrong. Your mail was not sent."
      end
      render :index
    end
 end

因为我不想保存我使用 ActiveModel 的数据:

class Contactus
   extend ActiveModel::Naming
   include ActiveModel::Conversion
   include ActiveModel::Validations
   include ActionView::Helpers::TextHelper
   attr_accessor :name, :email, :message

   validate :name,
       :presence => true

   validates :email,
        :format => { :with => /\b[A-Z0-9._%a-z\-]+@(?:[A-Z0-9a-z\-]+\.)+[A-Za-z]{2,4}\z/ }

   validates :message,
        :length => { :minimum => 10, :maximum => 1000 }

   def initialize(attributes = {})
      attributes.each do |name, value|
          send("#{name}=", value)
      end
   end

   def deliver
       return false unless valid?
   mail(
        :to => "XXXXXXXX@gmail.com",
        :from => %("#{name}" <#{email}>),
        :reply_to => email,
        :subject => "Website inquiry",
        :body => message,
        :html_body => simple_format(message)
       )
   true
 end

 def persisted?
     false
 end
end

一切正常。路由很好,验证有效,但我得到的唯一错误是:undefined method mail for #&lt;Contactus:0x007f9da67173e8&gt;

我尝试在其中制作名称为 Contactus 和用户型号代码的邮件,但我得到了 以下错误:private method new used

如何在 ActiveModel 中使用 ActionMailer 功能?

【问题讨论】:

    标签: ruby ruby-on-rails-3 actionmailer activemodel


    【解决方案1】:

    要详细说明如何设置邮件程序,请运行以下命令来生成邮件程序:

    rails generate mailer ContactMailer
    

    将以下内容添加到名为app/mailers/contact_mailer的文件中

    class ContactMailer < ActionMailer::Base
      default from: #{from_email}
    
      def contact_mail(contact)
        @contact = contact
        mail(to: #{email}, subject: #{Whatever your subject would be})
      end
    end
    

    ActionMailer 使用视图来呈现其消息的布局。我检查了documentation,但在您在这里使用的html_body 选项上找不到任何内容。也许尝试删除它并使用body: simple_format(message) 或者只是创建模板app/views/mailers/contact_mailer/contact_mail.html.erb 并将消息放入您自己。您可以看到我故意将@contact 设为实例变量,因此如果我要创建这样的模板,我将可以访问该对象的所有可用属性,因此您可以根据需要进一步自定义。

    换个说法……

    我有点担心您在此处将 new 操作转发到 index 操作。你真的有充分的理由改变 RESTful 方法吗? 10 次中有 9 次遇到 private method called 的奇怪问题或其他一些神秘错误时,就是我试图欺骗系统的时候。如果您重新分配 new 操作以创建新的 Contactus 对象,是否可以解决您的问题?

    SMTP 设置更新

    我的雇主对这些设置的标准可能不是最好的,但这是我迄今为止所做的。在我的environments/production.rb

    config.action_mailer.raise_delivery_errors = false
    config.action_mailer.perform_deliveries = true
    config.action_mailer.delivery_method = :smtp
    config.action_mailer.smtp_settings = {
      :hash => 'of',
      :mail => 'client',
      :smtp => 'settings'
    }
    

    在我的environments/development.rb 中,我复制了相同的块,但将第一行替换为

    config.action_mailer.raise_delivery_errors = true
    

    在我的environments/test.rb 中,我没有添加以上任何内容。相反,我添加了这一行

    config.action_mailer.delivery_method = :test
    

    但那是因为我喜欢测试电子邮件是否在我的 RSpec 测试中发送。

    我不确定您在environment.rb 中的配置是什么样的,但您可能需要确保像这样通过 ActionMailer 进行配置。当然,在进行这些更改后重新启动您的应用程序,看看您是否仍然存在身份验证问题。

    针对特定的身份验证问题

    我的个人 SMTP 设置通过 gmail 进行身份验证,因此我在这些文件中的设置包括以下对:

    :address => 'smtp.gmail.com',
    :port => #{port_number},
    :domain => #{our_email_domain},
    :authentication => 'plain',
    :enable_starttls_auto => true
    

    尝试查找您的portdomain。如果您不是为拥有自己域的企业执行此操作,则谷歌搜索就足够了。如果您的密码没有被正确解释,authenticationsmarttls 设置应该会有所帮助。

    【讨论】:

    • 我一定会尝试的,关于新的操作问题,我发现我正在创建一个邮件类的对象,但实际上它没有构造器,所以有问题:)
    • 给我几分钟。我会用一些细节更新我的答案。
    • 我找出了问题所在。我使用的是 gmail,他们需要用户拥有特定于设备的密码或特定于域的密码我为我的 mac 创建了一个密码,它工作正常,我收到了邮件。但是在 heroku 上的生产模式上出现错误:(。我们必须将 smpt 配置放在 production.rb 或 enviornment.rb 的 rails 4 中??
    • 我知道它说 7 月 6 日 11:45:14 weblabsolution app/web.1: I, [2013-07-06T18:39:37.037813 #2] INFO -- : Completed 500 Internal Server 425 毫秒错误 7 月 6 日 11:45:14 weblabsolution app/web.1:F,[2013-07-06T18:39:37.042030 #2] 致命 -- :7 月 6 日 11:45:14 weblabsolution app/web.1: Net::SMTPAuthenticationError(535 身份验证失败:用户名/密码错误,但我输入了正确的用户名和密码
    【解决方案2】:

    您必须在继承自ActionMailer::Baseapp/mailers 中使用create a separate class。在该类中,您可以调用mail 方法。您不能在 ContactUs 类中直接调用 mail

    一旦您设置好您的邮件程序类,就可以在ContactUs 中像这样使用它:

    ContactMailer.contact_mail(self).deliver
    

    【讨论】:

    • 我使用 ActionMailer::Base.mail( :to => "xxxxxxxx@gmail.com", :from => %("#{name}" ), : reply_to => email, :subject => "网站查询", :body => message, :html_body => simple_format(message) ) 虽然我收到了成功消息,但它工作但没有收到邮件,为什么这样??
    • 你能更精确地一步一步做什么......对不起新手:(
    猜你喜欢
    • 1970-01-01
    • 2017-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多