我正在尝试做一个简单的联系我们的表单,所以我需要实现的邮件。
我创建了一个控制器:
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 #<Contactus:0x007f9da67173e8>
我尝试使用名称Contactus和用户模型代码来制作邮件程序,但是我得到了以下错误:private method new used
如何在ActiveModel中使用ActionMailer函数?
发布于 2013-07-07 01:00:41
要更详细地了解如何设置邮件程序,请运行以下命令以生成邮件程序:
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
endActionMailer使用视图来呈现其消息的布局。我检查了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试着查找你的port和domain。如果你不是为一家有自己域名的企业做这件事,谷歌搜索应该就足够了。如果您的密码没有被正确解释,authentication和smarttls设置应该会有所帮助。
发布于 2013-07-06 23:58:20
你必须在继承自ActionMailer::Base的app/mailers中使用create a separate class。在该类中,您可以调用mail方法。不能在ContactUs类中直接调用mail。
设置好邮件类后,就可以在ContactUs中像这样使用它了
ContactMailer.contact_mail(self).deliverhttps://stackoverflow.com/questions/17504437
复制相似问题