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

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

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


案例:

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

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

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

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

使用方法

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

继续:

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

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

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

继续:

在replies_mailbox.rb内编写方法。

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

在gemfile.lock中可见:

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

  1. class RepliesMailbox < ApplicationMailbox
  2. MATCHER = /reply-(.+)@reply.example.com/i
  3.  
  4. # mail => Mail object
  5. # inbound_email => ActionMailbox::InboundEmail record
  6.  
  7. #before_processing :ensure_user
  8.  
  9. def process
  10. return if user.nil?
  11.  
  12. discussion.comments.create(
  13. user: user,
  14. # decoded方法返回对象的值(格式是string),即返回的是body,不是subject
  15. body: mail.decoded
  16. )
  17.  
  18. end
  19.  
  20. def user
  21. @user ||= User.find_by(email: mail.from)
  22. end
  23.  
  24. def discussion
  25. @discussion ||= Discussion.find(discussion_id)
  26. end
  27.  
  28. # recipients是mail的方法,find{}进行匹配找到第一个符合的data。
    # recipients可以用to方法代替。
    # recipient是一个string, 使用[Regexp, 1]得到捕捉到的字符。具体见正则表达式的捕捉()
  29. def discussion_id
  30. recipient = mail.recipients.find{|r| MATCHER.match?(r) }
  31. recipient[MATCHER, 1]
  32. end
  33.  
  34. private
  35.  
  36. def ensure_user
  37. if user.nil?
  38. bounce_with UserMailer.missing(inbound_email)
  39. end
  40. end
  1. class ApplicationMailbox < ActionMailbox::Base
  2. # routing /something/i => :somewhere
  3. # routing /reply\+.+@reply.github.com/i => :replies
  4. # routing :all => :replies
  5. routing RepliesMailbox::MATCHER => :replies
  6. end

然后添加user和discussion2条记录。

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

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

完成!

测试

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

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

这是source:

  1. Date: Fri, 24 May 2019 11:30:37 +0800
  2. From: 1@1.com
  3. To: reply-1@reply.example.com
  4. Message-ID: <5ce7655da43ed_c653feae01b5b9c714c@chentianweideiMac.local.mail>
  5. In-Reply-To:
  6. Subject: congratulations
  7. Mime-Version: 1.0
  8. Content-Type: text/plain;
  9. charset=UTF-8
  10. Content-Transfer-Encoding: 7bit
  11.  
  12. 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. 【Adobe Air程序开发】eclipse安装flash builder 4.7插件以及java、flex整合开发

    看了看网上不少文章,发现很多内容都是很老的,没法用.故把自己的安装过程记录下来,方便以后使用 1.在这里,eclipse使用最新版eclipse juno 3.7 2.在adobe官网https:// ...

  2. prometheus 监控 jar应用服务 + 修改监听IP和端口

    1.修改服务的启动脚本 [root@do1cloud01 init.d]# vim learn-school nohup ${JAVA_HOME}/bin/java -javaagent:/usr/l ...

  3. Reactor系列(一)基本概念

    基本概念 视频讲解:https://www.bilibili.com/video/av78731069/ 关注公众号,坚持每天3分钟视频学习

  4. pycharm 安装好,只要三部! 超级简单教程!

    pycharm的安装,确实比较麻烦,所以特意做了一期简单版本的安装教程,跟着教程走...只要三部! →下载 链接:https://pan.baidu.com/s/1JxZgAhPVKAIoM1_jpD ...

  5. 并不对劲的复健训练-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 ...

  6. unity ugui image更换图片

    1:利用资源加载方式 using UnityEngine; using System.Collections; using UnityEngine.UI; public class ChangeIma ...

  7. github常用搜索技巧

    1.在项目名称,readme文件和描述中包含关键字seckill的项目seckill in:name,readme,description 2.fork大于500,stars大于500springbo ...

  8. luogu题解 P3709 【大爷的字符串题】

    题目链接: https://www.luogu.org/problemnew/show/P3709 思路: 首先我是没读懂题目的,浏览了讨论区的dalao发现才知道就是求区间众数的出现次数. 然后肯定 ...

  9. O034、 Nova Pause / Resume Instance 操作详解

    参考https://www.cnblogs.com/CloudMan6/p/5496825.html   本节通过日志详细分析 Nova Pause / Resume 操作.   有时需要短时间暂停  ...

  10. sccrapy 爬虫框架网数据库储存时去重的问题

    from scrapy.exceptions import DropItem #导入异常处理模块 class Baidu03Pipeline(object): def __init__(self): ...