Reputation: 238707
I am creating an app that will be used to send emails. I don't need to use regular mailers and view templates because I will simply be receiving the data that will be used to generate the email. However, I assume that there are some benefits to using ActionMailer
instead of interacting with SMTP
directly. I ran into issues while trying to instantiate a new instance of ActionMailer::Base
. How can I use ActionMailer
by itself without having to define a new class that extends ActionMailer::Base
?
Upvotes: 2
Views: 2735
Reputation: 981
The underlying functionality for ActionMailer is provided by the mail gem. This allows you to send mail very simply, e.g:
Mail.deliver do
from '[email protected]'
to '[email protected]'
subject 'Here is the image you wanted'
body File.read('body.txt')
add_file :filename => 'somefile.png', :content => File.read('/somefile.png')
end
It supports delivery by all the same methods that ActionMailer does.
Upvotes: 7
Reputation: 49703
Ah, I think I better understand now. Basically you're looking for a simple one-liner similar to php's mail(), right?
If so, ActionMailer isn't making sense to you because it really isn't the right tool for this job.
I think the winner for you is a ruby gem called Pony: https://github.com/benprew/pony
example:
Pony.mail(:to => '[email protected]', :from => '[email protected]', :subject => 'hi', :body => 'Hello there.')
Upvotes: 2
Reputation: 6837
This is a most basic solution. You have to change smtp settings and the hard coded values to variables etc. This way you do not need to use a View. If you still want to use ERB I suggest you check out Railscast 206.
Just change this code, put it in a file like "test_email.rb" and call it with ruby test_email.rb
require 'action_mailer'
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:user_name => "testuser123",
:password => "secret",
:authentication => "plain",
:enable_starttls_auto => true
}
class TestMailer < ActionMailer::Base
default :from => "[email protected]"
# def somemethod()
# mail(:to => "John Doe <[email protected]>", :subject => "TEST", :body => "HERE WE GO!!!")
# end
def mail(args)
super
end
end
# TestMailer.somemethod().deliver
TestMailer.mail(:to => "John Doe <[email protected]>", :subject => "TEST", :body => "HERE WE GO!!!")
Upvotes: 2