ruby - How to send a contact me email from my rails site -
it seems such simple thing, don't want use devise or users matter, personal website, display work. want make contact me email form can recieve messages people visit site. of tutorial have encountered send email user.
when try create email. get:
smtp-auth requested missing user name
@contact = contact.new(params[:contact]) @contact.request = request if @contact.deliver flash.now[:notice] = 'thank you, contact soon.' redirect_to root_path else
this contact controller:
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 {@contact.name}" else render :new flash.now[:error] = 'could not send message is. please check email , phone fields.' end end end
contact model:
class contact < mailform::base attribute :name, :validate => true attribute :email, :validate => /\a([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i attribute :message # declare e-mail headers. accepts 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 # settings specified here take precedence on in config/application.rb. # code not reloaded between requests. config.cache_classes = true # eager load code on boot. eager loads of rails , # application in memory, allowing both threaded web servers # , relying on copy on write perform better. # rake tasks automatically ignore option performance. config.eager_load = true # full error reports disabled , caching turned on. config.consider_all_requests_local = true config.action_controller.perform_caching = true # enable rack::cache put simple http cache in front of application # add `rack-cache` gemfile before enabling this. # large-scale production use, consider using caching reverse proxy # nginx, varnish or squid. # config.action_dispatch.rack_cache = true # disable serving static files `/public` folder default since # apache or nginx handles this. config.serve_static_files = env['rails_serve_static_files'].present? # compress javascripts , 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" } # not fallback assets pipeline if precompiled asset missed. config.assets.compile = true config.assets.precompile = ['*.js', '*.css', '*.jpg', '*.png', '*.gif', '*.ico'] # asset digests allow set far-future http expiration dates on assets, # yet still able expire them through digest params. config.assets.digest = true # `config.assets.precompile` , `config.assets.version` have moved config/initializers/assets.rb # specifies header server uses sending files. # config.action_dispatch.x_sendfile_header = 'x-sendfile' # apache # config.action_dispatch.x_sendfile_header = 'x-accel-redirect' # nginx # force access app on ssl, use strict-transport-security, , use secure cookies. # config.force_ssl = true # use lowest log level ensure availability of diagnostic information # when problems arise. config.log_level = :debug # prepend log lines following tags. # config.log_tags = [ :subdomain, :uuid ] # use different logger distributed setups. # config.logger = activesupport::taggedlogging.new(sysloglogger.new) # use different cache store in production. # config.cache_store = :mem_cache_store # enable serving of images, stylesheets, , javascripts asset server. # config.action_controller.asset_host = 'http://assets.example.com' # ignore bad email addresses , not raise email delivery errors. # set true , configure email server immediate delivery raise delivery errors. # config.action_mailer.raise_delivery_errors = false # enable locale fallbacks i18n (makes lookups locale fall # i18n.default_locale when translation cannot found). config.i18n.fallbacks = true # send deprecation notices registered listeners. config.active_support.deprecation = :notify # use default logging formatter pid , timestamp not suppressed. config.log_formatter = ::logger::formatter.new # not dump schema after migrations. config.active_record.dump_schema_after_migration = false end
application.yml:
gmail_username: 'literallymyemail@gmail.com' gmail_password: 'literallymyemailpassword'
i making numerous mistakes, please have mercy on me. have been @ time, lurking other posts , tutorials no avail.
thank tips on sendgrid, next thing going bash head agaist. thank saving me headache. problem deeper though. seems 1 of gems dependent on sql3 causing not update controller. never noticed not pushing changes (oops!) noticed after saw own post , saw difference in error log , controller in github. don't know how useful question community, knows... thank you help.
Comments
Post a Comment