• Zahir Basith
  • NEWBIE
  • 20 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 4
    Replies
I was able to develope an API which can send emails from saleforce through mailgun. emails are successfully being send. but attachments are not working.. we despirately in need of ability to send attachements. Please find my code .. this perticular code is not showing any error its working but attachments are not getting send only the email is send. 

@RemoteAction  @future (callout=true)
    public static void sendEmailResponse(String notId, String response, Boolean hasAttachment, string csvFileId, String responseSubject, String toEmail, String toCCEmail){
        System.debug('********** inside send email******');
        if(toEmail == '' || response == ''){ 
            return;
        }
        String status = '';
        
        if(toEmail != '' && response != ''){ 
            string sURL =   'https://api.mailgun.net/v3/sandboxedaf562949124727b836108ded5d10ed.mailgun.org/messages'; 
            string sBody = '';
            String strSubject = responseSubject;
            string sReturn = '';
            String attachmentBody = '';
            String bodyPayload='';
            String separationString = 'A_RANDOM_STRING';
            HttpRequest req = new HttpRequest();
            HttpResponse res = new HttpResponse();
            Http http = new Http();
            
            String authUserName = 'postmaster@sandboxedafxxxxxxxxxed.mailgun.org'; 
            String authUserPassword = '1fxxxxxxxxxxxxxxxxxxxxxx';
            String filePath; 
            if(hasAttachment){
                Attachment csvFile = [select id, body, Name from Attachment where id=:csvFileId];
                String header = '--' + separationString + 
                 + 'Content-Disposition: form-data; name="file"; filename="'+csvFile.name
                 +'"\nContent-Type: application/octet-stream\n\n';
                 attachmentBody =  EncodingUtil.base64Encode(csvFile.Body);
                 String footer = '--' + separationString + '--';
                  bodyPayload= header+attachmentBody+footer;
                 
                // filePath = 'https://c.cs91.content.force.com/servlet/servlet.FileDownload?file='+csvFile.id;
                filePath = 'http://che.org.il/wp-content/uploads/2016/12/pdf-sample.pdf'; // a sample public file for testing purpose

            }
            
            String fromemail = 'NoReply<zahir.basith@virtual1.com>'; 
            String toSetCCEmail ='';
            if(responseSubject != '' || responseSubject != Null){
                responseSubject = EncodingUtil.urlEncode( responseSubject, 'UTF-8');
            }
            String subject = responseSubject;
            String message  = response.unescapeHtml4();
            message = EncodingUtil.urlEncode( message, 'UTF-8');
            
            Blob headerValue = Blob.valueOf('api:key-xxxxxxxxxxxxxxxxxxxxxxxx');
            String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);
            req.setHeader('Authorization', authorizationHeader);
            req.setHeader('Content-Type','multipart/form-data; boundary=' + separationString);
            req.setHeader('Host', 'sandboxedaf562949124727b836108ded5d10ed.mailgun.org');
            
            if(toCCEmail != ''){
                toSetCCEmail= '&cc='+toCCEmail;
            } 
            string mailBody;
            if(hasAttachment){
                mailBody = sURL+'?from='+fromemail+'&to='+toEmail+'&attachment='+filePath+'&subject='+subject+'&html='+message;
                req.setBody('{ "from": "'+fromemail+'",  "to": "'+toEmail+'", "cc":"'+toCCEmail+'", "attachments": "'+filePath+'", "subject": "'+subject+'",  "html": "'+message+'"}');
            }else{
                mailBody = sURL+'?from='+fromemail+'&to='+toEmail+'&subject='+subject+'&html='+message;
                req.setBody('{ "from": "'+fromemail+'",  "to": "'+toEmail+'", "cc":"'+toCCEmail+'", "subject": "'+subject+'",  "html": "'+message+'"}'); 
            }
                
            req.setMethod('POST');   
            req.setEndPoint(mailBody);  
            
            integer iLength = mailBody.length();
            req.setHeader('Content-Length', string.valueOf(iLength));
            
            if( !System.isBatch() && !test.isRunningTest() ) { 
                res = http.send(req);  
            } else{  
                res.setStatusCode(200);
                res.setStatusCode(414);
            }  
            
            String messageID = ''; 
            if( res.getStatusCode() == 200 ){   
                
               System.debug('Email sent');
            }  
            
            status = ''+res.getStatusCode()+''; 
        }
        //return status;
    }
Hello Dev friends, 

I have a custom object called notification details which will have all the circuits effected for a partner. The point to note is that, We will have many of these circuits belonging to the same partner . I want to prepare a notification email record per partner which will have all circuits belonging to a partner.
What is the efficient way of coding this in apex, I would like to get an expert opinion before I start coding my own way J

Notification detail object
CircuitsAccountPartner nameEmail address
V1C1111Xyz1Partner1Partner1@gmail.com
V1C1112Xyz1Partner1Partner1@gmail.com
V1C1113Xyz1Partner1Partner1@gmail.com
V1C1114Company 2Partner2Partner2@gmail.com
V1C1115Company 2Partner2Partner2@gmail.com
 
Notification email object: This is want I want from the above table
Circuits (text field)Account (Lookup)Partner name (Lookup)Email address (Text field)
V1C1111, V1C1112, V1C1113Xyz1Partner1Partner1@gmail.com
V1C1114, V1C1115Company 2Partner2Partner2@gmail.com

Thank you 
Hi Dev friends, 

I have a wrapper class with a combination of a custom object<Access__c> and couple of other variables. Now I need have a requirement to setup a similar list (wrapper) with the same set of variables but a different custom object<Asset__c>. end of the day I need a conbined list of all the Access__c records with the rest of parementers and all Asset__c records with the similar set of parements. 
what is the efficient way of doing this? Can I create a simiar wrapper class and combine both to form a third wrapper class? 

Sample of my existing wrapper class : 
    public class AccessWrapper{
        public Access__c accessRecord {get;set;}
        public List<String> contactEmailList {get;set;}

        public String partnerName {get; set;}
        public String endCustomer {get; Set;}
        public Integer numberOfCircuits {get; set;}
        public String plannedWorksEmail {get; set;}

        public AccessWrapper(String partner, String ec, Integer noOfCircuits, String emailAddress){
            this.partnerName = partner;
            this.endCustomer = ec;
            this.numberOfCircuits = noOfCircuits;
            this.plannedWorksEmail = emailAddress;
        }
    }
Next requirement is to have an asset wrapper  and have a thrid wrapper which is combination of AccessWrapper + AssetWrapper. 
    public class AssetWrapper{
        public Asset__c assetRecord {get;set;}
        public List<String> contactEmailList {get;set;}

        public String partnerName {get; set;}
        public String endCustomer {get; Set;}
        public Integer numberOfCircuits {get; set;}
        public String plannedWorksEmail {get; set;}

        public AccessWrapper(String partner, String ec, Integer noOfCircuits, String emailAddress){
            this.partnerName = partner;
            this.endCustomer = ec;
            this.numberOfCircuits = noOfCircuits;
            this.plannedWorksEmail = emailAddress;
        }
    }


Please advice ..
Hello Dev friends, 

I have a custom object called notification details which will have all the circuits effected for a partner. The point to note is that, We will have many of these circuits belonging to the same partner . I want to prepare a notification email record per partner which will have all circuits belonging to a partner.
What is the efficient way of coding this in apex, I would like to get an expert opinion before I start coding my own way J

Notification detail object
CircuitsAccountPartner nameEmail address
V1C1111Xyz1Partner1Partner1@gmail.com
V1C1112Xyz1Partner1Partner1@gmail.com
V1C1113Xyz1Partner1Partner1@gmail.com
V1C1114Company 2Partner2Partner2@gmail.com
V1C1115Company 2Partner2Partner2@gmail.com
 
Notification email object: This is want I want from the above table
Circuits (text field)Account (Lookup)Partner name (Lookup)Email address (Text field)
V1C1111, V1C1112, V1C1113Xyz1Partner1Partner1@gmail.com
V1C1114, V1C1115Company 2Partner2Partner2@gmail.com

Thank you 
Hi Dev friends, 

I have a wrapper class with a combination of a custom object<Access__c> and couple of other variables. Now I need have a requirement to setup a similar list (wrapper) with the same set of variables but a different custom object<Asset__c>. end of the day I need a conbined list of all the Access__c records with the rest of parementers and all Asset__c records with the similar set of parements. 
what is the efficient way of doing this? Can I create a simiar wrapper class and combine both to form a third wrapper class? 

Sample of my existing wrapper class : 
    public class AccessWrapper{
        public Access__c accessRecord {get;set;}
        public List<String> contactEmailList {get;set;}

        public String partnerName {get; set;}
        public String endCustomer {get; Set;}
        public Integer numberOfCircuits {get; set;}
        public String plannedWorksEmail {get; set;}

        public AccessWrapper(String partner, String ec, Integer noOfCircuits, String emailAddress){
            this.partnerName = partner;
            this.endCustomer = ec;
            this.numberOfCircuits = noOfCircuits;
            this.plannedWorksEmail = emailAddress;
        }
    }
Next requirement is to have an asset wrapper  and have a thrid wrapper which is combination of AccessWrapper + AssetWrapper. 
    public class AssetWrapper{
        public Asset__c assetRecord {get;set;}
        public List<String> contactEmailList {get;set;}

        public String partnerName {get; set;}
        public String endCustomer {get; Set;}
        public Integer numberOfCircuits {get; set;}
        public String plannedWorksEmail {get; set;}

        public AccessWrapper(String partner, String ec, Integer noOfCircuits, String emailAddress){
            this.partnerName = partner;
            this.endCustomer = ec;
            this.numberOfCircuits = noOfCircuits;
            this.plannedWorksEmail = emailAddress;
        }
    }


Please advice ..
Being required to use Mailgun to track messages being sent through Salesforce. I have looked into and tried to integrate with SalesForceSMTP, but everytime I turn the relay on (active), no emails are delivered (test or "real"). Has anyone successfully done so? Can you share what you did? Thank you!