我目前正在阅读http://learn-rails.com/content/online#send-mail以配置交易电子邮件。我到了本章的最后,在测试时,我收到了以下错误:
"An SMTP To address is required to send a message. Set the message smtp_envelope_to, to, cc, or bcc address."
我有" secrets.yml"和" .bash_profile"适当配置,以便可以调用to:收件人。
user_mailer.rb
class UserMailer < ApplicationMailer
default from: "do-not-reply@example.com"
def contact_email(contact)
@contact = contact
mail(to: Rails.application.secrets.owner_email, from: @contact.email, :subject => "Website Contact")
end
end
contacts_conrtoller.rb
class ContactsController < ApplicationController
def new
@contact = Contact.new
end
def create
@contact = Contact.new(secure_params)
if @contact.valid?
UserMailer.contact_email(@contact).deliver_now
flash[:notice] = "Message sent from #{@contact.name}."
redirect_to root_path
else
render :new
end
end
private
def secure_params
params.require(:contact).permit(:name, :email, :content)
end
end
contact.rb
class Contact
include ActiveModel::Model
attr_accessor :name, :string
attr_accessor :email, :string
attr_accessor :content, :string
validates_presence_of :name
validates_presence_of :email
validates_presence_of :content
validates_format_of :email,
with: /\A[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}\z/i
validates_length_of :content, :maximum => 500
end
答案 0 :(得分:1)
我能够找到解决这个问题的方法。我设置了一个新的gem文件 - https://github.com/bkeepers/dotenv,这样我就可以将我的env变量添加到.env文件而不是.bashrc或.bash_profile。这解决了这个问题。