2016-03-09 84 views
0

我想每天多次向我的应用程序注册我的应用程序的用户发送一条消息。 这里有一个邮件的方法我用的是:Rails 4 - 如果发送给多个收件人(使用Sendgrid),则不会发送邮件

def notify_users(product) 
    @product 
    emails = Customer.signed_up.pluck(:email_address) 
    puts "EMAILS:" 
    puts emails.inspect # I see here, say, 5 email addresses 
    emails.each do |email| 
     unless email.blank? 
     puts "Sending to #{email}" # email is displayed here properly 
     mail(to: email, subject: "New products", from: '[email protected]') 
     end      
    end 

什么我发现是,如果emails只包含一个电子邮件地址,电子邮件上的电子邮件地址发送。但是当我通过多个电子邮件地址发送电子邮件时,通常只发送最后一条消息(因此如果emails继续["[email protected]", "[email protected]", "[email protected]"]),则电子邮件仅在[email protected]上发送。

这是为什么?试图找出原因,但仍不成功。

+0

什么用的代码,除非email.blank的?只是尝试删除它。 –

回答

0

试试这样说:

def send_notification_to_user(email) 
    puts "Sending to #{email}" # email is displayed here properly 
    mail(to: email, subject: "New products", from:'[email protected]') 
end 

def notify_users(product) 
    @product 
    emails = Customer.signed_up.pluck(:email_address) 
    puts "EMAILS:" 
    puts emails.inspect # I see here, say, 5 email addresses 
    emails.each do |email| 
    unless email.blank? 
     send_notification_to_user(email).deliver_now 
    end      
    end 
end 
相关问题