rails email notification- mailer - ruby-on-rails

I'am new to rails. Users need to receive the email notification after few days only if they haven't read the notification yet. I've created the email notification with Action Mailer but it's not working after added the time format. The email still not deliver to the users after 5 days.
class NotificationsController < ApplicationController
def show
#notification = current_user.notifications.find_by_id(params[:id])
if current_user.notifications.unread > 5.days.ago
UserMailer.try_notif(#notification, #post, #user).deliver
end
end
def update
#notification = current_user.notifications.find_by_id(params[:id])
#notification.update(read: true)
redirect_to post_path(#notification.post)
end
def read_all
current_user.notifications.unread.update_all(read: true)
end
end
user_mailer.rb
class UserMailer < ApplicationMailer
def try_notif(notification, post, user)
#notification = notification
#post = post
#user = user
mail(to: #post.user.email, subject: 'New Notification')
end
end

i think correct code would be like this:
Need to check each notification with current time
if current_user.notifications.unread
current_user.notifications.each do |notification|
if Time.current - notification.created_at > 120 #just checking in hours
#send reminder
end
end
end
or can try other way
#notifications = Notification.where(created_at: 5.days.ago..Time.current, unread: true)
if #notifications
#notifications.each do |notification|
#send notification
end
end

Related

Rails: Why is only one email sent to the last member of a loop?

I am very much a rails novice!
I am trying to write a method for a kind of on-line committee meeting. There are a fixed number(9) of users. When a user proposes a topic for discussion and/or voting the submit button needs to send an email to all members.
in app/mailers/user_mailer.rb I have:-
class UserMailer < ApplicationMailer
def new_topic_alert(topic)
#users = User.all
#users.each do |user|
mail to: user.email, subject: "New topic alert"
end
end
end
as part of app/controllers/topics_controller.rb I have:-
def send_alert
#topic = Topic.new(topic_params)
UserMailer.new_topic_alert(#topic).deliver_now
end
and:-
def create
#topic = Topic.new(topic_params)
if #topic.save
send_alert
flash[:info] = "New Topic alert emails sent."
redirect_to root_url
else
render 'new'
end
end
Please, why does the loop in user_mailer only send an email to the final person of the list. By incorporating "byebug" I have shown that it goes through all the user emails.
Try like below:
def send_alert
#topic = Topic.new(topic_params)
users = User.all
users.each do |u|
UserMailer.new_topic_alert(#topic, u).deliver_now
end
end
and update the mailer like
class UserMailer < ApplicationMailer
def new_topic_alert(topic,user)
mail to: user.email, subject: "New topic alert"
end
end

Send mail at particular time in action mailer using delayed job

def time_confirmation(user)
#user = user
if(#time == (Time.now()))
mail(:to => user.email)
end
end
Is this right? Mail has to be sent at current time.
On your controller do something like:
def create
#user = User.create(user_params)
DelayedEmailJob.new(#user.email).enqueue(wait: 30.minutes)
redirect_to root_path
end

Mailboxer Gem, Admin View

I'm using the mailboxer gem for conversations/messages between the User model in my app. This is all working fine, thanks to some great help in stack overflow. I'm now trying to setup a section so admin can view all the conversations that are happening.
I have created a controller and view for conversations, nested in my admin section. I've pulled in all conversations on the index page with:
def index
#admin_conversations = Conversation.all
end
This has listed all the conversations, and a link to show each conversation, as expected.
The problem I am having, is that the mailboxer Gem is setup to only allow the current_user to view conversations that the current_user is a participant in. So I can click some of the conversations, (signed is as an admin) and see the contents, but some (that are between other test users) I cannot see i.e it throws an exception such as:
Couldn't find Conversation with id=5 [WHERE "notifications"."type" = 'Message' AND "receipts"."receiver_id" = 35 AND "receipts"."receiver_type" = 'User']
How can I define the method in my admin controller so that the admin can see everything?
I'm currently using cancan and allowing all 3 user roles I have (admin, client and supplier) like this:
can :manage, Conversation
...so it isn't a normal authorisation problem.
Here is my conversations controller:
class ConversationsController < ApplicationController
authorize_resource
helper_method :mailbox, :conversation
def create
recipient_emails = conversation_params(:recipients).split(',')
recipients = User.where(email: recipient_emails).all
conversation = current_user.
send_message(recipients, *conversation_params(:body, :subject)).conversation
redirect_to :back, :notice => "Message Sent! You can view it in 'My Messages'."
end
def count
current_user.mailbox.receipts.where({:is_read => false}).count(:id, :distinct => true).to_s
end
def reply
current_user.reply_to_conversation(conversation, *message_params(:body, :subject))
redirect_to conversation
end
def trash
conversation.move_to_trash(current_user)
redirect_to :conversations
end
def untrash
conversation.untrash(current_user)
redirect_to :conversations
end
private
def mailbox
#mailbox ||= current_user.mailbox
end
def conversation
#conversation ||= mailbox.conversations.find(params[:id])
end
def conversation_params(*keys)
fetch_params(:conversation, *keys)
end
def message_params(*keys)
fetch_params(:message, *keys)
end
def fetch_params(key, *subkeys)
params[key].instance_eval do
case subkeys.size
when 0 then self
when 1 then self[subkeys.first]
else subkeys.map{|k| self[k] }
end
end
end
end
The answer is probably something pretty silly, but I am new to this...
Thanks
In your conversation method, your calling mailbox.conversations.find(params[:id])
mailbox.conversations is what is limiting you to the current user's conversations.
Try just Conversation.find(params[:id])

Show username on Mailboxer inbox in Rails

I'm using the Mailboxer (https://github.com/ging/mailboxer) gem in my Rails app to handle private messages between users. As per the documentation, I'm using = render mailbox.inbox to show a list of all conversations (messages) for the logged-in user. However, the method only outputs the subject of the message. I need to show the sender of the message along with the subject. How can I edit this output so that the sender name is displayed?
The SQL table shows that the sender name is not stored in the conversations table, but on the notifications table via notifications_on_conversation_id (one to many). I tried to use <%= conversation.notifications.sender_id %> but I got the following error:
NoMethodError in Conversations#index
undefined method `notifications' for nil:NilClass
What can I add to the conversation controller and view in order to display the conversation's user and subject?
Thanks!
Conversations controller:
class ConversationsController < ApplicationController
before_filter :authenticate_user!
helper_method :mailbox, :conversation
def create
recipient_emails = conversation_params(:recipients).split(',')
recipients = User.where(email: recipient_emails).all
conversation = current_user.
send_message(recipients, *conversation_params(:body, :subject)).conversation
redirect_to conversation
end
def reply
current_user.reply_to_conversation(conversation, *message_params(:body, :subject))
redirect_to conversation
end
def trash
conversation.move_to_trash(current_user)
redirect_to :conversations
end
def untrash
conversation.untrash(current_user)
redirect_to :conversations
end
private
def mailbox
#mailbox ||= current_user.mailbox
end
def conversation
#conversation ||= mailbox.conversations.find(params[:id])
end
def conversation_params(*keys)
fetch_params(:conversation, *keys)
end
def message_params(*keys)
fetch_params(:message, *keys)
end
def fetch_params(key, *subkeys)
params[key].instance_eval do
case subkeys.size
when 0 then self
when 1 then self[subkeys.first]
else subkeys.map{|k| self[k] }
end
end
end
end
sender_id is available using receipt.notification.sender_id and .sender_type
for any conversation you can retrieve the receipts using:
#receipts = conversation.receipts_for
I ran on the same issue at some point and my solution was something like this: accessing current_user in mailboxer mailer notification

PayPal IPN Send Email

I have a controller that handles PayPal's IPN callback. I want to mark an attendee as 'paid' and send them a confirmation email if they've successfully paid.
The mark paid action is working but the email is not sending.
Here's my controller:
class PaymentNotificationsController < ApplicationController
protect_from_forgery :except => [:create]
def create
PaymentNotification.create!(:params => params, :attendee_id => params[:invoice], :status => params[:payment_status], :transaction_id => params[:txn_id])
if params[:payment_status] == 'Complete'
#attendee = Attendee.find(params[:invoice])
## Working
#attendee.update_attribute(:paid, Time.now)
## Not Working
UserMailer.welcome_email(#attendee).deliver
end
render nothing: true
end
end
Here's my user_mailer file:
class UserMailer < ActionMailer::Base
default from: 'example#email.com'
def welcome_email(user)
#user = user
email_with_name = "#{#user.first_name} #{#user.last_name} <#{#user.email}>"
#url = 'http://example.com'
mail(
to: email_with_name,
subject: 'Welcome to Yadda Yadda'
)
end
end
Here's the weird thing, in another controller that doesn't have PayPal the mailer works:
class VendorsController < ApplicationController
def create
#vendor = Vendor.new(vendor_params)
if #vendor.save
UserMailer.welcome_email(#vendor).deliver
redirect_to vendor_success_path
else
render 'new'
end
end
end
I am pulling your answer out of your question and posting it here for future reference.
This takes two actions (mark paid and send mail). It has been moved to the model as an after_create method.
Here's the model:
class PaymentNotification < ActiveRecord::Base
...
after_create :mark_attendee_paid
private
def mark_attendee_paid
if status == 'Completed'
attendee.update_attribute(:paid, Time.now)
UserMailer.welcome_email(attendee).deliver
end
end
end

Resources