good day!
I have an application mailer and I want to call the application record to put it on the mailer. Is it possible to do this?
class UserMailer < ApplicationMailer
def verification_email
#user = params[:user]
#token = #user.verification_token
email_template = EmailTemplate.where(category_id: 1)
#subject = email_template.subject
#greetings = email_template.greetings
#content = email_template.content
#closing = email_template.closing
mail(to: #user.email, subject: #subject)
end
end
EmailTemplate is the Application record that I want to call.
It should work but you should get a specific template, right now you are getting an ActiveRecord::Relation
change EmailTemplate.where(category_id: 1)
to
EmailTemplate.where(category_id: 1).first or EmailTemplate.find_by(category_id: 1)
Im getting an error when trying to send an email. Not to sure why but here is my code in my controller and mailer
Here is my controller code below
class Invitation::InvitesController < ApplicationController
def invite_provider
#patient = Patient.find_by_id(invite_params[:invitable_id])
recipient = params[:email]
InviteMailer.provider_invite(recipient).deliver_now
flash[:success] = "An email has been sent to"
redirect_back(fallback_location: root_path)
end
end
Here is my mailer code
class InviteMailer < ApplicationMailer
def provider_invite(recipient)
#recipient = recipient
mail(
to: recipient[:email],
subject: I18n.t('provider_invite_subject')
)
end
end
At the call to mail, in the to option, you're basically sending params[:email][:email]. I don't think that's what you want.
recipient = params[:email]
and then
to: recipient[:email],
You have called InviteMailer.provider_invite(recipient) where recipient is email inside controller.
You can change in controller,
InviteMailer.provider_invite(email: recipient)
and then,
class InviteMailer < ApplicationMailer
def provider_invite(attr)
#recipient = attr[:email]
mail(
to: #recipient,
subject: I18n.t('provider_invite_subject')
)
end
end
And error is due to your params o not have email key, so recipient is nil passed through controller
I am iterating over an array of subscriber ids to send each subscriber an email, but for some reason not all subscribers are getting the email.
Here is the controller calling the model method:
class Admin::EmailDigestsController < ApplicationController
def send_digest
article_ids = params[:article_ids]
subject = params[:subject]
EmailDigest.send_email_digest(article_ids, subject)
redirect_to email_digests_path
end
end
And here is email_digest.rb:
class EmailDigest < ActiveRecord::Base
def self.send_email_digest(article_ids, subject)
subscribers = EmailDigest.where.not(status: "Inactive").ids
subscribers.each do |subscriber|
DigestMailer.weekly_digest(subscriber, article_ids, subject).deliver_later
end
end
end
And here is digest_mailer.rb:
class DigestMailer < ActionMailer::Base
default from: "secret#secret.com"
def weekly_digest(subscriber, article_ids, subject)
#subscriber = EmailDigest.find_by_id(subscriber)
#subject = subject
articles_arr = []
article_ids.each do |f|
articles_arr << Article.find_by_id(f)
end
#articles = articles_arr
mail(to: #subscriber.email,
subject: subject)
end
end
I also have sidekiq set up and it works sufficiently to send at least one email, but perhaps there is something wrong with the way errors are handled.
Thanks for any help
I've created a basic mailer and am not able to see the preview using the following path: http://localhost:3000/rails/mailers/calendar_mailer/calendar_email
Here is my mailer, preview, and email body code:
calendar_mailer.rb:
class CalendarMailer < ActionMailer::Base
default from: "notifications#cogsmart.com"
def calendar_email(user)
#user = user
mail(to: #user.email, subject: 'Your Cogsmart To Do List')
end
end
calendar_mailer_preview.rb:
class CalendarMailerPreview < ActionMailer::Preview
def calendar_email
CalendarMailer.calendar_email(User.first)
end
end
calendar_email.html.erb:
<h1>Thanks #user.name for using Cogsmart, here's your calendar</h1>
As described here
The preview needs to go in the test/mailers/previews folder
Ok I have seen many discussions about customizing devise email subject but none seems to solve what I want. Currently my confirmation email subject reads "Confirm your Qitch.com account". I want to customize this email subject and add a dynamic value of a user's name in it such that if user ALEX signs up for an account, he should get an email address with the subject, Welcome ALEX, confirm your Qitch.com account. How can I achieve this in devise?
devise.en.yml
mailer:
confirmation_instructions:
subject: 'Confirm your Qitch.com account'
reset_password_instructions:
subject: 'Reset your Qitch.com password'
unlock_instructions:
subject: 'Unlock your Qitch.com account'
Lastly, how do I add a name in the reply address or from address, currently when you receive the mail, it says sender: no-reply#qitch.com Is there a way I can customize it to Qitch
Thanks
I see that no answers are clean enough so I would like to make a short summary here.
First of all, you have to tell Devise you're going to override its origin mailer methods by:
config/initializers/devise.rb
config.mailer = 'MyOverriddenMailer'
After that, you need to create your overridden mailer class and override whatever method you want like this:
app/mailers/my_overridden_mailer.rb
class MyOverriddenMailer < Devise::Mailer
helper :application # gives access to all helpers defined within `application_helper`.
include Devise::Controllers::UrlHelpers # Optional. eg. `confirmation_url`
default template_path: 'devise/mailer' # to make sure that you mailer uses the devise views
def confirmation_instructions(record, token, opts={})
if record.name.present?
opts[:subject] = "Welcome #{record.name}, confirm your Qitch.com account"
else
opts[:subject] = "Confirm your Qitch.com account"
end
super
end
end
Remember to restart your Rails server to apply the changes! :)
Note:
List of opts options: subject, to, from, reply_to, template_path, template_name.
record is the instance of User model
And of course, origin document
Devise helper here and How To: Use custom mailer
class MyMailer < Devise::Mailer
def confirmation_instructions(record, opts={})
headers = {
:subject => "Welcome #{resource.name}, confirm your Qitch.com account"
}
super
end
def reset_password_instructions(record, opts={})
headers = {
:subject => "Welcome #{resource.name}, reset your Qitch.com password"
}
super
end
def unlock_instructions(record, opts={})
headers = {
:subject => "Welcome #{resource.name}, unlock your Qitch.com account"
}
super
end
end
Or
class MyMailer < Devise::Mailer
...
...
private
def headers_for(action)
if action == :confirmation_instructions
headers = {
:subject => "Welcome #{resource.name}, confirm your Qitch.com account"
}
elsif action == :reset_password_instructions
headers = {
:subject => "Welcome #{resource.name}, reset your Qitch.com password"
}
else
headers = {
:subject => "Welcome #{resource.name}, unlock your Qitch.com account"
}
end
end
end
And tell devise to use your mailer:
#config/initializers/devise.rb
config.mailer = "MyMailer"
NOTE : I haven't tried them yet, but they may be helpful and for anyone, please correction my answer, if there is an error you could edit my answer
I'm working with devise (3.2.1) and I've implemented the following solution, but to modify the from: field with localization:
# app/mailers/devise_mailer.rb
class DeviseMailer < Devise::Mailer
def confirmation_instructions(record, token, opts={})
custom_options(opts)
super
end
def reset_password_instructions(record, token, opts={})
custom_options(opts)
super
end
def unlock_instructions(record, token, opts={})
custom_options(opts)
super
end
private
def custom_options(opts)
opts[:from] = I18n.t('devise.mailer.from', name: Tenancy.current_tenancy.name, mail: ENV['FROM_MAILER'] )
end
end
Then I've defined the message in my locale files
# config/locales/devise.es.yml
es:
devise:
mailer:
from: "Portal de trabajo %{name} <%{mail}>"
To modify the subject, it should be almost the same:
def confirmation_instructions(record, token, opts={})
custom_options(opts, :confirmation_instructions)
super
end
private
def custom_options(opts, key)
opts[:from] = I18n.t('subject', scope: [:devise, :mailer, key])
end
# and in your locale file
es:
devise:
mailer:
confirmation_instructions:
subject: Instrucciones de confirmaciĆ³n
Hook in headers_for to e.g. prefix the subject for all devise mails.
# config/initializers/devise.rb
Devise.setup do |config|
# ...
config.mailer = 'DeviseMailer'
# ...
end
# app/mailers/devise_mailer.rb
class DeviseMailer < Devise::Mailer
def headers_for(action, opts)
super.merge!({ subject: 'Hi ALEX! ' + subject_for(action) })
end
end
Yo should create an ActionMailer like this one:
class Sender < ActionMailer::Base
default :from => "'Eventer' <dfghjk#gmail.com>"
def send_recover(user, pw)
mail(:to => user.email , :subject => "You have requested to change your password from Eventer") do |format|
format.text {
render :text =>"Dear #{user.name},
**Body**
Regards."
}
end
end
end
Then you should call it from the controller this way:
Sender.send_recover(#mail, current_user, #meetup).deliver
I hope it works for you!
Regards