Gmail不在Rails 4项目中使用env变量发送,但env变量显示在控制台

时间:2015-06-27 15:52:45

标签: ruby-on-rails gmail environment-variables development-environment

我正在开发一个关于ActiveRecords的教程,需要配置ActionMailer。

我在〜/ .bashrc中配置了环境变量,这个系统已经可靠地工作了。

我在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
}

我的contact_form.rb模型是:

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

当我在rails console中测试一切时,我得到:

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

但没有邮件发送。

如果我将我的凭据硬编码到development.rb一切正常。 当我在rails控制台中测试ENV [“GMAIL_USERNAME”]时,我的邮件凭证会正确显示。

我错过了什么?

0 个答案:

没有答案