12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- package mail
- import (
- "fmt"
- "golang.org/x/text/encoding/charmap"
- "golang.org/x/text/encoding/japanese"
- "golang.org/x/text/encoding/korean"
- "golang.org/x/text/encoding/simplifiedchinese"
- "golang.org/x/text/transform"
- "io"
- "strings"
- )
- var charsetMap = map[string]transform.Transformer{
- "gb2312": simplifiedchinese.GBK.NewDecoder(),
- "gbk": simplifiedchinese.GBK.NewDecoder(),
- "ibm037": charmap.CodePage037.NewDecoder(),
- "ibm437": charmap.CodePage437.NewDecoder(),
- "ibm850": charmap.CodePage850.NewDecoder(),
- "ibm852": charmap.CodePage852.NewDecoder(),
- "ibm855": charmap.CodePage855.NewDecoder(),
- "ibm858": charmap.CodePage858.NewDecoder(),
- "ibm860": charmap.CodePage860.NewDecoder(),
- "ibm862": charmap.CodePage862.NewDecoder(),
- "ibm863": charmap.CodePage863.NewDecoder(),
- "ibm865": charmap.CodePage865.NewDecoder(),
- "ibm866": charmap.CodePage866.NewDecoder(),
- "ibm1047": charmap.CodePage1047.NewDecoder(),
- "ibm1140": charmap.CodePage1140.NewDecoder(),
- "iso-8859-1": charmap.ISO8859_1.NewDecoder(),
- "iso-8859-2": charmap.ISO8859_2.NewDecoder(),
- "iso-8859-3": charmap.ISO8859_3.NewDecoder(),
- "iso-8859-4": charmap.ISO8859_4.NewDecoder(),
- "iso-8859-5": charmap.ISO8859_5.NewDecoder(),
- "iso-8859-6": charmap.ISO8859_6.NewDecoder(),
- "iso-8859-7": charmap.ISO8859_7.NewDecoder(),
- "iso-8859-8": charmap.ISO8859_8.NewDecoder(),
- "iso-8859-9": charmap.ISO8859_9.NewDecoder(),
- "iso-8859-10": charmap.ISO8859_10.NewDecoder(),
- "iso-8859-13": charmap.ISO8859_13.NewDecoder(),
- "iso-8859-14": charmap.ISO8859_14.NewDecoder(),
- "iso-8859-15": charmap.ISO8859_15.NewDecoder(),
- "iso-8859-16": charmap.ISO8859_16.NewDecoder(),
- "koi8-r": charmap.KOI8R.NewDecoder(),
- "koi8-u": charmap.KOI8U.NewDecoder(),
- "macintosh": charmap.Macintosh.NewDecoder(),
- "x-mac-cyrillic": charmap.MacintoshCyrillic.NewDecoder(),
- "windows-874": charmap.Windows874.NewDecoder(),
- "windows-1250": charmap.Windows1250.NewDecoder(),
- "windows-1251": charmap.Windows1251.NewDecoder(),
- "windows-1252": charmap.Windows1252.NewDecoder(),
- "windows-1253": charmap.Windows1253.NewDecoder(),
- "windows-1254": charmap.Windows1254.NewDecoder(),
- "windows-1255": charmap.Windows1255.NewDecoder(),
- "windows-1257": charmap.Windows1257.NewDecoder(),
- "windows-1258": charmap.Windows1258.NewDecoder(),
- "x-user-defined": charmap.XUserDefined.NewDecoder(),
- "euc-jp": japanese.EUCJP.NewDecoder(),
- "iso-2022-jp": japanese.ISO2022JP.NewDecoder(),
- "shift_jis": japanese.ShiftJIS.NewDecoder(),
- "ks_c_5601-1987": korean.EUCKR.NewDecoder(),
- "euc-kr": korean.EUCKR.NewDecoder(),
- }
- // 定义一个自定义的 CharsetReader 函数,它能够处理 gb2312 和 gbk 字符集
- func myCharsetReader(charset string, input io.Reader) (io.Reader, error) {
- charset = strings.ToLower(charset)
- newDecoder, ok := charsetMap[charset]
- if ok {
- reader := transform.NewReader(input, newDecoder)
- return reader, nil
- }
- if charset == `utf-8` {
- return input, nil
- }
- switch strings.ToLower(charset) {
- case "gb2312", "gbk":
- reader := transform.NewReader(input, simplifiedchinese.GBK.NewDecoder())
- return reader, nil
- case "utf-8":
- return input, nil
- case "iso-8859-1":
- reader := transform.NewReader(input, charmap.ISO8859_1.NewDecoder())
- return reader, nil
- case "windows-1252":
- reader := transform.NewReader(input, charmap.Windows1252.NewDecoder())
- return reader, nil
- default:
- }
- return input, fmt.Errorf("unsupported charset: %s", charset)
- }
|