不带Rails的ActionMailer 3 [英] ActionMailer 3 without Rails

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

问题描述

我正在编写一个小的Ruby程序,该程序将从数据库中提取记录并每天发送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浪费时间吗?

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?

推荐答案

经过认真的调试后,有没有办法获得类似于Rails的环境?我找到了配置方法。

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

文件 mailer.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

文件 mailer / daily_email.html.erb

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

文件 mailer / 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天全站免登陆