2017-08-20 88 views
0

我能夠創建一個聯繫表單,當用戶點擊提交 - 它發送一封電子郵件和我收到的郵件。我希望通過sendgrid發送該電子郵件,以便我可以分析分析。我查看了Gorails Sendgrid課程,並能夠通過sendgrid發送電子郵件,但我不知道如何將其應用於我的聯繫表單。我在下面列出了我的代碼,任何幫助都會很棒。非常感謝! (當用戶點擊提交,定期發送電子郵件聯繫表)Rails如何將電子郵件表格與Sendgrid鏈接?

new.html.erb

<div align="center"> 
<h3>Send A message to Us</h3> 
    <%= form_for @contact do |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 you for your message. We will contact you 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(在我的配置>初始化文件夾)

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 
} 

郵件程序文件夾(我只有用我的聯繫表格兩個文件,通知和申請無交易)

回答

0

我想通了什麼我錯過了這一點。我需要爲聯繫人生成郵件。與完成並添加一行到我contacts_controller.rb,我能夠通過sendgrid送我的電子郵件沒有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 you for your message. We will contact you 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 

聯繫梅勒

class ContactMailer < ApplicationMailer 
def new_request 
end 
end 
相關問題