Skip to main content
Post Reopened by Salesforce Learner, Martin Lezer, martin, Samuel De Rycke
added 21 characters in body
Source Link

I need to send email along with attachments which reside in a different platform to external email IDs. Whatbelow is the best possible approach and the stepscode snippet which I have done so far. Please suggest how to achieve the sameproceed 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>

I need to send email along with attachments which reside in a different platform to external email IDs. What is the best possible approach and the steps to achieve the same.

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>
Post Closed as "Not suitable for this site" by Martin Lezer, Markus Dang - formerly Slabina, martin, Dave Humm, Adam Spriggs
Source Link

Send email with attachment

I need to send email along with attachments which reside in a different platform to external email IDs. What is the best possible approach and the steps to achieve the same.