问题
I am working on a tutorial about ActiveRecords and needed to configure ActionMailer.
I have Environmental Variables configured in ~/.bashrc and this system has been working reliably.
I configured everything in the development.rb
development.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.default_url_options = { :host => "localhost:3000" }
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "gmail.com",
user_name: ENV[ "GMAIL_USERNAME" ],
password: ENV[ "GMAIL_PASSWORD"],
authentication: "plain",
enable_starttls_auto: true
}
My contact_form.rb model is:
class ContactForm < MailForm::Base
attribute :name, :validate => true
attribute :email, :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
attribute :message, :validate => true
# Declare the e-mail headers
def headers
{
:subject => 'ArcadeNomad - Contact Request',
:to => ENV[ "OWNER_EMAIL" ],
:from => ENV[ "OWNER_EMAIL" ]
}
end
end
When I test everything in rails console i get:
Machine:dev_arcadenomad_com Account$ rails c --sandbox
Loading development environment in sandbox (Rails 4.2.1)
Any modifications you make will be rolled back on exit
>> contact = ContactForm.new(:name => 'Name', :email => 'SomeMail@domain.tld', :message => 'I love ArcadeNomad!')
=> #<ContactForm:0x007fde0d6d38a8 @name="Name", @email="SomeMail@domain.tld", @message="I love ArcadeNomad!">
>> contact.valid?
=> true
>> contact.deliver
Rendered /Users/MyAccount/.rvm/gems/ruby-2.2.1@learn-active-records/gems/mail_form-1.5.1/lib/mail_form/views/mail_form/contact.erb (8.1ms)
MailForm::Notifier#contact: processed outbound mail in 239.6ms
Sent mail to mymail@gmail.com (6.9ms)
Date: Sat, 27 Jun 2015 17:32:01 +0200
From: mymail@gmail.com
To: mymail@gmail.com
Message-ID: <LongString@machine.domain.tl.mail>
Subject: ArcadeNomad - Contact Request
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<h4 style="text-decoration:underline">ArcadeNomad - Contact Request</h4>
<p><b>Name:</b>
Name</p>
<p><b>Email:</b>
SomeMail@domain.tld</p>
<p><b>Message:</b>
I love ArcadeNomad!</p>
=> true
But no mail is delivered.
If I hardcode my credentials into development.rb everything works. When I test for ENV[ "GMAIL_USERNAME" ] in rails console my mail credentials are displayed correctly.
What am I missing?
来源:https://stackoverflow.com/questions/31090589/gmail-not-sending-with-env-variables-in-rails-4-project-but-env-variables-are-d