Actionmailer 3 Without Rails

ActionMailer 3 without Rails

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 => "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

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 :)

ActionMailer without Rails - views not being picked up

After migration from Rails 2.x -> 3.x I had the same problem (a bit different setup though). My html emails had no body. I moved my mailer class from models folder into mailers folder and changed my view file names:

file_name.text.html.erb

=>

file_name.html.erb

I hope this helps.

ActionMailer without template and multipart email

class TestMailer < ActionMailer::Base
def welcome_email
mail(to: 'example@example.com', subject: 'Welcome to My Awesome Site') do |format|
format.html { render html: '<h1>Welcome XYZ!</h1>'.html_safe }
format.text { render plain: 'Welcome XYZ' }
end
end
end

To use it call: TestMailer.welcome_email.deliver_now.

Documentation, section 2.4

Rails without actionmailbox and actionmailer

The roadmap for such a feature does not look good https://github.com/wycats/bundler/issues/143.

Since you can use each of rails gems independently and they declare their own dependencies, declaring what you need in the Gemfile should be ok.

# Gemfile

# gem "rails", "~> 7.0.2.3"

rails_version = "~> 7.0.2.3"
gem "activesupport", rails_version
gem "actionpack", rails_version
gem "actionview", rails_version
gem "activemodel", rails_version
gem "activerecord", rails_version
# gem "actionmailer", rails_version
gem "activejob", rails_version
gem "actioncable", rails_version
gem "activestorage", rails_version
# gem "actionmailbox", rails_version
gem "actiontext", rails_version
gem "railties", rails_version

# ...

Rails 3 - abandon sending mail within ActionMailer action

Instead put your conditions in the place where you are making the call to SomeMailer.some_emails.deliver!

Actionmailer not delivering mail, with rails 3

Somewhat late, but nevertheless maybe this will save someone a few hours of head banging. This is probably only relevant to sending emails from gmail.
First, in order to help debugging the situation, set the following line in development.rb to true (assuming you're in development mode):

config.action_mailer.raise_delivery_errors = true

This will make ActionMailer not to silently ignore errors.
When I did that, I realized gmail is refusing my username and password.
I then went to my configuration file where I put all the Action Mailer config directives (for me it was in development.rb, there is probably a better place to put it), and noticed that :user_name was set to "admin" rather than "admin@thedomain.com". Changing it solved the problem. Here is my corrected part of development.rb:

  config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => 'thedomain.com',
:user_name => 'admin@thedomain.com',
:password => '<password>',
:authentication => 'plain',
:enable_starttls_auto => true }

References:

http://forums.pragprog.com/forums/43/topics/541
http://edgeguides.rubyonrails.org/action_mailer_basics.html

How can I send mail with rails without a template?

The simplest way to send mail in rails 3 without a template is to call the mail method of ActionMailer::Base directly followed by the deliver method,

For ex, the following would send a plain text e-mail:

ActionMailer::Base.mail(
from: "me@example.com",
to: "you@example.com",
subject: "test",
body: "test"
).deliver

http://api.rubyonrails.org/classes/ActionMailer/Base.html#method-i-mail gives you all the header options and also ideas about the how to send a multipart/alternative email with text/plain and text/html parts directly.

Rails 3: action mailer not usng the :from = parameters

Gmail typically doesn't let you set the from field for sending emails. For this reason, a lot of people switch to other mail services like SendGrid.

If you google "Rails gmail from field" you'll unfortunately see all the other people who have struggled with this in the past.



Related Topics



Leave a reply



Submit