user3576036
user3576036

Reputation: 1435

How to stop `mail` action sending to `:to` address if :bcc addresses are empty in ActionMailer?

I have a actinmailer method where the @receiver is populated if it matches certain conditions and there is a @default_mail. Which sends mails to receivers in BCC and default_mail in to field. It is as follows:

def mail_users
    @default_mail = "[email protected]"
    @latest_listing_mail= Equipment.joins(:user).last.user.email
    @wanted_req_mail = WantedEquipment.where(sub_category_id: "#{a}", status: 2).pluck(:email) 
    @wanted_req_mail.include? @latest_listing_mail
    if true
       @receiver = @wanted_req_mail.delete(@latest_listing_mail)
       @receiver = @wanted_req_mail
       mail(bcc: @receiver, to: @default_mail)
    end
end

I don't want the mail to perform if the @receiver is empty. How do I achieve this?

Upvotes: 0

Views: 617

Answers (1)

Deepak Mahakale
Deepak Mahakale

Reputation: 23671

Just add a condition

mail(bcc: @receiver, to: @default_mail) if @receiver.present?

And you might want to remove this condition

if true # doesn't make sense
end

Upvotes: 1

Related Questions