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 ...
随机推荐
- 【Adobe Air程序开发】eclipse安装flash builder 4.7插件以及java、flex整合开发
看了看网上不少文章,发现很多内容都是很老的,没法用.故把自己的安装过程记录下来,方便以后使用 1.在这里,eclipse使用最新版eclipse juno 3.7 2.在adobe官网https:// ...
- prometheus 监控 jar应用服务 + 修改监听IP和端口
1.修改服务的启动脚本 [root@do1cloud01 init.d]# vim learn-school nohup ${JAVA_HOME}/bin/java -javaagent:/usr/l ...
- Reactor系列(一)基本概念
基本概念 视频讲解:https://www.bilibili.com/video/av78731069/ 关注公众号,坚持每天3分钟视频学习
- pycharm 安装好,只要三部! 超级简单教程!
pycharm的安装,确实比较麻烦,所以特意做了一期简单版本的安装教程,跟着教程走...只要三部! →下载 链接:https://pan.baidu.com/s/1JxZgAhPVKAIoM1_jpD ...
- 并不对劲的复健训练-bzoj5253:loj2479:p4384:[2018多省联考]制胡窜
题目大意 给出一个字符串\(S\),长度为\(n\)(\(n\leq 10^5\)),\(S[l:r]\)表示\(S_l,S_{l+1}...,S_r\)这个子串.有\(m\)(\(m\leq 3\t ...
- unity ugui image更换图片
1:利用资源加载方式 using UnityEngine; using System.Collections; using UnityEngine.UI; public class ChangeIma ...
- github常用搜索技巧
1.在项目名称,readme文件和描述中包含关键字seckill的项目seckill in:name,readme,description 2.fork大于500,stars大于500springbo ...
- luogu题解 P3709 【大爷的字符串题】
题目链接: https://www.luogu.org/problemnew/show/P3709 思路: 首先我是没读懂题目的,浏览了讨论区的dalao发现才知道就是求区间众数的出现次数. 然后肯定 ...
- O034、 Nova Pause / Resume Instance 操作详解
参考https://www.cnblogs.com/CloudMan6/p/5496825.html 本节通过日志详细分析 Nova Pause / Resume 操作. 有时需要短时间暂停 ...
- sccrapy 爬虫框架网数据库储存时去重的问题
from scrapy.exceptions import DropItem #导入异常处理模块 class Baidu03Pipeline(object): def __init__(self): ...