[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Save an attachment file into the folder

Deepa Rajendran

2/23/2009 11:39:00 AM

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',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
--
Posted via http://www.ruby-....

1 Answer

Heesob Park

2/23/2009 12:25:00 PM

0

Hi,

2009/2/23 Deepa Rajendran <deeparaj12@gmail.com>:
> 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',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
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', 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 Heesob