action mailbox
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的更多相关文章
- RabbitMQ in Action (1): Understanding messaging
1. Consumers and producers Producers create messages and publish (send) them to a broker server (Rab ...
- Rails6.0 Beta版本1: Action Text的简单使用
主要功能是新增2个主要的框架Mailbox和action Text. 和2个重要的可扩展的升级: multiple databases support和parallel testing. Action ...
- Flink整合oozie shell Action 提交任务 带kerberos认证
最近这段时间一直在忙新集群迁移,上了最新的cdh6.3.0 于是Flink 提交遇到了许多的问题 还好有cloudera License 有了原厂的帮助和社区的伙伴,问题解决起来快了不少,手动滑稽 集 ...
- redux-amrc:用更少的代码发起异步 action
很多人说 Redux 代码多,开发效率低.其实 Redux 是可以灵活使用以及拓展的,经过充分定制的 Redux 其实写不了几行代码.今天先介绍一个很好用的 Redux 拓展-- redux-amrc ...
- 尝试asp.net mvc 基于controller action 方式权限控制方案可行性
微软在推出mvc框架不久,短短几年里,版本更新之快,真是大快人心,微软在这种优秀的框架上做了大量的精力投入,是值得赞同的,毕竟程序员驾驭在这种框架上,能够强力的精化代码,代码层次也更加优雅,扩展较为方 ...
- ASP.NET Core 中文文档 第四章 MVC(4.1)Controllers, Actions 和 Action Results
原文:Controllers, Actions, and Action Results 作者:Steve Smith 翻译:姚阿勇(Dr.Yao) 校对:许登洋(Seay) Action 和 acti ...
- java中Action层、Service层和Dao层的功能区分
Action/Service/DAO简介: Action是管理业务(Service)调度和管理跳转的. Service是管理具体的功能的. Action只负责管理,而Service负责实施. DAO只 ...
- SpringMVC的Action在同一时间里只允许同一个浏览器的单次进入?
最近用SpringMVC写了一个很简单的测试程序,代码如下: @Controller public class LongTimeTaskController { @RequestMapping(val ...
- 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 ...
随机推荐
- Python 筛选前缀文件
筛选某一文件下内具备某一前缀的文件: for file in files filename = os.path.listdir(file) if 'qianzhui--' in filename: # ...
- 在Linux虚拟机里开启Apache服务
首先第一步我们配置环境:把yum与Linux ping通 1.我们来下载apache服务,输入:yum install httpd * 2.安装完毕之后默认是死的,要给他启动一下,输入命令:syste ...
- Ubuntu16.04下使用pycharm导入scrapy框架
出现迷之问题,ubuntu终端下安装的库在pycharm中无法识别 后重新为pycharm安装相关package,得以使用 看到3即可 https://www.cnblogs.com/airnew/p ...
- 阿里云服务器 lnmp安装流程
nginx安装:wget http://nginx.org/download/nginx-1.12.2.tar.gztar zxvf nginx-1.12.2.tar.gzcd nginx-1.12. ...
- docker容器创建MariaDB镜像
基于commit命令方式创建 docker的安装 [root@test01 ~]# yum install docker [root@test01 ~]# systemctl enable docke ...
- [SDOI2008]石子合并 题解
题面 GarsiaWachs算法专门解决石子合并问题: 设一个序列是A[0..n-1],每次寻找最小的一个满足A[k-1]<=A[k+1]的k,那么我们就把A[k]与A[k-1]合并,并向前寻找 ...
- linux常用国内的免费源及其各别的配置方法.阿里源,epel源,搜狐网易等等..
国内的一些开源的平台收集的源,确实给我们提供了很多便利,所以我就稍微收集整理了一些常用的源和网址,我也不确定能用到什么时候!欢迎评论区留言! 搜狐开源镜像站 http://mirrors.sohu.c ...
- 技能节-AI人脸识别
我们收到技能节项目的通知是在两周之前,项目要求做个人脸评分系统. 两周时间写一个"人脸评分系统",好像时间比较紧了,还好我们完成了~这个项目是将摄像头捕获到的包含人脸的图像传输到百 ...
- Jmeter之设置线程组运行次数/时间
线程组的设置 线程组运行的次数=线程数*循环次数 Ramp-Up Period:表示启动时间 例如:线程数:10,循环次数:10,Ramp-Up Period:2 表示,这个线程组一共有100个线程( ...
- 【vue】computed 和 watch
计算属性:computed 监听多个变量且变量是在vue实例中(依赖某个变量,变量发生改变就会触发) 侦听器: watch 监听一个变量的变化 使用场景:watch(异步场景) ...