如何从我的rails网站发送与我联系的电子邮件

时间:2015-09-30 12:36:35

标签: ruby-on-rails ruby email ruby-on-rails-4 mailer

看起来这么简单,我不想使用设计或任何用户,这是我的个人网站,我在那里展示我的作品。我想要做的就是联系我一个电子邮件表单,以便我可以收到访问我网站的人的消息。我遇到的所有教程都会将电子邮件发送给用户。

当我尝试创建电子邮件时。这就是我得到的:

请求SMTP-AUTH但缺少用户名

@contact = Contact.new(params[:contact])
@contact.request = request
if @contact.deliver
  flash.now[:notice] = 'Thank you, I will contact you soon.'
  redirect_to root_path
else

这是我的联络管理员:

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

  def create
    @contact = Contact.new(params[:contact])
    @contact.request = request
    if @contact.valid?
      ContactMe.contact_email(@contact).deliver
      redirect_to root_path
      flash[:notice] = "Message sent from {@contact.name}"
    else     
      render :new
      flash.now[:error] = 'Could not send message as is. Please check email and phone fields.'
    end
  end
end

联系模式:

class Contact < MailForm::Base
  attribute :name,      :validate => true
  attribute :email,     :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
  attribute :message


  # Declare the e-mail headers. It accepts anything the mail method
  # in ActionMailer accepts.
  def headers
    {
      :subject => "Contact",
      :to => "literallymyemail@gmail.com",
      :from => %("#{name}" <#{email}>)
    }
  end
end

class ContactMe < ApplicationMailer

  def contact_email(contact)
    @contact = contact
    mail(to: 'literallymyemail@gmail.com', from: @contact.email, :subject => "Website Contact")
  end
end

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       = true
  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 serving static files from the `/public` folder by default since
  # Apache or NGINX already handles this.
  config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?

  # Compress JavaScripts and CSS.
  config.assets.js_compressor = :uglifier
  # config.assets.css_compressor = :sass
  config.action_mailer.perform_deliveries = true
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.default_url_options = { :host => 'nicolasdev.herokuapp.com' }
  config.action_mailer.smtp_settings = {
    :address              => "smtp.gmail.com",  
    :port                 => 587,
    :domain               => 'gmail.com',
    :enable_starttls_auto => true,
    :user_name            => ENV["GMAIL_USERNAME"],
    :password             => ENV["GMAIL_PASSWORD"],
    :authentication       => "plain"

  }

  # Do not fallback to assets pipeline if a precompiled asset is missed.
  config.assets.compile = true
  config.assets.precompile = ['*.js', '*.css', '*.jpg', '*.png', '*.gif', '*.ico'] 

  # Asset digests allow you to set far-future HTTP expiration dates on all assets,
  # yet still be able to expire them through the digest params.
  config.assets.digest = true

  # `config.assets.precompile` and `config.assets.version` have moved to config/initializers/assets.rb

  # 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

  # Use the lowest log level to ensure availability of diagnostic information
  # when problems arise.
  config.log_level = :debug

  # 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'

  # 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

  # 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

application.yml:

GMAIL_USERNAME: 'literallymyemail@gmail.com'
GMAIL_PASSWORD: 'literallymyemailpassword'

我可能犯了很多错误,但请怜悯我。我已经有一段时间了,潜伏着其他帖子和教程无济于事。

1 个答案:

答案 0 :(得分:0)

感谢您关于sendgrid的提示,这可能是我要打击我的头脑的下一件事。谢谢你让我头疼。我的问题比这更深刻。似乎我的一个宝石依赖于sql3导致它不更新我的控制器。我从来没有注意到我没有推动我的更改(哎呀!)我看到自己的帖子后看到了错误日志和我的github中的控制器之间的差异。不知道这个问题对社区有多大帮助,但是谁知道......谢谢你的帮助。