Rails How to link an email form with Sendgrid? -
i able create contact form when user clicks submit - send email , receive in inbox. have email sent via sendgrid can analyze analytics. looked @ gorails sendgrid course , able send email via sendgrid don't know how apply contact form. have listed code below, amazing. thank much!
new.html.erb (contact form sends email regularly when user clicks submit)
<div align="center"> <h3>send message us</h3> <%= form_for @contact |f| %> <div class="field"> <%= f.label :name %><br> <%= f.text_field :name, :required => true %> </div> <div class="field"> <%= f.label :email %><br> <%= f.email_field :email, :required => true %> </div> <div class="field"> <%= f.label :message %><br> <%= f.text_area :message, :as => :text, :required => true %> </div> <div class="actions"> <%= f.submit "send message", :class => "btn btn-primary btn-md"%> </div> <% end %> </div>
contacts_controller.rb
class contactscontroller < applicationcontroller def new @contact = contact.new end def create @contact = contact.new(contact_params) @contact.request = request if @contact.deliver flash.now[:notice] = 'thank message. contact soon!' else flash.now[:error] = 'cannot send message.' render :new end end private def contact_params params.require(:contact).permit(:name, :email, :message) end end
sendgrid.rb (within config > initializers folder)
actionmailer::base.smtp_settings = { :user_name => 'apikey', :password => rails.application.secrets.sendgrid_api_key, :domain => 'tango.co', :address => 'smtp.sendgrid.net', :port => 587, :authentication => :plain, :enable_starttls_auto => true }
development.rb
config.action_mailer.perform_caching = false config.action_mailer.delivery_method = :smtp actionmailer::base.smtp_settings = { :user_name => 'apikey', :password => rails.application.secrets.sendgrid_api_key, :domain => 'tango.co', :address => 'smtp.sendgrid.net', :port => 587, :authentication => :plain, :enable_starttls_auto => true }
mailers folder (i have 2 files notification , application none deal contacts form)
i figured out missing this. needed generate mailer contacts. completed , adding line contacts_controller.rb, able send emails via sendgrid no problemo :)
class contactscontroller < applicationcontroller def new @contact = contact.new end def create @contact = contact.new(contact_params) @contact.request = request if @contact.save contactmailer.new_request(@contact.id).deliver_later end if @contact.deliver flash.now[:notice] = 'thank message. contact soon!' else flash.now[:error] = 'cannot send message.' render :new end end private def contact_params params.require(:contact).permit(:name, :email, :message) end end
contacts mailer
class contactmailer < applicationmailer def new_request end end
Comments
Post a Comment