Link Search Menu Expand Document

Extract Email Attchments - Salesforce

Email Send and Decode sample in Salesforce demonstrating ‘Extract Email Attchments’

ExtractEmailAttachments.cls
public class ExtractEmailAttachments {

    String API_KEY = 'YOUR_PDFCO_API_KEY';
    string endPointUrl = 'https://api.pdf.co/v1/email/extract-attachments';

    public void extractAttachments()
    {
        try
        {
            String jsonBody = '{ "url": "https://pdfco-test-files.s3.us-west-2.amazonaws.com/email-extractor/sample.eml", "inline": true, "async": false }';
            Http http = new Http();
            HttpRequest request = new HttpRequest();            
            request.setHeader('x-api-key', API_KEY);
            request.setEndpoint(endPointUrl);
            request.setHeader('Content-Type', 'application/json');
            request.setMethod('POST');            
            request.setBody(jsonBody);            
            HttpResponse response =  http.send(request);            
            Map<String, Object> json = (Map<String, Object>)JSON.deserializeUntyped(response.getBody());            
            if(response.getStatusCode() == 200) 
            {
                if ((Boolean)json.get('error') == false)
                {
                    System.debug('response.getBody() :: '+response.getBody());
                }
            }
            else
            {
                System.debug('Error Response ' + response.getBody());
                System.Debug(' Status ' + response.getStatus());
                System.Debug(' Status Code' + response.getStatusCode());
                System.Debug(' Response String' + response.toString());
            }
        }
        catch (Exception ex)
        {
            String errorBody = 'Message: ' + ex.getMessage() + ' -- Cause: ' + ex.getCause() + ' -- Stacktrace: ' + ex.getStackTraceString();
            System.Debug(errorBody);
        }
    }
}

ExtractEmailAttachmentsTest.cls
@isTest
private class ExtractEmailAttachmentsTest {

    static testMethod void testExtractAttachments() {
        Test.startTest();
        Test.setMock(HttpCalloutMock.class, new ExtractEmailAttachmentsTest.ExtractEmailAttachmentsMock());
        ExtractEmailAttachments extractAttachments = new ExtractEmailAttachments();
        extractAttachments.extractAttachments();
        Test.stopTest();
    }

    public class ExtractEmailAttachmentsMock implements HttpCalloutMock {
        public HttpResponse respond(HTTPRequest req) {
            HttpResponse res = new HttpResponse();
            String testBody = '{"url":"https://pdf-temp-files.s3.us-west-2.amazonaws.com/ESRNUYPYS5RJIOJ8MT5DL8QLNOWMT3SG/email-extractor/sample.eml?X-Amz-Expires=3600\u0026X-Amz-Security-Token=FwoGZXIvYXdzEG0aDHKmifPGIYK8lSM0gyKCAUpezMRqX46NuUtPXmG01%2F36PDGpPjXHDmQF53pRl%2Bu9DjbpnNUp0iwxssel1HzjZNklih1l1C9BXEdEKn3Zea9N3mcADliD7g6Mxb8hr1rRz79U8GI6VlHIZZVJXbPRVyUyGrY%2BiwhfgarRg%2B5lln8jHMxPiHO66HUwGGkz04G4Iaoo0pOXoAYyKHe1LuYDaSM5gunLIezTYMm2wYrjgcOTQ%2F%2B07VwIFeBRaZFclBGBqk4%3D\u0026X-Amz-Algorithm=AWS4-HMAC-SHA256\u0026X-Amz-Credential=ASIA4NRRSZPHBI7XR2FE/20230306/us-west-2/s3/aws4_request\u0026X-Amz-Date=20230306T155547Z\u0026X-Amz-SignedHeaders=host\u0026X-Amz-Signature=e7d7005e0de47b9d065339883fd54f92f1eb18e14ae836c1c84eed56838fc29c","error":false,"status":200,"name":"sample.eml","credits":21,"duration":255,"remainingCredits":1165585}';
            res.setHeader('Content-Type', 'application/json');
            res.setBody(testBody);
            res.setStatusCode(200);
            return res;
        }
    }
}

PDF.co Web API: the Web API with a set of tools for documents manipulation, data conversion, data extraction, splitting and merging of documents. Includes image recognition, built-in OCR, barcode generation and barcode decoders to decode bar codes from scans, pictures and pdf.

Get your PDF.co API key here!

Download Source Code (.zip)

return to the previous page explore Email Send and Decode endpoint