2016-06-08 71 views
0

我想知道如果有人能夠幫助我,在我的Ruby on Rails應用程序中,我試圖讓系統從特定的電子郵件地址發送電子郵件( Gmail)添加到特定的電子郵件地址(Gmail),但由於某種原因,電子郵件不會發送 - 但沒有錯誤消息。Ruby on Rails:創建記錄時發送電子郵件不起作用

在我的控制,我有以下的代碼,將在一個新的記錄保存電子郵件:

def create 
    @email = Email.new(email_params) 
    if @email.save 
    UserMailer.welcome_email.deliver 
     redirect_to @email 
    else 
     render 'new' 
    end 
end 

這應該稱之爲梅勒:

class UserMailer < ApplicationMailer 
    default from: '[email protected]' 

    def welcome_email() 
    mail(to: '[email protected]', subject: 'Welcome to My Awesome Site') 
    end 
end 

我的應用梅勒如下:

class ApplicationMailer < ActionMailer::Base 
    default from: "[email protected]" 
    layout 'mailer' 
end 

我那麼有welcome_email.html.erb

<!DOCTYPE html> 
<html> 
    <head> 
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' /> 
    </head> 
    <body> 
    <h1>Welcome to Thor Cinemas,!</h1> 
    <p> 
     You have successfully signed up to Thor Cinemas, 
     your username is: .<br> 
    </p> 
    <p> 
     To login to the site, just follow this link: <%= @url %>. 
    </p> 
    <p>Thanks for joining and have a great day!</p> 
    </body> 
</html> 

和上面類似的文本版本。

出於某種原因,這是行不通的,我在$RAILS_ENV以下(在\config\environments):

config.action_mailer.delivery_method = :sendmail 
config.action_mailer.perform_deliveries = true 
config.action_mailer.raise_delivery_errors = true 
config.action_mailer.default_options = {from: '[email protected]'} 

的下面在同一位置的development.rb

Rails.application.configure do 
    config.cache_classes = false 
    config.eager_load = false 
    config.consider_all_requests_local  = true 
    config.action_controller.perform_caching = false 
    config.action_mailer.raise_delivery_errors = false 
    config.active_support.deprecation = :log 
    config.active_record.migration_error = :page_load 
    config.assets.debug = true 
    config.assets.digest = true  
    config.assets.raise_runtime_errors = true  
    config.action_mailer.delivery_method = :smtp 
    config.action_mailer.smtp_settings = { 
    address:    'smtp.gmail.com', 
    port:     587, 
    domain:    'example.com', 
    user_name:   '[email protected]', #replace with your username Eg. john.smith 
    password:    'mypassword', #replace with your gmail password 
    authentication:  'plain', 
    enable_starttls_auto: true } 
end 

而以下在production.rb處在同一位置:

Rails.application.configure do 
    config.cache_classes = true 
    config.eager_load = true 
    config.consider_all_requests_local  = false 
    config.action_controller.perform_caching = true 
    config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present? 
    config.assets.js_compressor = :uglifier 
    config.assets.compile = false 
    config.assets.digest = true 
    config.log_level = :debug 
    config.i18n.fallbacks = true 
    config.active_support.deprecation = :notify 
    config.log_formatter = ::Logger::Formatter.new 
    config.active_record.dump_schema_after_migration = false 
    config.action_mailer.delivery_method = :smtp 
    config.action_mailer.smtp_settings = { 
    :address    => "smtp.gmail.com", 
    :port     => 587, 
    :user_name   => ENV['EMAIL_USER'], 
    :password    => ENV['EMAIL_PASSWORD'], 
    :authentication  => "plain", 
    :enable_starttls_auto => true 
    } 
end 

我的Gemfile如下:

source 'https://rubygems.org' 


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails' 
gem 'rails', '4.2.6' 
# Use sqlite3 as the database for Active Record 
gem 'sqlite3' 
# Use SCSS for stylesheets 
gem 'sass-rails', '~> 5.0' 
# Use Uglifier as compressor for JavaScript assets 
gem 'uglifier', '>= 1.3.0' 
# Use CoffeeScript for .coffee assets and views 
gem 'coffee-rails', '~> 4.1.0' 
# See https://github.com/rails/execjs#readme for more supported runtimes 
# gem 'therubyracer', platforms: :ruby 

# Use jquery as the JavaScript library 
gem 'jquery-rails' 
# Turbolinks makes following links in your web application faster. Read more:   https://github.com/rails/turbolinks 
gem 'turbolinks' 
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder 
gem 'jbuilder', '~> 2.0' 
# bundle exec rake doc:rails generates the API under doc/api. 
gem 'sdoc', '~> 0.4.0', group: :doc 

# Use ActiveModel has_secure_password 
# gem 'bcrypt', '~> 3.1.7' 

# Use Unicorn as the app server 
# gem 'unicorn' 

# Use Capistrano for deployment 
# gem 'capistrano-rails', group: :development 

group :development, :test do 
    # Call 'byebug' anywhere in the code to stop execution and get a debugger console 
    gem 'byebug' 
end 

group :development do 
# Access an IRB console on exception pages or by using <%= console %> in views 
    gem 'web-console', '~> 2.0' 
end 

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem 
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] 
gem 'validates_email_format_of' 
gem "letter_opener", :group => :development 

我得到的log什麼時候創建一個新的記錄如下:

Started POST "/emails" for ::1 at 2016-06-08 19:41:10 +0100 
Processing by EmailsController#create as HTML 
    Parameters: {"utf8"=>"✓",  "authenticity_token"=>"5MZ3NNxbc5T5T/S9vLFK7EskqEGg4Xd2ZBimoQd3eXklW9HW+i+wxRryp CFg//I5UUq7SnesIfPR0LDM4VFy7w==", "email"=>{"title"=>"qqqqqqqqqqqqqqqqq",  "text"=>""}, "commit"=>"Create Email"} 
    [1m[36m (0.0ms)[0m [1mbegin transaction[0m 
    [1m[35mSQL (1.0ms)[0m INSERT INTO "emails" ("title", "text", "created_at",  "updated_at") VALUES (?, ?, ?, ?) [["title", "qqqqqqqqqqqqqqqqq"], ["text",  ""], ["created_at", "2016-06-08 18:41:10.647620"], ["updated_at", "2016-06-08  18:41:10.647620"]] 
    [1m[36m (173.0ms)[0m [1mcommit transaction[0m 
    Rendered user_mailer/welcome_email.html.erb within layouts/mailer (0.0ms) 
    Rendered user_mailer/welcome_email.text.erb within layouts/mailer (0.0ms) 

UserMailer#welcome_email: processed outbound mail in 264.0ms 

Sent mail to [email protected] (442.0ms) 
Date: Wed, 08 Jun 2016 19:41:11 +0100 
From: [email protected] 
To: [email protected] 
Message-ID: <[email protected]> 
Subject: Welcome to My Awesome Site 
Mime-Version: 1.0 
Content-Type: multipart/alternative; 
boundary="--==_mimepart_575866c714aa5_1fa44e42ca880795"; 
charset=UTF-8 
Content-Transfer-Encoding: 7bit 


----==_mimepart_575866c714aa5_1fa44e42ca880795 
Content-Type: text/plain; 
charset=UTF-8 
Content-Transfer-Encoding: 7bit 

Welcome to example.com, 
=============================================== 

You have successfully signed up to example.com, 
your username is:. 

To login to the site, just follow this link: http://localhost:3000/email/new. 

Thanks for joining and have a great day! 

----==_mimepart_575866c714aa5_1fa44e42ca880795 
Content-Type: text/html; 
charset=UTF-8 
Content-Transfer-Encoding: 7bit 

<html> 
    <body> 
    <!DOCTYPE html> 
<html> 
    <head> 
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' /> 
    </head> 
    <body> 
    <h1>Welcome!</h1> 
    <p>  
    </p> 
    <p> 
     To login to the site, just follow this link:  http://localhost:3000/email/new. 
    </p> 
    <p>Thanks for joining and have a great day!</p> 
    </body> 
</html> 
    </body> 
</html> 

----==_mimepart_575866c714aa5_1fa44e42ca880795-- 

Redirected to http://localhost:3000/emails/42 
Completed 302 Found in 890ms (ActiveRecord: 174.0ms) 


Started GET "/emails/42" for ::1 at 2016-06-08 19:41:11 +0100 
Processing by EmailsController#show as HTML 
    Parameters: {"id"=>"42"} 
    [1m[35mEmail Load (0.0ms)[0m SELECT "emails".* FROM "emails" WHERE  "emails"."id" = ? LIMIT 1 [["id", 42]] 
    Rendered emails/show.html.erb within layouts/application (0.0ms) 
Completed 200 OK in 86ms (Views: 84.1ms | ActiveRecord: 0.0ms) 


Started GET "/emails" for ::1 at 2016-06-08 19:41:13 +0100 
Processing by EmailsController#index as HTML 
    [1m[36mEmail Load (0.0ms)[0m [1mSELECT "emails".* FROM "emails"[0m 
    Rendered emails/index.html.erb within layouts/application (9.0ms) 
Completed 200 OK in 88ms (Views: 87.3ms | ActiveRecord: 0.0ms) 

我,爲什麼這不工作真糊塗,有人知道嗎?

請注意,我使用Windows 7並運行Rails 4.2.6。

+1

這環境你與嘗試,如果有正在創建的郵件,你看到的日誌並沒有交付 – Bijendra

+0

我在開發環境中運行,並根據日誌的電子郵件正在創建 –

+0

你是否得到任何Net :: SMTPAuthenticationError或任何錯誤與您正在提供的郵件憑據 – Bijendra

回答

1

我認爲你需要使用deliver_now代替交付。

UserMailer.welcome_email.deliver_now 

希望這會對你有用。

+0

感謝您的回答,我剛剛嘗試過這一點,但沒有什麼區別 –

+1

您可以使用此config.action_mailer.perform_deliveries = true。 之後,您可以在您的電子郵件帳戶中看到任何交貨原因錯誤。 還有一件事你需要設置你的電子郵件帳戶的低安全應用程序也發送郵件。 –

0

郵件默認情況下不開發環境交付,在你development.rb添加此

 `config.action_mailer.perform_deliveries = true` 

進行交付。如果你正在使用Rails 4,使用

Notifier.welcome(User.first).deliver_now 

按照文件,你也可以使用

Notifier.welcome(User.first).deliver_now! 

它提供了不檢查perform_deliveries和raise_delivery_errors電子郵件,所以請謹慎使用。

編輯:

我看到域從production.rb您的郵件設置丟失,請使用

:domain    => "gmail.com", 
+0

感謝您的回答,這仍然不起作用 –

+0

在'development.rb'中添加行是否重要? –

+0

我會建議在生產環境中運行應用程序並測試它。確認郵件是否在生產日誌中創建,並且如果它們正在工作,則直接檢查憑證。 – Bijendra

相關問題