How to Convert an EML File to PDF in Go

Cloudmersive
2 min readJul 15, 2021

--

The EML format was developed by Microsoft in the 1980s to comply with the industry-message standard, and while it is the go-to format for Outlook users, you may run into compatibility issues with other clients. Fortunately, in today’s tutorial we will provide an API solution that can be used in Go to automatically convert an EML file to the popular PDF format, allowing you to share the file without any difficulty.

Let’s dive right in by inputting the EML file and API key into the below code; this will call the API function and perform the conversion in no time at all.

package mainimport (
"fmt"
"bytes"
"mime/multipart"
"os"
"path/filepath"
"io"
"net/http"
"io/ioutil"
)
func main() {url := "https://api.cloudmersive.com/convert/eml/to/pdf"
method := "POST"
payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload)
file, errFile1 := os.Open("/path/to/file")
defer file.Close()
part1,
errFile1 := writer.CreateFormFile("inputFile",filepath.Base("/path/to/file"))
_, errFile1 = io.Copy(part1, file)
if errFile1 != nil {
fmt.Println(errFile1)
return
}
err := writer.Close()
if err != nil {
fmt.Println(err)
return
}
client := &http.Client {
}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("bodyOnly", "<boolean>")
req.Header.Add("Content-Type", "multipart/form-data")
req.Header.Add("Apikey", "YOUR-API-KEY-HERE")
req.Header.Set("Content-Type", writer.FormDataContentType())
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}

And that’s really you need to do! To retrieve your API key, visit the Cloudmersive website and register for a free account; this will provide 800 monthly calls across our library of APIs.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet