当前位置: 首页>>代码示例>>Golang>>正文


Golang WordDecoder.DecodeHeader方法代码示例

本文整理汇总了Golang中mime.WordDecoder.DecodeHeader方法的典型用法代码示例。如果您正苦于以下问题:Golang WordDecoder.DecodeHeader方法的具体用法?Golang WordDecoder.DecodeHeader怎么用?Golang WordDecoder.DecodeHeader使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在mime.WordDecoder的用法示例。


在下文中一共展示了WordDecoder.DecodeHeader方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: getSubject

func getSubject(msg *mail.Message) (string, error) {
	var dec mime.WordDecoder
	ret, err := dec.DecodeHeader(msg.Header.Get("Subject"))
	if err != nil {
		return "", err
	}
	return ret, nil
}
开发者ID:koffeinsource,项目名称:kaffeeshare,代码行数:8,代码来源:subject.go

示例2: DecodeHeader

// DecodeHeader (per RFC 2047) using Golang's mime.WordDecoder
func DecodeHeader(input string) string {
	if !strings.Contains(input, "=?") {
		// Don't scan if there is nothing to do here
		return input
	}

	dec := new(mime.WordDecoder)
	dec.CharsetReader = NewCharsetReader
	header, err := dec.DecodeHeader(input)
	if err != nil {
		return input
	}
	return header
}
开发者ID:davrux,项目名称:go.enmime,代码行数:15,代码来源:header.go

示例3: parseAndDecodeHeader

func parseAndDecodeHeader(rawMimeHeader textproto.MIMEHeader, target string,
	mHeader PMIMEHeader) string {
	if targetArray, ok := rawMimeHeader[target]; !ok && len(targetArray) == 0 {
		return ""
	} else {
		var (
			encodedValue string = strings.TrimPrefix(targetArray[0], " ")
			decoded      string
			dec          *mime.WordDecoder = new(mime.WordDecoder)
			err          error
		)
		if decoded, err = dec.DecodeHeader(encodedValue); err != nil {
			fmt.Printf("[watney] WARNING: Couldn't decode string: \n\t%s\n\t%s\n", encodedValue,
				err.Error())
			return encodedValue
		}
		return decoded
	}
}
开发者ID:mdrobek,项目名称:watney,代码行数:19,代码来源:mail_parse.go

示例4: ExampleWordDecoder_DecodeHeader

func ExampleWordDecoder_DecodeHeader() {
	dec := new(mime.WordDecoder)
	header, err := dec.DecodeHeader("=?utf-8?q?=C3=89ric?= <[email protected]>, =?utf-8?q?Ana=C3=AFs?= <[email protected]>")
	if err != nil {
		panic(err)
	}
	fmt.Println(header)

	header, err = dec.DecodeHeader("=?utf-8?q?=C2=A1Hola,?= =?utf-8?q?_se=C3=B1or!?=")
	if err != nil {
		panic(err)
	}
	fmt.Println(header)

	dec.CharsetReader = func(charset string, input io.Reader) (io.Reader, error) {
		switch charset {
		case "x-case":
			// Fake character set for example.
			// Real use would integrate with packages such
			// as code.google.com/p/go-charset
			content, err := ioutil.ReadAll(input)
			if err != nil {
				return nil, err
			}
			return bytes.NewReader(bytes.ToUpper(content)), nil
		default:
			return nil, fmt.Errorf("unhandled charset %q", charset)
		}
	}
	header, err = dec.DecodeHeader("=?x-case?q?hello_?= =?x-case?q?world!?=")
	if err != nil {
		panic(err)
	}
	fmt.Println(header)
	// Output:
	// Éric <[email protected]>, Anaïs <[email protected]>
	// ¡Hola, señor!
	// HELLO WORLD!
}
开发者ID:RajibTheKing,项目名称:gcc,代码行数:39,代码来源:example_test.go

示例5: Parse

// Parse parses a MIME encoded message.
func Parse(r io.Reader) (
	header *Header,
	subject string,
	message string,
	attachments []*Attachment,
	err error,
) {
	var h Header
	// read message
	msg, err := mail.ReadMessage(r)
	if err != nil {
		return nil, "", "", nil, log.Error(err)
	}
	// parse 'From'
	h.From = msg.Header.Get("From")
	if h.From == "" {
		return nil, "", "", nil, log.Error("mime: 'From' not defined")
	}
	// parse 'To'
	h.To = msg.Header.Get("To")
	if h.To == "" {
		return nil, "", "", nil, log.Error("mime: 'To' not defined")
	}
	// parse 'Cc'
	addressList, err := msg.Header.AddressList("Cc")
	if err != nil && err != mail.ErrHeaderNotPresent {
		return nil, "", "", nil, log.Error(err)
	}
	if err != mail.ErrHeaderNotPresent {
		for _, address := range addressList {
			h.Cc = append(h.Cc, address.Address)
		}
	}
	// parse subject
	subj := msg.Header.Get("Subject")
	if subj == "" {
		return nil, "", "", nil, log.Error("mime: 'Subject' not defined")
	}
	dec := new(mime.WordDecoder)
	subject, err = dec.DecodeHeader(subj)
	if err != nil {
		return nil, "", "", nil, log.Error(err)
	}
	// parse 'Message-ID'
	h.MessageID = msg.Header.Get("Message-ID")
	if h.MessageID == "" {
		return nil, "", "", nil, log.Error("mime: 'Message-ID' not defined")
	}
	// parse 'In-Reply-To'
	h.InReplyTo = msg.Header.Get("In-Reply-To")
	// parse 'MIME-Version'
	if msg.Header.Get("MIME-Version") != "1.0" {
		return nil, "", "", nil, log.Error("mime: wrong 'MIME-Version' header")
	}
	// parse 'Content-Type'
	mediaType, params, err := mime.ParseMediaType(msg.Header.Get("Content-Type"))
	if err != nil {
		return nil, "", "", nil, log.Error(err)
	} else if mediaType != "multipart/mixed" {
		return nil, "", "", nil, log.Error("mime: wrong 'Content-Type' header ")
	}
	// read first MIME part (message)
	mr := multipart.NewReader(msg.Body, params["boundary"])
	p, err := mr.NextPart()
	if err != nil {
		return nil, "", "", nil, log.Error(err)
	}
	// check 'Content-Type'
	if p.Header.Get("Content-Type") != "text/plain" {
		return nil, "", "", nil,
			log.Error("mime: expected 'text/plain' Content-Type")
	}
	// check 'Content-Transfer-Encoding'
	if p.Header.Get("Content-Transfer-Encoding") != "base64" {
		return nil, "", "", nil,
			log.Error("mime: expected 'base64' Content-Transfer-Encoding")
	}
	// read message
	enc, err := ioutil.ReadAll(p)
	if err != nil {
		return nil, "", "", nil, log.Error(err)
	}
	content, err := base64.Decode(string(enc))
	if err != nil {
		return nil, "", "", nil, log.Error(err)
	}
	message = string(content)
	// read optional additional MIME parts (attachments)
	for {
		p, err := mr.NextPart()
		if err == io.EOF {
			break
		}
		// parse header
		contentType := p.Header.Get("Content-Type")
		if contentType == "" {
			return nil, "", "", nil,
				log.Error("mime: Content-Type undefined for attachment")
		}
//.........这里部分代码省略.........
开发者ID:JonathanLogan,项目名称:mute,代码行数:101,代码来源:mime.go


注:本文中的mime.WordDecoder.DecodeHeader方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。