Link Search Menu Expand Document

Send Email - Salesforce

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

SendEmailWithAttachment.cls
public class SendEmailWithAttachment {

    String API_KEY = 'YOUR_PDF_CO_API_KEY_HERE';
    String endPointUrl = 'https://api.pdf.co/v1/email/send';

    public void sendEmailWithAttachment() {
        try {
            String jsonBody = '{ "url": "https://pdfco-test-files.s3.us-west-2.amazonaws.com/pdf-info/sample.pdf", "from": "John Doe <john@example.com>", "to": "Partner <partner@example.com>", "subject": "Check attached sample pdf", "bodytext": "Please check the attached pdf", "bodyHtml": "Please check the attached pdf", "smtpserver": "smtp.gmail.com", "smtpport": "587", "smtpusername": "my@gmail.com", "smtppassword": "app specific password created as https://support.google.com/accounts/answer/185833", "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);

            if(response.getStatusCode() == 200) {
                System.debug('Email sent successfully: ' + 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);
        }
    }
}

SendEmailWithAttachmentTest.cls
@isTest
private class SendEmailWithAttachmentTest {

    static testMethod void testSendEmailWithAttachment() {
        Test.startTest();
        Test.setMock(HttpCalloutMock.class, new SendEmailWithAttachmentTest.SendEmailWithAttachmentMock());
        SendEmailWithAttachment sendEmail = new SendEmailWithAttachment();
        sendEmail.sendEmailWithAttachment();
        Test.stopTest();
    }

    public class SendEmailWithAttachmentMock implements HttpCalloutMock {
        public HttpResponse respond(HTTPRequest req) {
            HttpResponse res = new HttpResponse();
            String testBody = '{"error": false, "status": 200, "message": "Email sent successfully"}';
            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