Zack
Zack

Reputation: 2497

How to access the ActionMailer from name

I have an ActionMailer that has a variable from NAME, thus I would like to test that the name is being properly set.

In short:

  def test_email()
    mail({
      :from    => '"John Smith"<[email protected]>'
    })
  end

If I then generate a mailer and try to access the from...

$ m = UserMailer.test_email
$ m.from
=> ["[email protected]"]

I just get the email address. How do I access the "John Smith" part?

Upvotes: 0

Views: 116

Answers (2)

smathy
smathy

Reputation: 27961

There's a difference between the string you pass into mail (which will be included as the From: header) and the actual email address that the mail is from (and hence which will be used in the SMTP MAIL FROM command).

If you want to see the value that you passed in then use:

m[:from].value

Upvotes: 3

coderhs
coderhs

Reputation: 4837

You can extract it from the headers like so.

m.header.select { |i| i.name == "From" }.first.value[/\"(.*?)\"/,0]

If you want to remove the doubt quotes as well do:

m.header.select { |i| i.name == "From" }.first.value[/\"(.*?)\"/,0][1..-2]

Upvotes: 1

Related Questions