【发布时间】:2019-01-20 19:26:49
【问题描述】:
我正在尝试在我的应用中添加一个简单的电子邮件表单,以便我可以接收来自用户的电子邮件。我遵循了一些教程,最终成功地在开发模式下向自己发送电子邮件。我就是这样做的:
1) 我安装了这个 gem:'mail_form';
2) 生成一个联系人控制器:
#contacts_controller.rb
class ContactsController < ApplicationController
def new
@contact = Contact.new
end
def create
@contact = Contact.new(params[:contact])
@contact.request = request
if @contact.deliver
flash.now[:error] = nil
else
flash.now[:error] = 'Não foi possível enviar o email.'
end
redirect_back(fallback_location: vehicles_path)
end
end
3) 我(手动)创建了一个联系人模型:
#contact.rb
class Contact < MailForm::Base
attribute :name, :validate => true
attribute :email, :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
attribute :subject
attribute :message, :validate => true
attribute :nickname, :captcha => true
def headers
{
:subject => %("#{subject}"),
:to => "myEmail@gmail.com",
:from => %("#{name}" <#{email}>)
}
end
end
4) 编辑了我的 development.rb
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'gmail.com',
user_name: 'myusername@gmail.com',
password: Rails.application.credentials.email_password,
authentication: :plain,
enable_starttls_auto: true
}
5) 我的表单
<%= form_with model:contact do |f| %>
<div class="field">
<%= f.label "Name" %>
<%= f.text_field :name, required: true %>
</div>
<div class="field">
<%= f.label "Email" %>
<%= f.email_field :email, required: true %>
</div>
<div class="field">
<%= f.label "Subject" %>
<%= f.text_field :subject, required: true %>
</div>
<div class="field">
<%= f.label "Message" %>
<%= f.text_area :message, as: :text, rows: 8, required: true %>
</div>
<div class="hidden">
<%= f.email_field :nickname, hint: 'leave this field empty' %>
</div>
<div class="actions">
<%= f.submit "Submit", class: "contact_submit" %>
</div>
<% end %>
这在开发中运行良好。
但是,现在我不知道在生产中要做什么。我已经将我的应用程序托管在 DigitalOcean 中,它带有一键式应用程序,它已经安装了 Postfix。我不知道我是否真的需要 Postfix,或者我是否需要 SendGrid 或 MailGun 之类的服务,或者两者兼而有之。
总之,我想了解一下我真正需要什么样的服务。谢谢!
【问题讨论】:
-
在 prod 中,您只需要一些 SMTP 凭据,就像在
development.rb中一样。您目前拥有的那些可能会工作一段时间,但最终您可能需要像 Sendgrid 这样的服务。我不知道你为什么需要 Postfix。 -
我在搜索时看到很多人在谈论Postfix,所以我问我是否需要它^^谢谢你的回答:)
标签: ruby-on-rails digital-ocean