没有 Rails 的 ActionMailer 3 [英] ActionMailer 3 without Rails

查看:23
本文介绍了没有 Rails 的 ActionMailer 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个小型 Rub​​y 程序,它将从数据库中提取记录并每天发送一封 HTML 电子邮件.我正在尝试为此使用 ActionMailer 3.0.3,但我遇到了问题.到目前为止,我在 Rails 之外使用 ActionMailer 所做的所有搜索都适用于版本 3 之前的版本.有人可以指出我在哪里可以找到有关如何执行此操作的资源的正确方向吗?到目前为止,我的邮件程序文件是这样的:

I'm writing a small Ruby program that will pull records from a database and send an HTML email daily. I'm attempting to use ActionMailer 3.0.3 for this, but I'm running in to issues. All the searching I've done so far on using ActionMailer outside of Rails applies to versions prior to version 3. Could someone point me in the right direction of where to find resources on how to do this? Here's where I am so far on my mailer file:

# lib/bug_mailer.rb
require 'action_mailer'

ActionMailer::Base.delivery_method = :file

class BugMailer < ActionMailer::Base
  def daily_email
    mail(
            :to      => "example@mail.com",
            :from    => "example@mail.com",
            :subject => "testing mail"
    )
  end
end

BugMailer.daily_email.deliver

我绝对不知道该把我的观点放在哪里.我试图告诉 ActionMailer 我的模板在哪里都失败了.

I'm definitely stuck on where to put my views. Every attempt I've made to tell ActionMailer where my templates are has failed.

我想我也应该问一下是否有不同的方法来完成这个程序.基本上,此时我正在从头开始做所有事情.很明显,是什么让 Rails 很棒是因为它的约定,那么尝试自己使用 Rails 的一部分是在浪费时间吗?有没有办法在不创建成熟的 Rails 应用的情况下获得类似 Rails 的环境?

I guess I should also ask if there's a different way to go about accomplishing this program. Basically, I'm doing everything from scratch at this point. Obviously what makes Rails awesome is it's convention, so is trying to use parts of Rails on their own a waste of time? Is there a way to get the Rails-like environment without creating a full-blown Rails app?

推荐答案

经过一番认真的调试,我找到了如何配置它.

After some serious debugging, I found how to configure it.

文件 ma​​iler.rb

require 'action_mailer'

ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
   :address   => "smtp.gmail.com",
   :port      => 587,
   :domain    => "domain.com.ar",
   :authentication => :plain,
   :user_name      => "test@domain.com.ar",
   :password       => "passw0rd",
   :enable_starttls_auto => true
  }
ActionMailer::Base.view_paths= File.dirname(__FILE__)

class Mailer < ActionMailer::Base

  def daily_email
    @var = "var"

    mail(   :to      => "myemail@gmail.com",
            :from    => "test@domain.com.ar",
            :subject => "testing mail") do |format|
                format.text
                format.html
    end
  end
end

email = Mailer.daily_email
puts email
email.deliver

文件 ma​​iler/daily_email.html.erb

<p>this is an html email</p>
<p> and this is a variable <%= @var %> </p>

文件 ma​​iler/daily_email.text.erb

this is a text email

and this is a variable <%= @var %>

好问题!它帮助我更多地了解 Rails 3 的工作原理:)

Nice question! It helped me to understand a bit more how Rails 3 works :)

这篇关于没有 Rails 的 ActionMailer 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆