Rails Action_Mailer - ruby-on-rails

I am using admin mailer in my Rails 4 app.
I have two emails to send out upon registration. One is to me and the other is to the user. They are supposed to send from different email addresses, each of which is specified in the from field in the mailer method (as set out below). The problem is they are both being sent from the email address specified as the sender in the first method.
My mailer methods are:
def new_user_waiting_for_approval(user)
#user = user
mail(to: "aa#gmail.com", from: "bb#gmail.com",
subject: "Registration Request #{user.first_name} #{user.last_name} <#{user.email}>")
end
def new_user_waiting_for_access(user)
#user = user
mail(to: user.email, from: "cc#gmail.com", subject: "Welcome, #{user.first_name}")
end
Inside my Admin_Mailer class, I have a default 'from:' email address above the method which is specified as the sender in the first of the above methods. This might be overriding the from specified in the method itself.
Does anyone know how to specify different senders in separate methods so that my emails send from the appropriate email address?
Thank you

If you couldn't figure it out by changing the configs you can use the code given below.
Add this code snippet to your code base.
class Emailer < ActionMailer::Base
def activation_instructions(recipients_emails, sender = nil, mail_subject = nil, text = "")
subject mail_subject || "Default subject"
recipients recipients_emails
from sender || "default_mail_id#abc.com"
sent_on Time.now
content_type "text/html"
body text
end
end
And you can send the mail by calling the above defined method as follows.
Emailer.deliver_activation_instructions("recient#abc.com", "sender#abc.com", "Subject", "content")

Related

How to make copy of email using Action Mailer?

Here is my code:
class Mailer < ActionMailer::Base
def notify(subject:, email_to:, email_from:)
mail(subject: subject, to: email_to, from: email_from)
end
end
I am calling notify method like below:
mail = Mailer.notify(subject: 'This is test email', email_from: ['user1#mail.com'], email_to: ['user2#mail.com'])
mail.deliver
Now before calling mail.deliver, I want to copy email in another variable(copy_email) with current state and make some changes in copy_email subject and send it to another user.
I am making copy with using.
copy_email = mail.clone
or
copy_email = mail.dup
in both cases when I am changing subject of copy_email it's also changing subject of original email.
Anyone have any idea about how to avoid it?

how to change the sender email credentials according to the user of application in rails

I want to give different sender email credentials according to the user of application in rails which will be fetched from database.
If you're using ActionMailer, you can to set 'from':
fetch_sender = Object.select(:email, :name).find(id)
mail(from: "#{fetch_sender.name} <#{fetch_sender.email}>", to: 'Receiver <example#receiver.com>', subject: 'Something')
Example for ActionMailer class:
app/mailers/custom_mailer.rb
class CustomMailer < ApplicationMailer
def custom(sender, receiver)
mail(from: "#{sender.name} <#{sender.email}>", to: "#{receiver.name} <#{receiver.email}>", subject: 'Something')
end
end
For sending
CustomMailer.custom(sender, receiver).deliver
P.s. If you have sidekiq or something else, yo may use deliver_now or deliver_later

Send email after attribute updated Rails

I would like to send an email to a user once the status of their pdform is updated. I already have some stuff written out on how I want this done.
In my pdform.rb model
after_update :sendemail
def sendemail
if status_changed?
end
end
I already have emails being sent out when the user creates a new form, however, I am not sure how to send an email in the model.
The controller has this mailer function that works correctly. How could I send this in this model?
NewPdform.notify_user(current_user, #pdform).deliver
Any help would be greatly appreciated. Still getting the hang of ActiveRecord.
Update:
In my pdforms_controller update method I have added the following variable.
update_user = #pdform.user
I added an attr_accessor in pdform.rb (the model)
attr_accessor :update_user
after_update :sendemail
def sendemail
NewPdform.notify_update(update_user).deliver
end
And in my mailer
def notify_update(user)
#user = user
mail(to: #user.email, subject: "some change occured")
end
I solved my own issue after using my brain more extensively.
In the call to the mailer function instead of passing the parameter of pdform, which is the name of the class anyways, just pass self.
def sendemail
NewPdform.notify_update(self).deliver
end

Trouble accessing certain methods from within Action Mailer in Ruby

Thanks for reading this post guys. Rails newbie here with what I'm pretty sure is a fundamental error.
I have a users table with a column 'added_by' that shows which user added them to the database.
So in this example, Melissa (id:2) was added by Thomas (id:1).
This is my Actionmailer notification_mail.rb, that fires emails when tickets are created:
class NotificationMailer < ActionMailer::Base
add_template_helper HtmlTextHelper
def new_ticket(ticket, user)
unless user.locale.blank?
#locale = user.locale
else
#locale = Rails.configuration.i18n.default_locale
end
title = I18n::translate(:new_ticket, locale: #locale) + ': ' + ticket.subject.to_s
add_attachments(ticket)
unless ticket.message_id.blank?
headers['Message-ID'] = "<#{ticket.message_id}>"
end
#ticket = ticket
#user = user
assoc = User.find_by_id(user.added_by)
mail(to: user.email, subject: title, from: ticket.reply_from_address) #sends email to ticket creator
mail(to: assoc.email, subject: title, from: ticket.reply_from_address) #sends email to user who added ticket creator
end
It should send an email to:
The ticket creator.
The user who added the ticket creator.
But this is the error I get:
undefined method `email' for nil:NilClass
Which comes from this line:
mail(to: assoc.email, subject: title, from: ticket.reply_from_address)
I'm pretty sure it's to do with me not including something in this file, but I simply can't figure it out despite reading other questions and ruby documentation.
Can someone help?
Thanks guys.
If added_by is not present for an user in the database (which is the case for Thomas in your given example) then assoc = User.find_by_id(user.added_by) won't find the assoc user record and assoc will be nil. So, if you call assoc.email it will fail with this message:
undefined method `email' for nil:NilClass
Alternatively, you can do:
assoc.try(:email)
Or, you can check first if assoc is present, only then you send them email:
if assoc && assoc.email
#sends email to user who added ticket creator
mail(to: assoc.email, subject: title, from: ticket.reply_from_address)
end

Message.body empty in ActionMailer Observer

I am using an Observer to log outgoing emails; it fires correctly but when I attempt to extract the body of the email I get an empty string. The emails are definitely not blank and the logging record is definitely created. Breakpointing and inspecting message.body confirms that it is an empty string.
class MailObserver
def self.delivered_email(message)
for address in message.to
user = User.find_by_email(address)
if user
UserMailerLogging.create!(user_id: user.id, email_type: message.subject,
contents: message.body, sent_at: Time.now)
end
end
end
end
ActionMailer::Base.register_observer(MailObserver)
In Rails 3, emails are built and sent using the Mail gem. According to their docs
message.text_part # plaintext version
message.html_part # html version
You can also go a bit further depending on whether your emails are multipart or not.
message.text_part.body.decoded
This question here on SO may also be hefpful.

Resources