简体   繁体   中英

ruby on rails action mailer not receiving emails to arrays from a yaml file

I have an issue where i am able to send email via the mailer for a hardcoded array. But the email does not go through for an array picked up from config.yml

Here is my config.yml

company:
  email:
    - user1@company.com
    - User1.Lastname@company.com
    - User2.Lastname@company.com

This is my mailer class:

class ReportMailer < ActionMailer::Base
  default :from => "donotreply@company.com"


  def send_report(company, file)

      mail(:to=> "#{CONFIG[company]['email']}", :subject => "Daily  Report")
    end
  end
end

when run it in my rails console & view the logs, seems like everything was executed fine but I did not receive my email:

[DEBUG] 2016-04-21 18:21:29 :: Date: Thu, 21 Apr 2016 18:21:29 -0400
From: donotreply@merchantlink.com
to: ["user1@company.com", "User1.Lastname@company.com","User2.Lastname@company.com"]
 ...
 ...

 [INFO] 2016-04-21 18:21:29 ::
Sent mail to ["user1@company.com", "User1.Lastname@company.com", "User2.Lastname@company.com"]

If i change my code & replace it with hardcoded array instead of reading from config.yml it works fine.

Am i reading the yaml array wrong?

As correctly pointed out in the comment by @Alfie, you are passing a stringified array to to: .

CONFIG[company]['email'] returns an array, then string interpolation calls to_s on it and you end up with

"['user1@company.com', 'User1.Lastname@company.com','User2.Lastname@company.com']"

Just pass in the array without inserting it into a string:

mail(:to=> CONFIG[company]['email'], :subject => "Daily  Report")

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