Forum: Ruby on Rails sending Mail in loop w attachmt -> only 1.mail has the attachment

6fbb66f7314637b5816f041bdaca9c59?d=identicon&s=25 einar k. (einar_k)
on 2013-04-24 23:27
Hello,
i have a Loop where an email is sent to several adresses one by one.
works fine, but if i attach a file it works only for the first adress
correctly. THe other get the mailtext, but no attachment (coorct name,
but 0 Bytes)
Any Idea why?
TIA


Code of the controller:
def gruppenmail_schreiben
    puts "------------------"
    attach = params[:attachment]
    params[:auftragnr].each do |t|
      if t != '0'
        neuemail = Writtenmail.new
        neuemail.attachment = attach
        neuemail.betreff =  params[:betreff]
        neuemail.body =  params[:body]
        neuemail.candidate_id =  t
        neuemail.absender =  current_fakusers.email
        neuemail.email =  mailsuchen(t)
        neuemail.save
        mailsenden(neuemail)
      end

    end
    redirect_to :controller => 'anzeige', :action => 'suchmaske'
  end #Ende gruppenmail_schreiben

  def mailsenden(mail)
      Notifier.deliver_multipart_alternative_rich(
            #t,#
            mail.email,
            mail.betreff,
            mail.body,
            mail.candidate_id,
            mail.attachment,
            mail.absender
            )
    #end
  end #Ende mailsenden

models/notifier Code:
  def multipart_alternative_rich(email, betreff, inhalt, nr, anhang,
absender, sent_at = Time.now)
    require 'mime/types'

    @subject  =   betreff
    @recipients = email
    @from = absender
    @sent_on =    sent_at
    @body  =  {:inhalt => inhalt}
    @content_type = 'text/html'

    unless anhang.blank?
      datentyp = typerkennung(anhang.original_filename)
      puts datentyp
      attachment  :content_type =>  "'" + datentyp + "'",
                  :body => anhang.read,
                  :filename =>  anhang.original_filename
    end
  end

  def typerkennung(dateiname)
     dateityp = MIME::Types.type_for(dateiname)
     return dateityp[0]
  end

The Log-Part:
Processing AnzeigeController#gruppenmail_schreiben (for 193.25.32.138 at
2013-04-24 14:44:48) [POST]
  Parameters: {"commit"=>"los", "body"=>"<p>fgdsg</p>",
"attachment"=>#<File:/tmp/RackMultipart.24841.0>,
"authenticity_token"=>"YGNJMTo4CBlONuQX+GlhfxBkIzCl5xz6u70zWY1fr/g=",
"betreff"=>"test", "auftragnr"=>["0", "0", "0", "0", "0", "0", "124",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "16", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0"]}
Sent mail to e.eckh@loel.hs.de
Sent mail to ekre@b.de
Redirected to http://mlahsa.de/anzeige/suchmaske
Completed in 1764ms (DB: 1) | 302 Found
[http://mlahsa.einar-kretzler.de/anzeige/gruppenmai...]
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2013-04-25 10:38
(Received via mailing list)
On Wednesday, April 24, 2013 10:27:50 PM UTC+1, Ruby-Forum.com User
wrote:
>
> Hello,
> i have a Loop where an email is sent to several adresses one by one.
> works fine, but if i attach a file it works only for the first adress
> correctly. THe other get the mailtext, but no attachment (coorct name,
> but 0 Bytes)
> Any Idea why?
> TIA
>

If the attachment is an IO like object (which seems like it is, from the
log), then you'll probably need to rewind it after it has been read.

Fred
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.