這花了我1-2周的大部分時間搞清楚,所以希望這個代碼將會給你一些想法該怎麼辦。
下面是我列出控制器,我生成消息被髮送給寄件人:
def messages
@list = List.find(params[:id])
#@message = Message.new(:user_id => @list.user.id, :list_id => @list.id,
#:subject => params[:subject], :body => params[:body])
@message = Message.new(params[:message])
@contacts = @list.contacts
respond_to do |format|
if @list.save && @message.save
#Blaster is my mailer, and blast is the send method in the blaster.rb mailer
Blaster.blast(@message, @contacts)
format.html {
redirect_to @list, notice: 'Message was saved.' }
format.json { render json: @list, status: :created, location: @list }
else
format.html { redirect_to @list, notice: 'There was an error, try again'}
format.json { render json: @list.errors, status: :unprocessable_entity }
end
end
末
=========
這裏是我的郵件 - 這是叫blast.rb
def blast(message, contacts)
# can't send without a message, and an array of contacts
@message = message
@contacts = contacts
# with variables set, let's create the loop to do its magic
@contacts.each do |contact|
mail = mail(
:to => "#{contact.email}",
:from => "[email protected]",
:return_path => "[email protected]",
:subject => @message.subject,
:body => @message.body
#:template_path => 'blaster',
#:template_name => 'blast'
) do |format|
format.html { render 'blast.html.erb'}
format.text { render 'blast.text.erb'}
end
mail.deliver
end # contacts.each loop
end #blast method
我很肯定這個代碼是次優的,質量差,所以告誡emptor,但也許你會在這裏找到一些能夠幫助你解決你的獨特實現的東西。希望能幫助到你!
D.
你爲什麼不先準備好您的電子郵件,然後對他們的呼籲提供每個迭代? – socjopata
這是因爲您將數組傳遞給':to',而不是每個電子郵件地址。 –