# Howto decode encoded mail subjects?

**URL:** <https://www.ruby-forum.com/t/howto-decode-encoded-mail-subjects/113103>\
**Category:** Ruby\
**Created:** [August 27, 2007, 5:49pm UTC](https://www.ruby-forum.com/t/howto-decode-encoded-mail-subjects/113103 "2007-08-27T17:49:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pure](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pure](https://www.ruby-forum.com/u/pure)\
**Post date:** [August 27, 2007, 5:49pm UTC](https://www.ruby-forum.com/t/howto-decode-encoded-mail-subjects/113103/1 "2007-08-27T17:49:52Z")

</div>

Hi,

i need to parse mails. Some of the Mails in question use some ending  
scheme to support sepcial characters which are not ASCII-7.

encoded chars look like this: =?UTF-8?Q?=20some text…?UTF-?Q?..

Is there a way to decode these subjects soemhow to real utf? Then I  
could use Iconv to change the encoding as i like.

Any tips appreciated!

Cheers,

Steph.

---

<div class="post-metadata">

**Author:** ![pure](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pure](https://www.ruby-forum.com/u/pure)\
**Post date:** [August 27, 2007, 9:18pm UTC](https://www.ruby-forum.com/t/howto-decode-encoded-mail-subjects/113103/2 "2007-08-27T21:18:18Z")

</div>

Hi,

Am Dienstag, 28. Aug 2007, 00:48:39 +0900 schrieb Stephan M.:

> i need to parse mails. Some of the Mails in question use some ending  
> scheme to support sepcial characters which are not ASCII-7.
> 
> encoded chars look like this: =?UTF-8?Q?=20some text…?UTF-?Q?..
> 
> Is there a way to decode these subjects soemhow to real utf? Then I  
> could use Iconv to change the encoding as i like.

Half a year ago I wrote me a mail filter that does much more  
than only decode header fields. I post some example code for  
your task below.

I use the filter every day since last December. I provide it  
as open source but I think nobody else is using it. So I  
cannot estimate how good the documentation and the  
installation tools are.

Bertram

* * *

require “bs-net/mail”  
include BsNet

mb = MBox.new “~/Mail/lists/ruby-talk”

# Variant #1

froms = mb.map { |text|  
mail = Mail.new text  
mail.addrs( :from).map { |plain,long,group| long }  
}  
froms.flatten!  
puts froms.uniq.sort

puts “-”\*32

# Variant #2

froms = mb.map { |text|  
mail = Mail.new text  
mail.addrs\_norm :from # plain lower case addresses  
}  
froms.flatten!  
puts froms.uniq.sort

puts “-”\*32

# Variant #3

froms = []  
mb.map { |text|  
mail = Mail.new text  
f = mail.addrs :from  
froms.concat f.to\_a  
}  
froms.uniq.sort.each { |(plain,long,group)|  
puts “#{plain} – #{long} – #{group}”  
}

---

<div class="post-metadata">

**Author:** ![pure](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pure](https://www.ruby-forum.com/u/pure)\
**Post date:** [August 28, 2007, 7:19am UTC](https://www.ruby-forum.com/t/howto-decode-encoded-mail-subjects/113103/3 "2007-08-28T07:19:04Z")

</div>

Hi Bertram,

- Bertram S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) [27.08.2007]:

> I use the filter every day since last December. I provide it  
> as open source but I think nobody else is using it. So I  
> cannot estimate how good the documentation and the  
> installation tools are.

thanks for your examples on how to use bs-net. One question remains:  
Where can i find bs-net? 🙂

Cheers,

Steph.

---

<div class="post-metadata">

**Author:** ![pure](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pure](https://www.ruby-forum.com/u/pure)\
**Post date:** [August 28, 2007, 10:51am UTC](https://www.ruby-forum.com/t/howto-decode-encoded-mail-subjects/113103/4 "2007-08-28T10:51:38Z")

</div>

Hi,

Am Dienstag, 28. Aug 2007, 14:18:10 +0900 schrieb Stephan M.:

> - Bertram S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) [27.08.2007]:
> 
> > I use the filter every day since last December. I provide it  
> > as open source but I think nobody else is using it. So I  
> > cannot estimate how good the documentation and the  
> > installation tools are.
> 
> thanks for your examples on how to use bs-net. One question remains:  
> Where can i find bs-net? 🙂

Oops. I just forgot the link:

[http://opensource.bertram-scharpf.de/sites/cropmail](http://opensource.bertram-scharpf.de/sites/cropmail)

Bertram

---

<div class="post-metadata">

**Author:** ![pure](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pure](https://www.ruby-forum.com/u/pure)\
**Post date:** [August 29, 2007, 10:15am UTC](https://www.ruby-forum.com/t/howto-decode-encoded-mail-subjects/113103/5 "2007-08-29T10:15:44Z")

</div>

Hi,

- Bertram S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) [28.08.2007]:

> Oops. I just forgot the link:
> 
> [http://opensource.bertram-scharpf.de/sites/cropmail](http://opensource.bertram-scharpf.de/sites/cropmail)

thank you for providing the link. Maybe you should announce cropmail on  
rubyforge to gain more users. I guess nowadays most people look on  
rubyforge first when looking for ruby-related stuff.

Cheers,

Steph.
