ApplicationMailer default from title not email address? - ruby-on-rails

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

Related

ActionMailer with Rails: uninitialized constant UserMailer

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.

Multiple default from address in ActionMailer Rails

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

Rails mailer alternate from address

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

ActionMailer are the methods static?

class UserMailer < ActionMailer::Base
default from: 'notifications#example.com'
def welcome_email(user)
#user = user
#url = 'http://example.com/login'
mail(to: #user.email, subject: 'Welcome to My Awesome Site')
end
end
To send the email I write UserMailer.welcome_email(#user).deliver so my question is: are the methods declared in the Mailer Controller static? Becausei I call welcome_email on a class, so I am cunfused
Not really, but in practice it works as if they were. You have the answer here: How can you call class methods on mailers when they're not defined as such? .
Basically, the Mailer has a method_missing defined that if the method called doesn't exist, it will create an instance of the mailer and call the method on it.

Rails mailer views in separated directory

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

Resources