简体   繁体   中英

How to send an email to multiple recipients

I want to send an email to @user.email and @business.manager.email .

The business doesn't always have a manager.

Is there a short way to create an array of recipients with these 2 (possibly 1) email addresses?

recipients = [@user.email, @business.manager.email]
mail(from: "admin@spam.com", to: recipients, subject: "Spam")

You could create an array with an email, and push a second one.

recipients = [@user.email]
recipients << @business.manager.email if @business.manager

This won't push the manager's email unless there is a manager.

您还可以: recipients = [@user.email, @business.manager.email].compact.reject(&:blank?)

If you want to keep the array declaration to one line, you could do this:

[@user.email, @business.manager.try(:email)].compact

try for the email will return nil if a business doesn't have a manager. And compact removes nil values from the array.

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