Action Mailer Basics和Action Mailbox Basics:邮件系统。

https://edgeguides.rubyonrails.org/action_mailbox_basics.html#exim

https://edgeguides.rubyonrails.org/action_mailer_basics.html


案例:

//在rails6 app分支
rails action_mailbox:install
//会生成一个app/mailboxes/application_mailbox.rb
//同时生成3个数据库table
rails g scaffold User email name
rails g scaffold Discussion title
rails g scaffold Comment user:references discussion:references body:text
rails db:migrate rails g mailbox Replies

上面代码使用rails6的新特性:action mailbox。它用于导航接收的email给控制器mailboxes。

它可以使用Postmark等常见API库。也可以直接通过它内建的Exim等APi来处理接收的email。

接收的email,通过使用action job来异步导航到一个或多个指定的mailboxes。

使用方法

安装-》配置api-》生成一个mailbox-》使用。具体见

继续:

class ApplicationMailbox < ActionMailbox::Base
# routing /something/i => :somewhere
routing /reply\+.+@reply.github.com/i => :replies
routing :all => :replies
end

routing方法,指定不同的邮件到特定的mailbox中。本例所有的邮件都被RepliesMailbox类进行处理。

⚠️一个方便的转化ruby 正则表达式语法网站:https://rubular.com/

继续:

在replies_mailbox.rb内编写方法。

mailbox集成了mail这个model。一个Ruby Mail Library.
https://github.com/mikel/mail/
提供了生成,转化,发送的一系列方法。

在gemfile.lock中可见:

actionmailbox (6.0.0.rc1)
  ...
  mail (>= 2.7.1)

class RepliesMailbox < ApplicationMailbox
MATCHER = /reply-(.+)@reply.example.com/i # mail => Mail object
# inbound_email => ActionMailbox::InboundEmail record #before_processing :ensure_user def process
return if user.nil? discussion.comments.create(
user: user,
# decoded方法返回对象的值(格式是string),即返回的是body,不是subject
body: mail.decoded
) end def user
@user ||= User.find_by(email: mail.from)
end def discussion
@discussion ||= Discussion.find(discussion_id)
end # recipients是mail的方法,find{}进行匹配找到第一个符合的data。
# recipients可以用to方法代替。
# recipient是一个string, 使用[Regexp, 1]得到捕捉到的字符。具体见正则表达式的捕捉()
def discussion_id
recipient = mail.recipients.find{|r| MATCHER.match?(r) }
recipient[MATCHER, 1]
end private def ensure_user
if user.nil?
bounce_with UserMailer.missing(inbound_email)
end
end
class ApplicationMailbox < ActionMailbox::Base
# routing /something/i => :somewhere
# routing /reply\+.+@reply.github.com/i => :replies
# routing :all => :replies
routing RepliesMailbox::MATCHER => :replies
end

然后添加user和discussion2条记录。

在discussions/show.html.erb增加下面的代码:

...
<h4>Comments</h4>
<div>
 <% @discussions.comments.each do |comment| %>
<strong><%= comment.user.name%></strong> commented:<br/>
<%= simple_format comment.body%>
<% end %>
</div>

完成!

测试

进入http://localhost:3000/rails/conductor/action_mailbox/inbound_emails

点击Deliver new inbound email链接,选择发送一封

这是source:

Date: Fri, 24 May 2019 11:30:37 +0800
From: 1@1.com
To: reply-1@reply.example.com
Message-ID: <5ce7655da43ed_c653feae01b5b9c714c@chentianweideiMac.local.mail>
In-Reply-To:
Subject: congratulations
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit hello boy!

action mailbox的更多相关文章

  1. RabbitMQ in Action (1): Understanding messaging

    1. Consumers and producers Producers create messages and publish (send) them to a broker server (Rab ...

  2. Rails6.0 Beta版本1: Action Text的简单使用

    主要功能是新增2个主要的框架Mailbox和action Text. 和2个重要的可扩展的升级: multiple databases support和parallel testing. Action ...

  3. Flink整合oozie shell Action 提交任务 带kerberos认证

    最近这段时间一直在忙新集群迁移,上了最新的cdh6.3.0 于是Flink 提交遇到了许多的问题 还好有cloudera License 有了原厂的帮助和社区的伙伴,问题解决起来快了不少,手动滑稽 集 ...

  4. redux-amrc:用更少的代码发起异步 action

    很多人说 Redux 代码多,开发效率低.其实 Redux 是可以灵活使用以及拓展的,经过充分定制的 Redux 其实写不了几行代码.今天先介绍一个很好用的 Redux 拓展-- redux-amrc ...

  5. 尝试asp.net mvc 基于controller action 方式权限控制方案可行性

    微软在推出mvc框架不久,短短几年里,版本更新之快,真是大快人心,微软在这种优秀的框架上做了大量的精力投入,是值得赞同的,毕竟程序员驾驭在这种框架上,能够强力的精化代码,代码层次也更加优雅,扩展较为方 ...

  6. ASP.NET Core 中文文档 第四章 MVC(4.1)Controllers, Actions 和 Action Results

    原文:Controllers, Actions, and Action Results 作者:Steve Smith 翻译:姚阿勇(Dr.Yao) 校对:许登洋(Seay) Action 和 acti ...

  7. java中Action层、Service层和Dao层的功能区分

    Action/Service/DAO简介: Action是管理业务(Service)调度和管理跳转的. Service是管理具体的功能的. Action只负责管理,而Service负责实施. DAO只 ...

  8. SpringMVC的Action在同一时间里只允许同一个浏览器的单次进入?

    最近用SpringMVC写了一个很简单的测试程序,代码如下: @Controller public class LongTimeTaskController { @RequestMapping(val ...

  9. No result defined for action com.lk.IndexAction and result success

    意图访问一个 /es/index.action 竟然出现: [SAE ] ERROR [05-11 13:54:32] [http-80-5] com.opensymphony.xwork2.util ...

随机推荐

  1. 无监督异常检测之卷积AE和卷积VAE

    尝试用卷积AE和卷积VAE做无监督检测,思路如下: 1.先用正常样本训练AE或VAE 2.输入测试集给AE或VAE,获得重构的测试集数据. 3.计算重构的数据和原始数据的误差,如果误差大于某一个阈值, ...

  2. 【计算机视觉】【并行计算与CUDA开发】GPU硬解码---DXVA

    前面介绍利用NVIDIA公司提供的CUVID库进行视频硬解码,下面将介绍利用DXVA进行硬解码. 一.DXVA介绍 DXVA是微软公司专门定制的视频加速规范,是一种接口规范.DXVA规范制定硬件加速解 ...

  3. 【计算机视觉】UCLA开源图像检测器

    UCLA (加州大学洛杉矶分校)发布了一个强大的图像检测软件的源码 ,该软件可以非常高速的检测每个图像的细节,例如可用于检测指纹和虹膜,或者用于自动驾驶.通过识别物体的边界进行提取.首先确定一个物体的 ...

  4. 【VS开发】CFormView

    原文地址:CFormView作者:罗纳尔多 CFormView是MFC使用无模式对话框的一个典型例子.CFormView是基于对话框模板创建的视,它的直接基类是CSrcollView,CSrcollV ...

  5. 基于 CentOS 7 搭建 Git服务器

    Git 是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目. ⒈安装依赖库和编译工具 1.安装依赖库 yum install -y curl-devel expat-devel ...

  6. DL4J中文文档/Keras模型导入/函数模型

    导入Keras函数模型 假设使用Keras的函数API开始定义一个简单的MLP: from keras.models import Model from keras.layers import Den ...

  7. windowsformshost mouse event not transmit to it's parent control

    in the case you can do it to fix: MouseEventArgs e = new MouseEventArgs(Mouse.PrimaryDevice, 0); e.R ...

  8. js变量浅拷贝 深拷贝

    js的变量分为简单数据类型和复杂数据类型(即引用类型). 简单数据类型在内存中占据着固定大小的空间,被保存在栈内存中,在简单数据类型中,当一个变量指向另一个变量时,只是创建了值的副本,两个变量只是占用 ...

  9. 作业8:常用java命令(二)

    一.jinfo(Configuration Info for Java) 1.功能:jinfo可以实时地查看和调整虚拟机的各项参数. 2.参数: 选项 作用 -flag name 打印改名字的VM设置 ...

  10. nop4.1学习ServiceCollectionExtensions(二)(ioc,ef,ef连接的实现)

    这个是获取程序路径,并初始化文件管理类 初始化插件管理 接下来就是注册服务和autoafc 在INopStartup配置sql连接,插件,mvc等 配置了sql连接 数据库的配置类 在AddAutoM ...