【问题标题】:How to access the ActionMailer from name如何从名称访问 ActionMailer
【发布时间】:2015-02-28 04:46:30
【问题描述】:

我有一个带有来自 NAME 的变量的 ActionMailer,因此我想测试该名称是否正确设置。

简而言之:

  def test_email()
    mail({
      :from    => '"John Smith"<john@smith.com>'
    })
  end

如果我随后生成一个邮件并尝试访问来自...

$ m = UserMailer.test_email
$ m.from
=> ["john@smith.com"]

我只是得到电子邮件地址。如何访问“John Smith”部分?

【问题讨论】:

  • 你能试着像这样格式化from:吗? 'John Smith &lt;john@smith.com&gt;'?

标签: ruby-on-rails ruby actionmailer


【解决方案1】:

您传递给邮件的字符串(将包含在From: 标头中)与邮件来自的实际电子邮件地址(因此将在 SMTP MAIL FROM 命令中使用)之间存在差异.

如果您想查看传入的值,请使用:

m[:from].value

【讨论】:

    【解决方案2】:

    您可以像这样从标题中提取它。

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

    如果您也想删除疑问引号,请执行以下操作:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-22
      • 2010-10-25
      • 2023-04-03
      • 1970-01-01
      • 2017-03-29
      • 2011-01-15
      相关资源
      最近更新 更多