简体   繁体   中英

Is it possible to replace default from: with an email from database in Rails Action Mailer?

I am trying to pass on an email from a user to the default from: field so that it looks like it's coming directly from them. Here is what I have right now. Is there any way of bringing in a dynamic variable into the default from field?

class IntroMailer < ActionMailer::Base
      default from: "Me@gmail.com"

      def intro_email(intro)
        @intro = intro
        mail(to: @intro.person1_email, subject: 'Testing Intro Email')
      end
    end

You can override this in the Mailer action's mail method:

class IntroMailer < ActionMailer::Base
  default from: "Me@gmail.com"

  def intro_email(intro, current_user)
    mail(to: intro.person1_email, subject: 'Testing Intro Email', from: current_user.email)
  end
end

but a WARNING . Email clients, like Google, are pretty smart at detecting spam. If they see that a specific SMTP server is sending out emails with lots of different 'from' attributes, your spam rating will go up and your emails will be filtered out by spam filters. To get around this, choose one or two default from emails (eg support@mywebsite.com & jobs@mywebsite.com) that fit the email's type, and then add a dynamic reply_to attribute instead.

class IntroMailer < ActionMailer::Base
  default from: "ourteam@oursite.com"

  def intro_email(intro, current_user)
    mail(to: intro.person1_email, subject: 'Testing Intro Email', reply_to: full_from(current_user))
  end

  private

  def full_from(user)
    address = Mail::Address.new user.email
    address.display_name = user.full_name
    address.format
  end
end

Default 只是一个默认地址,所以你可以在发送这样的邮件时覆盖它:

mail(from: @some_object.mail, ...)

实际上不是,它在 Rails 5.XX 中根本不起作用

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM