This is what I''ve done to extract the attachments.
If you''d rather develop it in a rails application you could do
something
like this.
I created a model to manage the attachments specifically, once I did that
everything was made much easier and cleaner. I then created a class method
called ''extract_attachment'' which had an argument of a tmail
mail part. The
method took the part and extracted and stored the contents and returned it
so that it could be assigned to the email. View below for the code, hope it
helps. This could work easily with a file system storage as well, instead
of Attachment.new you would open a new file, dump the contents into it, then
close the file.
Reason for the rescues:
The I found that not all emails were parsed properly with the TMail class
library. So I had to do quite a bit of trial and error on insertion. If
there''s a better way to do that, please let me know.
# == Schema Information
# Schema version: 50
#
# Table name: attachments
#
# id :integer(11) not null, primary key
# parent_id :integer(11)
# message_id :integer(11)
# filename :string(255)
# content_type :string(255)
# size :integer(11)
# width :integer(11)
# height :integer(11)
# thumbnail :string(255)
# data :binary
#
class Attachment < ActiveRecord::Base
belongs_to :email
def self.extract_attachment part
Attachment.new(
:filename => safe_filename(part),
:content_type => safe_content_type(part),
:data => safe_data(part),
:disposition => part_disposition(part),
:cid => part_cid(part)
)
end
def self.safe_filename part
begin
part.filename
rescue
begin
part.original_filename
rescue
begin
part[''content-disposition''][''filename'']
rescue
''Attachment''
end
end
end
end
def self.safe_content_type part
begin
part.content_type
rescue
''binary''
end
end
def self.safe_data part
begin
part.read
rescue
begin
part.body
rescue
''no content''
end
end
end
def self.extract_name part
part.original_filename.nil? or part.original_filename ==
'''' ?
''Attachment'' : part.original_filename
end
def self.part_disposition part
(part.content_disposition.nil? or part.content_disposition ==
"inline")
? "inline" : "attachment"
end
def self.part_cid part
(!part.header_string("Content-ID").nil? and
!part.header_string("Content-ID").empty?) ?
part.header_string("Content-ID")
: nil
end
validates_presence_of :email_id, :filename, :data
end
Thanks,
- Matt
On 1/8/09 1:29 AM, "Bj?rn Andersson" <ba at sanitarium.se>
wrote:
> Hi!
>
> To write out an attachment you need to fetch the body of the
> attachment and write it to a file.
>
> I''ve written a script using TMail to extract mime attachments and
> uuencoded attachments that you could look at to get an idea. :)
> http://cell.sanitarium.se/~ba/tmp/strip-email-attachment.rb.txt
>
> /Bj?rn
>
> On Tue, Dec 30, 2008 at 4:59 PM, Sam Luther <sam at three60.com>
wrote:
>> Hello,
>> @email =
TMail::Mail.load(''/Users/sam/Desktop/test.eml'')
>> @email.attachments.each do |f|
>> # how do i write out the attachment or move it? I tried this,
>> # FileUtils.mv f,
"/Users/sam/Desktop/processed/#{f.original_filename}"
>> end
>>
>> thanks in advance for any help.
>> _______________________________________________
>> Tmail-talk mailing list
>> Tmail-talk at rubyforge.org
>> http://rubyforge.org/mailman/listinfo/tmail-talk
>>
>
>