How to Convert Email to PDF with PHP
This PHP walkthrough uses Guzzle to send the file and conversion options. It converts an Email (.eml) file into a PDF through the pdfRest Convert to PDF API Tool. The complete sample is included below so PHP developers can see the file handling, request construction, and response path together.
Why Email to PDF with PHP?
PHP portals and workflow applications often receive Email files as part of customer cases, requests, or uploaded evidence. Creating a PDF copy gives staff a predictable document to review without asking them to open a mailbox-format file.
A web-based legal-intake portal could take an .eml message submitted with a matter, send it to pdfRest from a PHP worker, and associate the output resource with the case record. That supports a document-oriented review process while retaining the original input separately.
Guzzle’s multipart API makes the request structure direct: stream the Email file, name the output, and process the returned JSON. The surrounding PHP application can then decide whether to download the result or pass its ID to another tool.
PHP Code Example for Email to PDF
require 'vendor/autoload.php'; use GuzzleHttp\Client; use GuzzleHttp\Psr7\Utils; // By default, we use the US-based API service. This is the primary endpoint for global use. $apiUrl = "https://api.pdfrest.com"; /* For GDPR compliance and enhanced performance for European users, you can switch to the EU-based service by uncommenting the URL below. * For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work */ //$apiUrl = "https://eu-api.pdfrest.com"; // This sample converts an Email (.eml) file to PDF by sending it directly in a // multipart /pdf request. $inputPath = '/path/to/sample.eml'; $multipart = [ ['name' => 'file', 'contents' => Utils::tryFopen($inputPath, 'r'), 'filename' => basename($inputPath), 'headers' => ['Content-Type' => 'message/rfc822']], ['name' => 'output', 'contents' => 'pdf_from_email'], ]; $response = (new Client())->post($apiUrl . '/pdf', ['headers' => ['Api-Key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', 'Accept' => 'application/json'], 'multipart' => $multipart]); echo $response->getBody();
Source: View the sample on GitHub
Breaking Down the Code
Configure the service and input
// By default, we use the US-based API service. This is the primary endpoint for global use. $apiUrl = "https://api.pdfrest.com"; /* For GDPR compliance and enhanced performance for European users, you can switch to the EU-based service by uncommenting the URL below. * For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work */ //$apiUrl = "https://eu-api.pdfrest.com";
For this Email to PDF example, PHP sends the multipart request to /pdf. The PHP samples use Guzzle for HTTP and PSR-7 stream utilities for uploads. Install Guzzle with Composer when moving the code outside the repository, and provide the API key through protected runtime configuration. The sample’s placeholder path and API key make the moving pieces visible; replace them with a real local path and protected runtime credential before using the request.
Upload the Email File
// multipart /pdf request. $inputPath = '/path/to/sample.eml'; $multipart = [ ['name' => 'file', 'contents' => Utils::tryFopen($inputPath, 'r'), 'filename' => basename($inputPath), 'headers' => ['Content-Type' => 'message/rfc822']], ['name' => 'output', 'contents' => 'pdf_from_email'], ]; $response = (new Client())->post($apiUrl . '/pdf', ['headers' => ['Api-Key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', 'Accept' => 'application/json'], 'multipart' => $multipart]); echo $response->getBody();
The PHP request places the Email source in the multipart part named file. Retain its .eml filename extension because Convert to PDF uses the uploaded name to identify the source type, then use the optional output field to choose the generated resource name without a PDF extension.
Build the multipart request
// multipart /pdf request. $inputPath = '/path/to/sample.eml'; $multipart = [ ['name' => 'file', 'contents' => Utils::tryFopen($inputPath, 'r'), 'filename' => basename($inputPath), 'headers' => ['Content-Type' => 'message/rfc822']], ['name' => 'output', 'contents' => 'pdf_from_email'], ]; $response = (new Client())->post($apiUrl . '/pdf', ['headers' => ['Api-Key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', 'Accept' => 'application/json'], 'multipart' => $multipart]); echo $response->getBody();
For the Email to PDF form, guzzle accepts multipart fields as an array. Each entry names a field and supplies either stream content for a file or a simple string for an option, then Guzzle produces the correct multipart boundary.
Read the output resource
['name' => 'file', 'contents' => Utils::tryFopen($inputPath, 'r'), 'filename' => basename($inputPath), 'headers' => ['Content-Type' => 'message/rfc822']], ['name' => 'output', 'contents' => 'pdf_from_email'], ]; $response = (new Client())->post($apiUrl . '/pdf', ['headers' => ['Api-Key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', 'Accept' => 'application/json'], 'multipart' => $multipart]); echo $response->getBody();
After the Email to PDF call, the example prints Guzzle’s response body. A production caller can decode that JSON, retain outputId, and branch on an unsuccessful response instead of assuming every submitted conversion completed.
Beyond the Tutorial
In this PHP tutorial, you converted an Email file into a PDF resource with a direct multipart request. The pattern gives a message-based workflow a document output that can be reviewed, downloaded, or passed to another pdfRest operation.
Try representative email to pdf inputs in API Lab. For the authoritative request fields, accepted values, and response contract, consult the Convert to PDF API Tool documentation. The same request pattern can then be adapted to your PHP application’s error handling, retention policy, and delivery workflow.
The repository also provides a JSON-payload Email-to-PDF version for PHP. It uploads the message first and then posts its resource ID to /pdf, which is useful when an application stages inputs for several later operations. View the JSON-payload sample on GitHub.