lintingy
lintingy

Reputation: 21

Ruby, how to correctly decode mails with MIME multipart?

I'm trying to write a system which could pull mails from gmail and get the content in Ruby. (using imap or pop)
And as far as I know, there are 'ruby-gmail', 'mail' (the newer version of tmail) and 'action mailer' that might help me to do this.

I'm now trying 'mail' and 'ruby-gmail', and I use the decoded function like this:

gmail.inbox.emails[0].body.decoded

But some mails could be correctly decoded, but some couldn't.
The output of the decoded mail looks like this:

This is MIME multipart 6.
--__=_Part_Boundary_002_310696381.907173471
Content-Type: text/plain;
    charset="big5"
Content-Transfer-Encoding: quoted-printable

=AE=BC=A5=BF=A7A=A6n,
.......(some encoded content)

And to some other mails, the Content-Transfer-Encoding are base64.

Is there any better way to correctly decode the mails?
Or I just need to read into the mail, get the encoded part, and use Base64.decode64 or unpack.("M") to decode the mail?

Upvotes: 2

Views: 6564

Answers (1)

Luis
Luis

Reputation: 1302

I don't know about 'gmail' gem, but 'mail' one works pretty well. Something like

require 'mail'
mail = Mail.new(mail_text)
mail.parts[0].body.decoded

should work (use 'n' instead of 0 for other parts)

Also be aware that it could be an attachment, so you'd need mail.attachments[0].decoded

Upvotes: 10

Related Questions