Search code examples
ruby-on-rails-4mailboxer

Mailboxer Change Recipients from User's Email to User's Name RoR 4


I'm a beginning programmer, and I have just implemented Mailboxer in my Ruby on Rails 4 web app. Everything is working great, but I want to have the recipients be the user's names, not their emails. I've searched for hours and can't find a precise answer. Here is my code, though it's pretty much exactly what the documentation instructs to do.

config/initializers/mailboxer.rb

    Mailboxer.setup do |config|

  #Configures if you application uses or not email sending for Notifications and Messages
  config.uses_emails = true

  #Configures the default from for emails sent for Messages and Notifications
  config.default_from = "[email protected]"

  #Configures the methods needed by mailboxer
  config.email_method = :mailboxer_email
  config.name_method = :name

  #Configures if you use or not a search engine and which one you are using
  #Supported engines: [:solr,:sphinx]
  config.search_enabled = false
  config.search_engine = :solr

  #Configures maximum length of the message subject and body
  config.subject_max_length = 255
  config.body_max_length = 32000
end

app/models/user.rb

  acts_as_messageable

  ...

  def name
    email
  end

  def mailboxer_email(object)
    email
  end

Any help would be greatly appreciated. I've tried playing around with the code but I just can't quite get to the right combination of tweaks. Thank you so much!


Solution

  • This is basically a feature of the receiving mail user agent, which takes a mail header

    To: somebody <[email protected]>
    

    and displays only "somebody". One way to build that header should be

    def mailboxer_email(object)
        "#{name} <#{email}>"
    end
    

    if the variable name contains the username you want the mail user agent to display.