Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionMailer 3 without Rails

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      => "[email protected]",             :from    => "[email protected]",             :subject => "testing mail"     )   end end  BugMailer.daily_email.deliver 

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

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?

like image 820
Spencer R Avatar asked Feb 09 '11 22:02

Spencer R


1 Answers

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

file 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      => "[email protected]",    :password       => "passw0rd",    :enable_starttls_auto => true   } ActionMailer::Base.view_paths= File.dirname(__FILE__)  class Mailer < ActionMailer::Base    def daily_email     @var = "var"      mail(   :to      => "[email protected]",             :from    => "[email protected]",             :subject => "testing mail") do |format|                 format.text                 format.html     end   end end  email = Mailer.daily_email puts email email.deliver 

file mailer/daily_email.html.erb

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

file mailer/daily_email.text.erb

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

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

like image 121
Augusto Avatar answered Oct 04 '22 13:10

Augusto