[英]How can my ruby app receive and parse emails with attachments?
I'd like to allow users to send an attachment via email to my app and have the attachment saved in their account. 我想允许用户通过电子邮件将附件发送到我的应用,并将附件保存在他们的帐户中。 Each user would have a unique and private email to send files to. 每个用户都有一个唯一的私人电子邮件来发送文件。 Something similar to Send to Dropbox is what I'm going for. 我想要的是类似于发送到Dropbox的东西。
I've been searching for a while now and can't find anything on how to approach this. 我已经搜索了一段时间,但找不到有关如何解决此问题的任何信息。 I'm guessing I'm searching the wrong thing. 我猜我在搜索错误的内容。 My ultimate question/s is/are: what are the components that go into a feature like this and what are the best gems to get the job done? 我的最终问题是:像这样的功能中包含哪些组件?完成这项工作的最佳方法是什么?
Thanks. 谢谢。
Use procmail or .forward
to pipe the incoming mail into your script. 使用procmail或.forward
将传入的邮件传递到脚本中。 The script comes in along STDIN
in mbox format (same as http: headers-blankline-body). 该脚本以mbox格式随STDIN
一起提供(与http:headers-blankline-body相同)。 I have no clue if there are gems, you can parse it manually with zero difficulty. 我不知道是否有宝石,您可以手动将其解析为零难度。
.forward
is way easier than procmail: on a system with sendmail, if you have an account named my_app
, put in ~my_app/.forward
the following line: .forward
比procmail更容易:在具有sendmail的系统上,如果您有一个名为my_app
的帐户,请在~my_app/.forward
输入以下行:
|/foo/bar/my_app/process_mail.rb
and it will pipe any mails coming to my_app@example.com
into /foo/bar/my_app/process_mail.rb
. 它将把发送到my_app@example.com
所有邮件发送到/foo/bar/my_app/process_mail.rb
。 Separate into headers and body, split headers into lines, find the From
and Subject
header, identify your user, do what you want with the rest. 分为标题和正文,将标题拆分为行,找到From
和Subject
标题,标识您的用户,其余部分做您想做的事情。
On Rails, I suspect you should do: 在Rails上,我怀疑您应该这样做:
|/usr/bin/env rails runner /foo/bar/my_app/process_mail.rb
or if using Bundler, 或者如果使用Bundler,
|/usr/bin/env bundle exec rails runner /foo/bar/my_app/process_mail.rb
so you get your Rails environment for your script. 这样您就可以获得脚本的Rails环境。
Below is a detailed explanation on how to intercept emails with rails: 以下是有关如何使用Rails拦截电子邮件的详细说明:
http://steve.dynedge.co.uk/2010/09/07/incoming-email-in-rails-3-choosing-the-right-approach/ http://steve.dynedge.co.uk/2010/09/07/incoming-email-in-rails-3-choosing-the-right-approach/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.