Created
February 22, 2014 11:45
-
-
Save masahide/9152644 to your computer and use it in GitHub Desktop.
go で utf8メールを送信 ref: http://qiita.com/yamasaki-masahide/items/a9f8b43eeeaddbfb6b44
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Subject: =?utf-8?B?6ZW344GE44K144OW44K444Kn44Kv44OI6ZW344GE44K144OW44K4?= | |
=?utf-8?B?44Kn44Kv44OI6ZW344GE44K144OW44K444Kn44Kv44OI6ZW344GE?= | |
=?utf-8?B?44K144OW44K444Kn44Kv44OI6ZW344GE44K144OW44K444Kn44Kv?= | |
=?utf-8?B?44OI6ZW344GE44K144OW44K444Kn44Kv44OI6ZW344GE44K144OW?= | |
=?utf-8?B?44K444Kn44Kv44OI?= | |
MIME-Version: 1.0 | |
Content-Type: text/plain; charset="utf-8" | |
Content-Transfer-Encoding: base64 | |
Message-Id: <20140222115231.8DECE260593@st> | |
Date: Sat, 22 Feb 2014 20:52:31 +0900 (JST) | |
5pys5paH5pys5paH5pys5paH5pys5paH5pys5paH5pys5paH5pys5paH5pys5paH5pys5paH5pys | |
5paH5pys5paH5pys5paH5pys5paH5pys5paH5pys5paH5pys5paH5pys5paH5pys5paHCuacrOaW | |
h+acrOaWh+acrOaWh+acrOaWh+acrOaWh+acrOaWh+acrOaWh+acrOaWh+acrOaWh+acrOaWh+ac | |
rOaWh+acrOaWh+acrOaWh+acrOaWh+acrOaWh+acrOaWh+acrOaWh+acrOaWhwo= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"encoding/base64" | |
"log" | |
"net/mail" | |
"net/smtp" | |
"strings" | |
) | |
// 76バイト毎にCRLFを挿入する | |
func add76crlf(msg string) string { | |
var buffer bytes.Buffer | |
for k, c := range strings.Split(msg, "") { | |
buffer.WriteString(c) | |
if k%76 == 75 { | |
buffer.WriteString("\r\n") | |
} | |
} | |
return buffer.String() | |
} | |
// UTF8文字列を指定文字数で分割 | |
func utf8Split(utf8string string, length int) []string { | |
resultString := []string{} | |
var buffer bytes.Buffer | |
for k, c := range strings.Split(utf8string, "") { | |
buffer.WriteString(c) | |
if k%length == length-1 { | |
resultString = append(resultString, buffer.String()) | |
buffer.Reset() | |
} | |
} | |
if buffer.Len() > 0 { | |
resultString = append(resultString, buffer.String()) | |
} | |
return resultString | |
} | |
// サブジェクトをMIMEエンコードする | |
func encodeSubject(subject string) string { | |
var buffer bytes.Buffer | |
buffer.WriteString("Subject:") | |
for _, line := range utf8Split(subject, 13) { | |
buffer.WriteString(" =?utf-8?B?") | |
buffer.WriteString(base64.StdEncoding.EncodeToString([]byte(line))) | |
buffer.WriteString("?=\r\n") | |
} | |
return buffer.String() | |
} | |
func main() { | |
from := mail.Address{"ふろむ", "from@hoge.fuga"} | |
to := mail.Address{"とぅー", "to@fuga.hoge"} | |
title := "長いサブジェクト長いサブジェクト長いサブジェクト長いサブジェクト長いサブジェクト長いサブジェクト長いサブジェクト" | |
var body bytes.Buffer | |
body.WriteString("本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文\n") | |
body.WriteString("本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文\n") | |
var header bytes.Buffer | |
header.WriteString("From: " + from.String() + "\r\n") | |
header.WriteString("To: " + to.String() + "\r\n") | |
header.WriteString(encodeSubject(title)) | |
header.WriteString("MIME-Version: 1.0\r\n") | |
header.WriteString("Content-Type: text/plain; charset=\"utf-8\"\r\n") | |
header.WriteString("Content-Transfer-Encoding: base64\r\n") | |
var message bytes.Buffer | |
message = header | |
message.WriteString("\r\n") | |
message.WriteString(add76crlf(base64.StdEncoding.EncodeToString(body.Bytes()))) | |
err := smtp.SendMail( | |
"localhost:25", | |
nil, | |
from.Address, | |
[]string{to.Address}, | |
[]byte(message.String()), | |
) | |
if err != nil { | |
log.Fatal(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment