0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ruby mail gem でPOP3メール受信&削除&添付保存

Last updated at Posted at 2019-09-01

Rubyのmail gemでメール受信&削除をしつつ添付ファイルを保存する話

#! /usr/local/bin/ruby
require 'mail'

Mail.defaults {
	retriever_method(:pop3,address:"127.0.0.1",port:110,user_name:popus,password:poppw)  
}
Mail.find_and_delete(what: :first,find_and_delete:true).each do |m|
	m.parts.each do |p| 
		File.open('/tmp/' + p.filename , "w+b" , 0644) {|f| f.write p.decoded} if p.attachment?
	end
end

こんな簡単にできるなんて嬉しい。
会社のメール処理自働化進みます。

なんでこんな追加要素がほとんどないページを書いたかというと、mail gemでサーバからの削除方法がgithubに書いてなかったからなんです。
おまけにgoogleでググっても日本語が引っかからない。

find_and_deleteメソッドですからね!

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?