Below is the normal format I copied from http://api.rubyonrails.org/
class ApplicationMailer < ActionMailer::Base
default from: 'from#example.com'
layout 'mailer'
end
But what I want is to be able to have multiple address that it send form.
I tried to call a method, for example:
class ApplicationMailer < ActionMailer::Base
default from: address
layout 'mailer'
def address
Apartment::Tenant.current_tenant == "org" ? "custom#email.com" : "from#example.com"
end
end
When I call that method it returns
<ActionMailer::Base::NullMail:0x007fbefe0eb388>
and not the string that I want.
Try below code:
class ApplicationMailer < ActionMailer::Base
default from: Apartment::Tenant.current_tenant == "org" ? "custom#email.com" : "from#example.com"
layout 'mailer'
end
Related
Getting this error in console when I try UserMailer.welcome.deliver_now
(irb):6:in `<main>': uninitialized constant UserMailer (NameError)
UserMailer.welcome.deliver_now
^^^^^^^^^^
Did you mean? UserMailerPreview
And before I was getting this NameError, the console was throwing the error that the method welcome was undefined.
enter image description here
This is my file structure.
app/mailers/application_mailer.rb:
class ApplicationMailer < ActionMailer::Base
default from: "from#example.com"
layout "mailer"
end
app/mailers/user_mailer.rb:
class UserMailer < ApplicationMailer
def welcome
#greeting = "Hi"
mail to: "to#example.org"
end
end
app/user_mailer/welcome_email.html.erb:
<h1><%= #greeting %>,</h1>
<p>Example Email Body</p>
app/user_mailer/welcome_email.text.erb:
<%= #greeting %>,
Example Email Body
You say:
app/mailers/application_mailer.rb:
class ApplicationMailer < ActionMailer::Base
default from: "from#example.com"
layout "mailer"
end
app/mailers/user_mailer.rb:
class UserMailer < ApplicationMailer
def welcome
#greeting = "Hi"
mail to: "to#example.org"
end
end
But that isn't true. Those files are in app/views/mailers according to your screenshot. Rails isn't able to autoload them properly. Move them to the correct location and try again.
I'm trying to get access to some of my application_helper methods within my mailer, but nothing seems to be working from these SO posts:
View Helpers in Mailers
Access Helpers from mailer
In app/helpers/application_helper.rb I have the following:
module ApplicationHelper
def get_network_hosts
.. stuff get get a #network_hosts object
end
end
In my mailer at app/mailers/user_notifier.rb I have the following:
class UserMailer < ActionMailer::Base
default from: "Support <support#me.co>"
add_template_helper(ApplicationHelper)
def trial_notifier(user)
get_network_hosts
#user = user
#total = user.company.subscription.per_device_charge * #network_hosts.count
if #total < 501
#message = "You'd pay #{#total}/month if you converted to full-access now!"
else
#message = "You'd pay #{#total}/month if you converted to full-access now, but we have a better deal for you!"
end
#url = edit_user_registration_url
mail(to: #user.email, subject: 'What's up?')
end
end
In my mailer I've tried all of the suggestions in the above SO posts, but I'm still getting this error:
`NameError: undefined local variable or method `get_network_hosts' for #<UserMailer:0x007fe756c67c18`>
I'm currently using Rails 4.1.7.
So what do I have to actually do to be able to use my application_helper methods within a mailer?
You can try to do this as following:
In your mailer at app/mailers/user_notifier.rb:
class UserMailer < ActionMailer::Base
default from: "Support <support#me.co>"
helper :application
or you can try this:
helper ApplicationHelper
default from works with an email address, but when a recipient receives an email I want the title to be "Live to Challenge" not "livetochallenge.com"
class ApplicationMailer < ActionMailer::Base
default from: "livetochallenge.com#gmail.com" #This Line Needs An Email Address to Work
layout 'mailer'
end
How can Action Mailer send with livetochallenge.com#gmail.com, but be shown as "Live to Challenge"?
default from: '"Live to Challenge" <livetochallenge.com#gmail.com>'
class ApplicationMailer < ActionMailer::Base
default from: "Live to Challenge <livetochallenge.com#gmail.com>"
layout 'mailer'
end
I can set a default from address in a rails like so;
class UserMailer < ActionMailer::Base
default :from => "\"Company\" <company#example.com>"
def custom_address(user)
# I want to set the from address here
mail(to: user.email, subject: 'Custom from address')
end
end
but how do I set a custom address for a different method? I can't see it listed anywhere in the docs
I may be wrong, but I believe you can just override the from within the mail method.
class UserMailer < ActionMailer::Base
default :from => "\"Company\" <company#example.com>"
def custom_address(user)
# I want to set the from address here
mail(to: user.email, subject: 'Custom from address', from: 'asdf#other.com')
end
end
I have small organizatoric issue, in my application I have 3 mailer User_mailer, prduct_mailer, some_other_mailer and all of them store their views in app/views/user_mailer ...
I will want to have a subdirectory in /app/views/ called mailers and put all in the folders user_mailer, product_mailer and some_other_mailer.
Thanks,
You should really create an ApplicationMailer class with your defaults and inherit from that in your mailers:
# app/mailers/application_mailer.rb
class ApplicationMailer < ActionMailer::Base
append_view_path Rails.root.join('app', 'views', 'mailers')
default from: "Whatever HQ <hq#whatever.com>"
end
# app/mailers/user_mailer.rb
class UserMailer < ApplicationMailer
def say_hi(user)
# ...
end
end
# app/views/mailers/user_mailer/say_hi.html.erb
<b>Hi #user.name!</b>
This lovely pattern uses the same inheritance scheme as controllers (e.g. ApplicationController < ActionController::Base).
I so agree with this organization strategy!
And from Nobita's example, I achieved it by doing:
class UserMailer < ActionMailer::Base
default :from => "whatever#whatever.com"
default :template_path => '**your_path**'
def whatever_email(user)
#user = user
#url = "http://whatever.com"
mail(:to => user.email,
:subject => "Welcome to Whatever",
)
end
end
It is Mailer-specific but not too bad!
I had some luck with this in 3.1
class UserMailer < ActionMailer::Base
...
append_view_path("#{Rails.root}/app/views/mailers")
...
end
Got deprecation warnings on template_root and RAILS_ROOT
If you happen to need something really flexible, inheritance can help you.
class ApplicationMailer < ActionMailer::Base
def self.inherited(subclass)
subclass.default template_path: "mailers/#{subclass.name.to_s.underscore}"
end
end
You can put the templates wherever you want, but you will have to specify it in the mailer. Something like this:
class UserMailer < ActionMailer::Base
default :from => "whatever#whatever.com"
def whatever_email(user)
#user = user
#url = "http://whatever.com"
mail(:to => user.email,
:subject => "Welcome to Whatever",
:template_path => '**your_path**',
)
end
end
Take a look at 2.4 Mailer Views for more info.
Easy Solution: Specify Path in ApplicationMailer
class ApplicationMailer < ActionMailer::Base
layout 'mailer'
prepend_view_path "app/views/mailers" # <---- dump your views here
end