2015-09-30 112 views
0

这似乎是这样一个简单的事情,我不想使用设计或任何用户的事,这是我的个人网站,在那里我展示我的工作。我想要做的就是联系我的电子邮件表格,以便我可以从访问我的网站的人那里收到消息。我遇到的所有教程都会将电子邮件发送给用户。如何发送联系我的电子邮件从我的rails网站

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

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 => "[email protected]", 
     :from => %("#{name}" <#{email}>) 
    } 
    end 
end 

class ContactMe < ApplicationMailer 

    def contact_email(contact) 
    @contact = contact 
    mail(to: '[email protected]', from: @contact.email, :subject => "Website Contact") 
    end 
end 

生产。 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: '[email protected]' 
GMAIL_PASSWORD: 'literallymyemailpassword' 

我可能做了许多错误,但请可怜我吧。我一直在这一段时间,潜伏其他职位和教程无济于事。

+0

在您能够发送邮件之前,需要安装并配置Rails之外的电子邮件堆栈。 Heroku应用程序可以很容易地使用Sendgrid,但您必须启用它。你是否将Sendgrid添加到你的Heroku应用程序中? – Elvn

+0

我不知道这件事。我会做一些研究。这在任何教程中都没有讨论 –

+0

下面是一个很好的开始:https://devcenter.heroku.com/articles/sendgrid – Elvn

回答

0

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

相关问题