mail.parts.length appears to change when mail.parts are iterated through #119

Closed
novemberkilo opened this Issue Oct 14, 2010 · 2 comments

Comments

Projects
None yet
2 participants
require 'rubygems'
require 'mail'


mail = Mail.new do
  to 'navin@novemberkilo.org'
  from 'novemberkilo@gmail.com'
  subject 'Plain text email - constructed in Ruby'
end

text_part = Mail::Part.new do
   body 'This is plain text'
 end

mail.text_part = text_part

puts "mail has #{mail.part.length.to_s} parts"
puts "---"

mail.part.each do |x|
  puts x.to_s
  puts "---"
end

puts "mail has #{mail.part.length.to_s} parts"

Here, even though we have a plain text email with one part, mail.parts.length initially returns 2 and after iterating through the parts of mail, returns 4. The correct answer of course, is 1. When you play with this in irb and repeat the

mail.parts.each { |x| ... }

new parts appear (with headers only and no body).

Collaborator

jeremy commented Jan 26, 2013

When you call mail.part you're adding a new part each time. Use mail.parts.

@jeremy jeremy closed this Jan 26, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment