# Save an attachment file into the folder

**URL:** <https://www.ruby-forum.com/t/save-an-attachment-file-into-the-folder/161102>\
**Category:** Ruby\
**Created:** [February 23, 2009, 12:39pm UTC](https://www.ruby-forum.com/t/save-an-attachment-file-into-the-folder/161102 "2009-02-23T12:39:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![deeparaj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/85f322/32.png) [@deeparaj](https://www.ruby-forum.com/u/deeparaj)\
**Post date:** [February 23, 2009, 12:39pm UTC](https://www.ruby-forum.com/t/save-an-attachment-file-into-the-folder/161102/1 "2009-02-23T12:39:06Z")

</div>

Hai friends,  
I received an attachment file in a encoding format but i want  
to get it as s text file.And those file want to store it in a specified  
Folder.  
Here the coding is:

require ‘rubygems’  
require ‘net/pop’  
require ‘tlsmail’  
require ‘net/smtp’

BASE\_DIR = “C:\Documents and Settings\mpf18.MPFD18\Desktop\attachment”  
username = ‘username’  
password = ‘password’

Net::POP3.enable\_ssl(OpenSSL::SSL::VERIFY\_NONE)  
conn = Net::POP3.new(‘[pop.gmail.com](http://pop.gmail.com)’,Net::POP3.default\_pop3s\_port)  
conn.start(username, password)

conn.each\_mail do |msg|

# Print the ‘From:’ header line

#puts msg.header.split(“\r\n”).grep(/^From: /)  
filename=msg.header.split(“\r\n”).grep(/^From: /)  
puts filename

# File.open(File.join(“#{BASE\_DIR}/#{filename}”),

File::CREAT|File::TRUNC|File::WRONLY) do |f|  
# f.write(msg)

# Put message to $stdout (by calling \<\<)

puts “\nFull message:\n”  
msg.all($stdout)

#end  
end

---

<div class="post-metadata">

**Author:** ![deeparaj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/85f322/32.png) [@deeparaj](https://www.ruby-forum.com/u/deeparaj)\
**Post date:** [February 23, 2009, 1:26pm UTC](https://www.ruby-forum.com/t/save-an-attachment-file-into-the-folder/161102/2 "2009-02-23T13:26:04Z")

</div>

Hi,

2009/2/23 Deepa R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> require ‘net/smtp’
> 
> # Put message to $stdout (by calling \<\<)
> 
> puts “\nFull message:\n”  
> msg.all($stdout)
> 
> #end  
> end  
> I recommend you install tmail library(gem install tmail)  
> If you installed tmail, you can try this:

require ‘net/pop’  
require ‘tmail’

Net::POP3.enable\_ssl(OpenSSL::SSL::VERIFY\_NONE)  
Net::POP3.start(‘[pop.gmail.com](http://pop.gmail.com)’, 995, username, password) do |pop|  
if pop.mails.empty?  
puts ‘No mail.’  
else  
pop.each\_mail do |message|  
string = message.pop  
mail = TMail::Mail.parse( string )  
if mail.multipart? then  
mail.parts.each do |m|  
if m.disposition  
filename = m.disposition\_param(‘filename’)  
if filename[0,2]==‘=?’  
filename.gsub!(/=?[^?]+?(.)?([^?]+)?=$/){$1==‘B’ ?  
$2.unpack(‘m\*’) : $2.unpack(‘M\*’)}  
end  
puts filename  
File.open(filename,‘wb’) {|f|f.write(m.body)}  
end  
end  
end

```
end

```

end  
end

Regards,  
Park H.

---

<div class="post-metadata">

**Author:** ![deeparaj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/85f322/32.png) [@deeparaj](https://www.ruby-forum.com/u/deeparaj)\
**Post date:** [February 24, 2009, 7:10am UTC](https://www.ruby-forum.com/t/save-an-attachment-file-into-the-folder/161102/3 "2009-02-24T07:10:36Z")

</div>

Hi,

```
   I got the file name but I want to store that file in the 

```

specified folder.Thanks for helping me…
