2017-08-20 12 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

(私は私の連絡先フォームを持つ2つのファイルの通知およびアプリケーションなしの契約を持っています)。連絡先のメーラーを生成する必要がありました。それが完了し、私のcontacts_controller.rbに行を追加することで、私はノーproblemoでsendgrid経由で私の電子メールを送信することができました:)

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 
関連する問題