How to Convert an Email MSG file to a PDF Document
If Microsoft Outlook is your email app of choice, you’re likely familiar with MSG message format, which is used to store emails, appointments, contacts, tasks, and various other administrative details in your mailbox. Much like the more broadly used EML format, MSG files can be converted into new formats for storage redundancy, content sharing, and any other purpose you can think of. Our MSG to PDF API makes this conversion process easy to accomplish at scale, providing a swift & secure service which captures your input file and returns the encoding for a new PDF document. In addition, this API provides the option to either include or leave out extraneous message details such as the subject (please note: this information will instead be provided as “properties” in the API response object).
To take advantage of this API, all you need to do is register a free account on our website (this will provide an API key with a limit of 800 API calls per month) and follow the brief instructions provided below to structure your API call with ready-to-run, complementary Go code examples.
Calling this API is extremely straightforward; all you need to do is copy & paste the code below into your file. After that, configure your file path & API key headers, and if you want to remove the email subject from your resulting PDF file, set the bodyOnly header equal to true:
package main
import (
"fmt"
"bytes"
"mime/multipart"
"os"
"path/filepath"
"io"
"net/http"
"io/ioutil"
)
func main() {
url := "https://api.cloudmersive.com/convert/msg/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))
}
That’s all there is to it!