首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ActionMailer和ActiveModel

ActionMailer和ActiveModel
EN

Stack Overflow用户
提问于 2013-07-06 23:40:09
回答 2查看 221关注 0票数 0

我正在尝试做一个简单的联系我们的表单,所以我需要实现的邮件。

我创建了一个控制器:

代码语言:javascript
复制
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的数据:

代码语言:javascript
复制
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 #<Contactus:0x007f9da67173e8>

我尝试使用名称Contactus和用户模型代码来制作邮件程序,但是我得到了以下错误:private method new used

如何在ActiveModel中使用ActionMailer函数?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-07-07 01:00:41

要更详细地了解如何设置邮件程序,请运行以下命令以生成邮件程序:

代码语言:javascript
复制
rails generate mailer ContactMailer

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

代码语言:javascript
复制
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

代码语言:javascript
复制
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中,我复制了相同的块,但将第一行替换为

代码语言:javascript
复制
config.action_mailer.raise_delivery_errors = true

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

代码语言:javascript
复制
config.action_mailer.delivery_method = :test

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

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

针对特定身份验证问题的

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

代码语言:javascript
复制
:address => 'smtp.gmail.com',
:port => #{port_number},
:domain => #{our_email_domain},
:authentication => 'plain',
:enable_starttls_auto => true

试着查找你的portdomain。如果你不是为一家有自己域名的企业做这件事,谷歌搜索应该就足够了。如果您的密码没有被正确解释,authenticationsmarttls设置应该会有所帮助。

票数 2
EN

Stack Overflow用户

发布于 2013-07-06 23:58:20

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

设置好邮件类后,就可以在ContactUs中像这样使用它了

代码语言:javascript
复制
ContactMailer.contact_mail(self).deliver
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17504437

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档