【问题标题】:Rails How to link an email form with Sendgrid?Rails 如何将电子邮件表单与 Sendgrid 链接?
【发布时间】:2017-08-20 12:56:28
【问题描述】:

我能够创建一个联系表单,当用户单击提交时,它会发送一封电子邮件,我会在我的收件箱中收到该表单。我想通过 sendgrid 发送该电子邮件,以便我可以分析分析。我查看了 Gorails Sendgrid 课程,并能够通过 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
}

邮件文件夹(我只有两个文件通知和应用程序都没有处理我的联系人表单)

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 ruby-on-rails-5 sendgrid


    【解决方案1】:

    我知道我缺少什么。我需要为联系人生成邮件。完成并在我的contacts_controller.rb中添加一行后,我就可以毫无问题地通过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
    

    【讨论】:

    • 谢谢!我做了与您相同的 gorails 教程,并且想知道如何将其应用于与您相同的联系表单,虽然我的代码看起来略有不同,但这帮助我找到了我需要的缺失部分。哦,对于那些想知道的人,我可以确认这仍然适用于 Rails 6。
    猜你喜欢
    • 2021-06-19
    • 1970-01-01
    • 1970-01-01
    • 2012-01-03
    • 2017-07-27
    • 2015-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多