func (msg *MimeMessage) EncodeToMime() ([]byte, error) {
	if len(msg.Blocks) < 1 {
		return nil, errors.New("not a valid MimeMessage")
	}

	// If there is a mimeparts block we will use that to
	// recreate the MIME message
	if block, err := getBlock("mimeparts", msg.Blocks); err == nil {
		m, err := encodeWithMimeBlock(block, msg)
		if err != nil {
			return nil, err
		}

		return []byte(generateHeader(msg) + m), nil
	}

	// There is no mimeparts block, convert a standard Bitmaelum message to
	// a MIME valid message
	var mimeMsg string
	if len(msg.Blocks) > 1 || len(msg.Attachments) > 0 {
		mimeMsg = "Content-Type: multipart/mixed; boundary=\"" + dummyBoundary + "\"\r\n\r\n"
		for _, block := range msg.Blocks {
			parts := strings.SplitN(block, ",", 2)
			if parts[0] == "default" {
				parts[0] = "text/plain"
			}
			mimeMsg = mimeMsg + "--" + dummyBoundary + "\r\n"
			if strings.HasPrefix(parts[0], "text") {
				mimeMsg = mimeMsg + "Content-Type: " + parts[0] + "\r\n\r\n" + parts[1] + "\r\n\r\n"
			} else {
				mimeMsg = mimeMsg + "Content-Type: application/octet-stream; name=\"" + parts[0] + ".dat\"\r\n"
				mimeMsg = mimeMsg + "Content-Disposition: attachment; filename=\"" + parts[0] + ".dat\"\r\n"
				mimeMsg = mimeMsg + "Content-Transfer-Encoding: base64\r\n\r\n" + string(internal.Encode([]byte(parts[1]))) + "\r\n"
			}
		}
		for name, attachment := range msg.Attachments {
			mimeMsg = mimeMsg + "--" + dummyBoundary + "\r\n"
			mimeMsg = mimeMsg + "Content-Type: application/octet-stream; name=\"" + name + "\"\r\n"
			mimeMsg = mimeMsg + "Content-Disposition: attachment; filename=\"" + name + "\"\r\n"
			mimeMsg = mimeMsg + "Content-Transfer-Encoding: base64\r\n\r\n" + string(attachment) + "\r\n"
		}

		mimeMsg = mimeMsg + "--" + dummyBoundary + "--"
	} else {
		parts := strings.SplitN(msg.Blocks[0], ",", 2)
		if parts[0] == "default" {
			parts[0] = "text/plain"
		}

		mimeMsg = "Content-Type: " + parts[0] + "\r\n\r\n"
		mimeMsg = mimeMsg + strings.SplitN(msg.Blocks[0], ",", 2)[1] + "\r\n"
	}

	return []byte(generateHeader(msg) + mimeMsg), nil
}
// EncodeToMime will encode a MimeMessage struct to a MIME message
EncodeToMime will encode a MimeMessage struct to a MIME message
// EncodeToMime will encode a MimeMessage struct to a MIME message
func (msg *MimeMessage) EncodeToMime() ([]byte, error) {
	if len(msg.Blocks) < 1 {
		return nil, errors.New("not a valid MimeMessage")
	}
	if block, err := getBlock("mimeparts", msg.Blocks); err == nil {
		m, err := encodeWithMimeBlock(block, msg)
		if err != nil {
			return nil, err
		}
		return []byte(generateHeader(msg) + m), nil
	}
	var mimeMsg string
	if len(msg.Blocks) > 1 || len(msg.Attachments) > 0 {
		mimeMsg = "Content-Type: multipart/mixed; boundary=\"" + dummyBoundary + "\"\r\n\r\n"
		for _, block := range msg.Blocks {
			parts := strings.SplitN(block, ",", 2)
			if parts[0] == "default" {
				parts[0] = "text/plain"
			}
			mimeMsg = mimeMsg + "--" + dummyBoundary + "\r\n"
			if strings.HasPrefix(parts[0], "text") {
				mimeMsg = mimeMsg + "Content-Type: " + parts[0] + "\r\n\r\n" + parts[1] + "\r\n\r\n"
			} else {
				mimeMsg = mimeMsg + "Content-Type: application/octet-stream; name=\"" + parts[0] + ".dat\"\r\n"
				mimeMsg = mimeMsg + "Content-Disposition: attachment; filename=\"" + parts[0] + ".dat\"\r\n"
				mimeMsg = mimeMsg + "Content-Transfer-Encoding: base64\r\n\r\n" + string(internal.Encode([]byte(parts[1]))) + "\r\n"
			}
		}
		for name, attachment := range msg.Attachments {
			mimeMsg = mimeMsg + "--" + dummyBoundary + "\r\n"
			mimeMsg = mimeMsg + "Content-Type: application/octet-stream; name=\"" + name + "\"\r\n"
			mimeMsg = mimeMsg + "Content-Disposition: attachment; filename=\"" + name + "\"\r\n"
			mimeMsg = mimeMsg + "Content-Transfer-Encoding: base64\r\n\r\n" + string(attachment) + "\r\n"
		}
		mimeMsg = mimeMsg + "--" + dummyBoundary + "--"
	} else {
		parts := strings.SplitN(msg.Blocks[0], ",", 2)
		if parts[0] == "default" {
			parts[0] = "text/plain"
		}
		mimeMsg = "Content-Type: " + parts[0] + "\r\n\r\n"
		mimeMsg = mimeMsg + strings.SplitN(msg.Blocks[0], ",", 2)[1] + "\r\n"
	}
	return []byte(generateHeader(msg) + mimeMsg), nil
}
func ( msg * MimeMessage ) EncodeToMime ( ) ( [ ] byte , error ) { if len ( msg . Blocks ) < 1 { return nil , errors . New ( " " ) } if block , err := getBlock ( " " , msg . Blocks ) ; err == nil { m , err := encodeWithMimeBlock ( block , msg ) if err != nil { return nil , err } return [ ] byte ( generateHeader ( msg ) + m ) , nil } var mimeMsg string if len ( msg . Blocks ) > 1 || len ( msg . Attachments ) > 0 { mimeMsg = " \" " + dummyBoundary + " \" \r \n \r \n " for _ , block := range msg . Blocks { parts := strings . SplitN ( block , " " , 2 ) if parts [ 0 ] == " " { parts [ 0 ] = " " } mimeMsg = mimeMsg + " " + dummyBoundary + " \r \n " if strings . HasPrefix ( parts [ 0 ] , " " ) { mimeMsg = mimeMsg + " " + parts [ 0 ] + " \r \n \r \n " + parts [ 1 ] + " \r \n \r \n " } else { mimeMsg = mimeMsg + " \" " + parts [ 0 ] + " \" \r \n " mimeMsg = mimeMsg + " \" " + parts [ 0 ] + " \" \r \n " mimeMsg = mimeMsg + " \r \n \r \n " + string ( internal . Encode ( [ ] byte ( parts [ 1 ] ) ) ) + " \r \n " } } for name , attachment := range msg . Attachments { mimeMsg = mimeMsg + " " + dummyBoundary + " \r \n " mimeMsg = mimeMsg + " \" " + name + " \" \r \n " mimeMsg = mimeMsg + " \" " + name + " \" \r \n " mimeMsg = mimeMsg + " \r \n \r \n " + string ( attachment ) + " \r \n " } mimeMsg = mimeMsg + " " + dummyBoundary + " " } else { parts := strings . SplitN ( msg . Blocks [ 0 ] , " " , 2 ) if parts [ 0 ] == " " { parts [ 0 ] = " " } mimeMsg = " " + parts [ 0 ] + " \r \n \r \n " mimeMsg = mimeMsg + strings . SplitN ( msg . Blocks [ 0 ] , " " , 2 ) [ 1 ] + " \r \n " } return [ ] byte ( generateHeader ( msg ) + mimeMsg ) , nil }
EncodeToMime will encode a MimeMessage struct to a MIME message
EncodeToMime will encode a MimeMessage struct to a MIME message
// If there is a mimeparts block we will use that to // recreate the MIME message // There is no mimeparts block, convert a standard Bitmaelum message to // a MIME valid message