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. flex 遇上white-space:nowrap的2种解决方法

    需求:使用flex布局,超出部分想使用点点点显示 一.方法1使用min-width:0 效果: HTML代码如下: <div class="team-body"> &l ...

  2. Vuecli 3.0 项目自定义添加静态目录,支持在index.html引入

    参考链接:https://blog.csdn.net/qq_15253407/article/details/89491255

  3. Hbase和Hadoop的内存参数调优 + 前端控制台

    1.hadoop的内存配置调优 mapred-site.xml的内存调整 <property> <name>mapreduce.map.memory.mb</name&g ...

  4. Linux三剑客之sed深度实践讲解(上)

    sed sed 是Stream Editor(流编辑器)缩写,是操 作过滤和转换文本内容的强大工具.常用功能有增删改查,过滤,取行. 2.增删改查 2.1 增 a 追加文本到指定行后 i 插入文本到指 ...

  5. 9.centos7 安装mysql

    sudo yum install libaio # 安装依赖包 检查 MySQL 是否已安装 sudo yum list installed | grep mysql 也可以通过命令 rpm -qa| ...

  6. Linux常用安装配置

    一.创建.删除.分组 创建用户 命令:useradd  用户名 或    adduser 用户名 注意:只有root用户才能创建新用户 例如,创建一个名为zhangsan的用户 使用passwd命令为 ...

  7. Bean属性复制,字段名可不同,字段类型不同需要自行处理

    @Setter @Getter public class SourceA { private String name; private String text; public SourceA(Stri ...

  8. Win32汇编语言语法基础

    汇编语言(assembly language)是一种用于电子计算机.微处理器.微控制器或其他可编程器件的低级语言,亦称为符号语言.在汇编语言中,用助记符(Mnemonics)代替机器指令的操作码,用地 ...

  9. 高性能MySQL3_笔记1_Mysql的架构与历史

    第一层:连接处理.授权认证.安全 第二层:mysql的核心功能,包括查询解析.分析.优化.缓存以及所有的内置函数(例如日期.加密.数学函数), 所有跨存储引擎的功能都在这一层实现:存储过程.触发器.视 ...

  10. id和class的区别

    id和class是定义css样式用到的,不同的是定义样式时的写法不一样,使用id选择样式时,定义的格式为 #main{width:20px;} ,使用class时用到的是 .main{width:20 ...