0

I need to send email along with attachments which reside in a different platform to external email IDs. below is the code snippet which I have done so far. Please suggest how to proceed further.

Rest Class

@RestResource(urlMapping='/sendPDFEmail/*')
Global class GETPDFContent{
 @HttpPost
global static void sendEmail(String EmailIdCSV, String Subject, String body) 
{

List<String> EmailIds = EmailIdCSV.split(',');

    PageReference ref = Page.PDF_DEMO;
    ref.getParameters().put('id', '00590000000ZiGV');
    //Id is User ID
    Blob b = ref.getContentAsPDF();

    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

    Messaging.EmailFileAttachment efa1 = new Messaging.EmailFileAttachment();
    efa1.setFileName('attachment_WORK.pdf');
    efa1.setBody(b);

    String addresses;
    email.setSubject( Subject +String.valueOf(DateTime.now()));
    email.setToAddresses( EmailIds  );
    email.setPlainTextBody(Body);
    email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa1});
    Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});

}

}

Controller:

 public with sharing class PDFController {

 public User u{get;set;}
 public Account Acc{get;set;}
 public Id accountId{get;set;}

 public PDFController(ApexPages.StandardController stdController){
 accountId = apexpages.currentpage().getparameters().get('id');
 //Acc =[Select id, Name,Phone FROM Account];
 u = [Select id,AccountId,Name, Account.Name,Account.phone from User where 
 id=:accountId limit 1];
 acc = [Select Id, Name,Phone From Account Where Id =: u.AccountId];

 }

 // Pass in the endpoint to be used using the string url
 public String getCalloutResponseContents(String url) {

// Instantiate a new http object
Http h = new Http();

 // Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
HttpRequest req = new HttpRequest();
req.setEndpoint(url);
req.setMethod('GET');

// Send the request, and return a response
HttpResponse res = h.send(req);
return res.getBody();
}

}

VF Page:

<apex:page showHeader="false" standardController="User" 
extensions="PDFController"  sidebar="false" renderAs="pdf" 
applyBodyTag="false" docType="html-5.0" >
<head>
    <style type="text/css">
        body {
            font-family: 'Arial Unicode MS';
        }

           @page {
            size: letter;
            margin: 20mm;      
            }
    </style>
  </head>


  <br/>
<br/>
 <apex:outputText style="font-size: 13px;background-color: yellow;" value="
 {0,date,MMMM dd, yyyy}">
  <apex:param value="{!TODAY()}" />


 </apex:outputText>
 <br/>
 <br/>
 <br/>
</apex:page>
1
  • What are you having trouble with ? Commented Aug 31, 2017 at 7:51

1 Answer 1

2
  1. Figure out a way to read the attachment data from the "different platform". Typically you will need to first authenticate and then make HTTP or HTTPS requests to get the data. Apex includes the building blocks you need for this but the details depend on exactly what the "different platform" offers.
  2. Use the Apex Messaging.SingleEmailMessageand Messaging.EmailFileAttachment classes to send the email addresses. There is an example of that sort of code in the Creating an Email Attachment documentation.
3
  • @SalesforceLearner The data being public makes it much easier. I've added a link to the Apex HTTP classes you can use to request the data.
    – Keith C
    Commented Aug 30, 2017 at 8:05
  • I've updated my question with the code which I have tried. Not sure how to proceed further to send the mails. Please suggest. Commented Aug 31, 2017 at 5:40
  • @SalesforceLearner The links in my answer provide examples: you'll hardly ever find exactly the code you need. Instead you need to experiment and fix compile errors and runtime errors (use the debug log) until you figure it out.
    – Keith C
    Commented Aug 31, 2017 at 7:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .