【问题标题】:Rails: Send email from gemRails:从 gem 发送电子邮件
【发布时间】:2014-11-30 19:51:58
【问题描述】:

我正在创建一个 gem,用于发送通知

通知使用他的电子邮件地址发送给用户,因此发送通知我将发送文本电子邮件给用户。

那么如何从 gem 发送邮件呢?因为我需要从Actionmailer::Base继承类

Module ABC
  class notify < ActionMailer::Base
    def send_mail
    end
  end
end

我需要在我的 gemspec 文件中添加任何 gem 吗? railsactionmailer 宝石? 提前致谢

【问题讨论】:

  • ActionMailer 是自给自足的,非常适合这项任务。为什么要创建 gem?
  • 它是给第 3 方的通知,并且正在向他们发送通知.. 所以想给他们发送电子邮件,因此为内部目的添加 gem.. 如果您能帮我解决这个问题,那将很有帮助怀疑

标签: ruby-on-rails ruby email actionmailer


【解决方案1】:

我仍然无法理解您创建宝石的动机。然而,我会做这样的事情: 创建 app/model/notify.rb

class notify < ActionMailer::Base
  default :from => "notify@example.com", :content_type => "text/html"
  def send_mail(user)
    @user = user
    mail(:to => @user.email, :subject => "Notification mail from xyz")
  end
end

现在创建一个邮件视图 app/views/notify/send_mail.erb

Hi @user.name,
Here is your notification.
Thanks

现在可以从代码中的任何位置调用它。您所要做的就是:

Notify.send_mail(user).deliver

您可以继续添加不同的方法及其模板视图并将它们发送为

Notify.your_method(user).deliver

【讨论】:

  • 试过这个,但是得到错误未初始化常量 ABC::ActionMailer (NameError) 它没有从哪里引用 ActionMailer(ABC 是我的模块名称).. 我们需要在 gemspec 中添加任何依赖项吗?
  • 您是否在 config/application.rb 中配置了您的 actionmailer?
  • 抱歉,我的 gem 结构中没有任何配置文件夹。我只有 lib 文件夹、gemspec、gemfile 等。
  • 在这里找到了部分解决方案 stackoverflow.com/questions/4951310/… 需要进一步挖掘.. @shivam 感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 2011-06-28
  • 2018-07-28
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 2011-06-30
  • 2011-10-25
  • 2016-07-17
相关资源
最近更新 更多