How to generate and parse MIME messages with alternative content types (e.g., text and HTML) in Go?

To generate and parse MIME messages with alternative content types in Go, you can use the "mime/multipart" and "mime/textproto" packages. Here's an example code that demonstrates the process:

  1. Generating a MIME message:
package main import ( "bytes" "fmt" "io/ioutil" "mime/multipart" "net/mail" "net/textproto" ) func main() { // Create a buffer to store the message var message bytes.Buffer // Create a new multipart writer writer := multipart.NewWriter(&message) // Set the content type of the multipart message writer.SetBoundary("MyBoundary") // Create the first message part with text/plain content type part1 := make(textproto.MIMEHeader) part1.Set("Content-Type", "text/plain") // Write the content of the first message part part1Writer, _ := writer.CreatePart(part1) part1Writer.Write([]byte("This is the text part.")) // Create the second message part with text/html content type part2 := make(textproto.MIMEHeader) part2.Set("Content-Type", "text/html") // Write the content of the second message part part2Writer, _ := writer.CreatePart(part2) part2Writer.Write([]byte("<p>This is the HTML part.</p>")) // Close the multipart writer after writing all parts writer.Close() // Print the generated MIME message fmt.Println(message.String()) }
  1. Parsing a MIME message:
package main import ( "fmt" "io" "mime" "mime/multipart" "net/mail" ) func main() { // Example MIME message message := `Content-Type: multipart/alternative; boundary=MyBoundary --MyBoundary Content-Type: text/plain This is the text part. --MyBoundary Content-Type: text/html <p>This is the HTML part.</p> --MyBoundary--` // Parse the message parsedMessage, _ := mail.ReadMessage(bytes.NewBufferString(message)) // Get the content type of the message contentType := parsedMessage.Header.Get("Content-Type") // Check if the message is multipart isMultipart := false mediatype, params, err := mime.ParseMediaType(contentType) if err == nil && mediatype == "multipart/alternative" { isMultipart = true } if isMultipart { // Create a new multipart reader using the boundary specified in the content type multipartReader := multipart.NewReader(parsedMessage.Body, params["boundary"]) // Iterate over each part in the multipart message for { part, err := multipartReader.NextPart() if err == io.EOF { break } // Get the content type of the part partContentType := part.Header.Get("Content-Type") // Check if the part is text/plain if partContentType == "text/plain" { // Read and print the content of the part partContent, _ := ioutil.ReadAll(part) fmt.Println("Text Part:", string(partContent)) } // Check if the part is text/html if partContentType == "text/html" { // Read and print the content of the part partContent, _ := ioutil.ReadAll(part) fmt.Println("HTML Part:", string(partContent)) } } } }

This example demonstrates how to generate a MIME message with text and HTML parts, as well as how to parse such a message to retrieve the content of each part.