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
}
郵件程序文件夾(我只有用我的聯繫表格兩個文件,通知和申請無交易)