Code Monkey home page Code Monkey logo

jmail's Introduction

jmail

メールの内容を解析したい場合、net/mailパッケージだと、そのままでは日本語メールを読めないですよね。

件名と本文をデコードして出力するメソッドを追加してみました。

net/mailパッケージの構造体を埋め込んで実装しているので、同じように使えます。

$ go get github.com/curious-eyes/jmail

下記例のコードはutf-8ですが、iso-2022-jpの場合でも正しく出力されます。

package main

import (
  "fmt"
  mail "github.com/curious-eyes/jmail"
  "log"
  "strings"
)

func main() {
  msg := `Date: Wed, 16 Sep 2015 05:32:06 +0900
From: Gopher <[email protected]>
To: Another Gopher <[email protected]>
Subject: =?UTF-8?Q?=E7=94=9F=E6=B4=BB=E3=81=AE=E3=81=BB=E3=81=A8=E3=82=93=E3=81=A9=E3=82=92?=
 =?UTF-8?Q?=E5=9C=B0=E4=B8=AD=E3=81=A7=E9=81=8E=E3=81=94=E3=81=99?=
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

=E3=83=9B=E3=83=AA=E3=83=8D=E3=82=BA=E3=83=9F=20=28=E8=8B=B1=3A=20Gopher=
=29=E3=81=AF=E3=80=81=E5=93=BA=E4=B9=B3=E7=B6=B1=E3=83=8D=E3=82=BA=E3=83=
=9F=E7=9B=AE=EF=BC=88=E9=BD=A7=E6=AD=AF=E7=9B=AE=EF=BC=89=E3=83=8D=E3=82=
=BA=E3=83=9F=E4=BA=9C=E7=9B=AE=E3=83=9B=E3=83=AA=E3=83=8D=E3=82=BA=E3=83=
=9F=E7=A7=91=E3=81=AB=E5=B1=9E=E3=81=99=E3=82=8B=E5=93=BA=E4=B9=B3=E9=A1=
=9E=E3=81=AE=E7=B7=8F=E7=A7=B0
`

  r := strings.NewReader(msg)
  m, err := mail.ReadMessage(r)
  if err != nil {
    log.Fatal(err)
  }

  header := m.Header
  fmt.Println("Date:", header.Get("Date"))
  fmt.Println("From:", header.Get("From"))
  fmt.Println("To:", header.Get("To"))

  fmt.Println("Subject:", m.DecSubject())
  body, err := m.DecBody()
  if err != nil {
    log.Fatal(err)
  }
  fmt.Printf("%s", body)

  // Output:
  // Date: Wed, 16 Sep 2015 05:32:06 +0900
  // From: Gopher <[email protected]>
  // To: Another Gopher <[email protected]>
  // Subject: 生活のほとんどを地中で過ごす
  // ホリネズミ (英: Gopher)は、哺乳綱ネズミ目(齧歯目)ネズミ亜目ホリネズミ科に属する哺乳類の総称
}

Subject(件名)と Body(本文)をデコードしたい場合だけ、

  • DecSubject
  • DecBody

を使ってください。

From, To アドレスリストもデコードできればいいですね。

今どきメールを扱うことなんて、無いかもしれませんけどね。

jmail's People

Contributors

curious-eyes avatar dozen avatar

Stargazers

Roman avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.