//根据编码格式获取转码后的主题内容
func getRealSubject(lang, encoding, content string) []byte {
	encoding = strings.ToLower(encoding)
	if encoding == "b" {
		decoded, err := base64.StdEncoding.DecodeString(content)
		if err != nil {
			log.Printf("decode error:%s", err)
			return nil
		}

		if lang == "utf-8" {
			log.Printf(string(decoded))
			return decoded
		}

		//非u8编码语言需要统一转为u8
		dec := mahonia.NewDecoder(lang)
		if decoded, ok := dec.ConvertStringOK(string(decoded)); ok {
			log.Printf(string(decoded))
		}

		return decoded
	} else if encoding == "q" {
		return nil
	} else {
		return nil
	}

}
Beispiel #2
0
func getDecodingContent(lang, encoding, content string) string {

	if encoding == "b" {
		decoded, err := base64.StdEncoding.DecodeString(content)
		if err != nil {
			log.Printf("decode error:%s", err)
			return ""
		}

		if lang == "utf-8" {
			//log.Printf("utf-8, noneed to convert: %s\n", string(decoded))
			return string(decoded)
		}

		//非u8编码语言需要统一转为u8
		dec := mahonia.NewDecoder(lang)
		if u8Decoded, ok := dec.ConvertStringOK(string(decoded)); ok {
			//log.Printf("%s convert to utf-8: %s\n", lang, string(decoded))
			return string(u8Decoded) //注意作用域
		}

	} else if encoding == "q" {
		//通过字符串构造Reader
		r := quotedprintable.NewReader(strings.NewReader(content))
		body, err := ioutil.ReadAll(r)
		if err != nil {
			log.Printf("read content error: %s", err)
			return ""
		}
		log.Printf("quoted decode: %s\n", string(body))
		return string(body)
	}

	//转换失败,返回空
	return ""
}