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 ...
随机推荐
- 【并行计算-CUDA开发】Apple's OpenCL——再谈Local Memory
在OpenCL中,用__local(或local)修饰的变量会被存放在一个计算单元(Compute Unit)的共享存储器区域中.对于nVidia的GPU,一个CU可以被映射为物理上的一块SM(Str ...
- 【图像处理】H.264有四种画质级别
H.264有四种画质级别,分别是BP.EP.MP.HP,想要说明H.264 HP与H.264 MP的区别就要讲到H.264的技术发展了 H.264 视频编码技术在视频采集.后期制作等网络视频服务领域的 ...
- airflow迁移
airflow迁移:airflow.cfg文件可以copydbinit时改数据参数 #airflow震乾源码copy:/data/venv/lib/python3.6/site-packages/ai ...
- beautifulsoap爬虫
从html文件读 from bs4 import BeautifulSoup html_doc="文件地址" html_file=open(html_doc,"r&quo ...
- centos 6.x下jira显示饼图乱码解决方法
由于最近业务反馈jira出饼图时出现乱码现象,无法显示中文. 1. 由于平时用的yum源没有相关安装包,这里推荐暂时修改yum源如下: [epel] name=Extra Packages - $ba ...
- RabbitMQ 的安装配置
环境:Ubuntu16 linux系统,ERlang语言的源码包 :otp_src_22.0.tar.gz,rabbitMQ安装包:rabbitmq-server-generic-unix-3.7.1 ...
- mybatis-plus配置多数据源invalid bound statement (not found)
mybatis-plus配置多数据源invalid bound statement (not found) 错误原因 引入mybatis-plus应该使用的依赖如下,而不是mybatis <de ...
- java水题集
POJ - 1220 进制转换 import java.io.*; import java.util.*; import java.math.*; public class Main { public ...
- Python【print函数】
下面是 print函数的一种用法,用逗号隔开,可在同一行打印不同类型的数据.x = input('请你输入被除数:')y = input('请你输入除数:')z = float(x)/float(y) ...
- 【python tkinter】对于窗口存在的认识
今天写了messagebox(信息提示框) 和 inputbox(输入框). 对于 [窗口] 有了新的认识... 发现一个奇怪的事情(虽然原因很简单啦······) def msg_box(titl ...