参数错误发送消息sendgrid需要SMTP To地址

时间:2016-06-13 06:54:35

标签: ruby-on-rails email heroku sendgrid

所以我在codermanual上做了一个在线训练营,顺便说一句,我是一个初学者,刚开始学习Ruby和其他webdev的东西。

我坚持某一步,而且我有一个关于"接收电子邮件的问题"从我的webapp的联系表单页面通过heroku。

出于某种原因,我似乎无法使sendgrid工作。 当我点击联系表单的提交按钮时,网页会返回错误消息。

我精确地遵循了这些步骤,但我的contacts_controller.rb似乎有问题,特别是这行代码:

ContactMailer.contact_email(姓名,电子邮件,正文).deliver

ruby​​ v2.3.0p0

$ heroku logs

ArgumentError(发送邮件需要SMTP地址。设置消息smtp_envelope_to,to,cc或bcc地址。): 2016-06-13T05:24:00.381485 + 00:00 app [web.1]:app / controllers / contacts_controller.rb:14:在'create'

感谢帮助。 以下是其余文件:

控制器/ contacts_controller.rb

class ContactsController < ApplicationController
def new
@contact = Contact.new
end

def create
@contact = Contact.new(contact_params)

if @contact.save
  name = params[:contact][:name]
  email = params[:contact][:email]
  body = params[:contact][:comments]


  ContactMailer.contact_email(name, email, body).deliver

  flash[:success] = 'Message sent.'
  redirect_to new_contact_path
else
  flash[:danger] = 'Error occured, message has not been sent'
  redirect_to new_contact_path
end
end

private
def contact_params
  params.require(:contact).permit(:name, :email, :comments)
end
end

配置/ application.rb中

require File.expand_path('../boot', __FILE__)

require 'rails/all'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module SimplecodecastsSaas
class Application < Rails::Application

# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.

# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'

# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
end
end

视图/ contact_mailer / contact_email.html.erb

<!DOCTYPE html>
<html>
<head>

</head>
<body>
    <p>You have received a message from the site's contact form, from <%= "#{ @name }, #{ @email }." %></p>
    <p><%= @body %></p>
</body>

环境/ production.rb

Rails.application.configure do
# Settings specified here will take precedence over those in          config/application.rb.

# Code is not reloaded between requests.
config.cache_classes = true


# Eager load code on boot. This eager loads most of Rails and
# your application in memory, allowing both threaded web servers
# and those relying on copy on write to perform better.
# Rake tasks automatically ignore this option for performance.
config.eager_load = true

# Full error reports are disabled and caching is turned on.
config.consider_all_requests_local       = false
config.action_controller.perform_caching = true

# Enable Rack::Cache to put a simple HTTP cache in front of your application
# Add `rack-cache` to your Gemfile before enabling this.
# For large-scale production use, consider using a caching reverse proxy like nginx, varnish or squid.
# config.action_dispatch.rack_cache = true

# Disable Rails's static asset server (Apache or nginx will already do this).
config.serve_static_assets = false

# Compress JavaScripts and CSS.
config.assets.js_compressor = :uglifier
# config.assets.css_compressor = :sass

# Do not fallback to assets pipeline if a precompiled asset is missed.
config.assets.compile = false

# Generate digests for assets URLs.
config.assets.digest = true

# Version of your assets, change this if you want to expire all your assets.
config.assets.version = '1.0'

# Specifies the header that your server uses for sending files.
# config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx

# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
# config.force_ssl = true

# Set to :debug to see everything in the log.
config.log_level = :info

# Prepend all log lines with the following tags.
# config.log_tags = [ :subdomain, :uuid ]

# Use a different logger for distributed setups.
# config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)

# Use a different cache store in production.
# config.cache_store = :mem_cache_store

# Enable serving of images, stylesheets, and JavaScripts from an asset server.
# config.action_controller.asset_host = "http://assets.example.com"

# Precompile additional assets.
# application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
# config.assets.precompile += %w( search.js )

# Ignore bad email addresses and do not raise email delivery errors.
# Set this to true and configure the email server for immediate delivery to raise delivery errors.
# config.action_mailer.raise_delivery_errors = false

# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation cannot be found).
config.i18n.fallbacks = true

# Send deprecation notices to registered listeners.
config.active_support.deprecation = :notify

# Disable automatic flushing of the log to improve performance.
# config.autoflush_log = false

# Use default logging formatter so that PID and timestamp are not suppressed.
config.log_formatter = ::Logger::Formatter.new

# Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false
end

环境/ development.rb

Rails.application.configure do
# Settings specified here will take precedence over those in   config/application.rb.

# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false

#a fix for circular dependency while autoloading constant.
#config.middleware.delete Rack::Lock

# Do not eager load code on boot.
config.eager_load = false

# Show full error reports and disable caching.
config.consider_all_requests_local       = true
config.action_controller.perform_caching = false

# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false

# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log

# Raise an error on page load if there are pending migrations.
config.active_record.migration_error = :page_load

# Debug mode disables concatenation and preprocessing of assets.
# This option may cause significant delays in view rendering with a large
# number of complex assets.
config.assets.debug = true

# Adds additional error checking when serving assets at runtime.
# Checks for improperly declared sprockets dependencies.
# Raises helpful error messages.
config.assets.raise_runtime_errors = true

# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
end

应用程序/邮寄者/ contact_mailer.rb

class ContactMailer < ActionMailer::Base
default to: "jxxmxlmxo@gmail.com"

def contact_email(name, email, body)
    @name = name
    @email = email
    @body = body

    mail(from: email, subject: 'Contact Form Message')
end
end

0 个答案:

没有答案