ruby-on-rails - 如何在 Rails 上发送电子邮件
问题描述
我正在尝试在我的应用程序中添加一个简单的电子邮件表单,以便我可以接收来自用户的电子邮件。我遵循了一些教程,最终成功地在开发模式下向自己发送电子邮件。我是这样做的:
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 之类的服务,或者两者兼而有之。
所以总结一下,我想了解一下我真正需要什么样的服务。谢谢!
解决方案
gmail 可以正常工作,只需确保您为应用程序设置的安全性较低,这是参考
以下是适用于我的应用程序的设置可能可以作为您的参考:
config.action_mailer.default_url_options = { :host => "ip_address_from_digital_ocean" }
config.action_mailer.delivery_method=:smtp
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = { :host => 'host_name' }
# Gmail SMTP server setup
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:enable_starttls_auto => true,
:port => 587,
:authentication => :plain,
:user_name => "username@gmail.com",
:password => password
}
推荐阅读
- java - 30 秒后来自 SpeechClient 的 CancellationException
- python - beautifulsoup for-if 循环提取
- php - iOS Swift 5 - 通过调用 PHP 函数将文件上传到服务器
- parameters - Panel + Param 自定义 MultiFileSelector 小部件
- types - 如果无法在编译时确定,如何检测 Crystal 中的数据类型?
- java - 使用来自 POST 请求正文的 Java REST API 解析的有效嵌套 JSON 结构
- c# - SMO 备份失败并出现错误 1827(完整数据库)
- angular - 用角度+2打开插座的最佳方法是什么?
- javascript - 使用拍摄的快照作为输入文件中的文件
- c# - 在 aspnet core 2 中使用静态文件