簡體   English   中英

Rails Action_Mailer

[英]Rails Action_Mailer

我在Rails 4應用程序中使用管理員郵件。

我有兩封電子郵件在注冊時發出。 一個是給我的,另一個是給用戶的。 它們應該從不同的電子郵件地址發送,每個電子郵件地址都在mailer方法的“發件人”字段中指定(如下所述)。 問題在於它們都是從第一種方法中指定為發件人的電子郵件地址發送的。

我的郵寄方法是:

def new_user_waiting_for_approval(user)
    @user = user
    mail(to: "aa@gmail.com", from: "bb@gmail.com", 
    subject: "Registration Request #{user.first_name} #{user.last_name} <#{user.email}>")
  end

  def new_user_waiting_for_access(user)
    @user = user
    mail(to: user.email, from: "cc@gmail.com", subject: "Welcome, #{user.first_name}")
  end

在我的Admin_Mailer類中,我在方法上方有一個默認的“發件人:”電子郵件地址,該方法在上述方法中的第一個方法中指定為發件人。 這可能會覆蓋方法本身中指定的from。

有誰知道如何用不同的方法指定不同的發件人,以便我的電子郵件從適當的電子郵件地址發送?

謝謝

如果您無法通過更改配置來解決問題,則可以使用以下代碼。

將此代碼段添加到您的代碼庫中。

class Emailer < ActionMailer::Base
  def activation_instructions(recipients_emails, sender = nil, mail_subject = nil, text = "")
    subject       mail_subject || "Default subject"
    recipients    recipients_emails
    from          sender || "default_mail_id@abc.com"
    sent_on       Time.now
    content_type  "text/html"
    body          text
  end
end

您可以通過調用以下定義的方法來發送郵件。

Emailer.deliver_activation_instructions("recient@abc.com", "sender@abc.com", "Subject", "content")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM