function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Brad HamerBrad Hamer 

Has anyone successfully integrated Mailgun into Salesforce? Need it to track messages...

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!
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Brad Hamer,
Mailgun integration with Salesforce.May I suggest you please refer the below link for reference. Thanks 
Rahul Kumar
Brad HamerBrad Hamer
Thank you Rahul.

We've tried everything in the SalesforceSMTP article, and email aren't being sent when the relay is activated. That, and no logging in Mailgun about emails attempting to be sent.

The other article is about writing our own API and we are not going to be doing that.

Are there other solutions? This seems like it should be straight forward and we need to get it completed. Anyone have any other thoughts or ideas?

Thanks.
Zahir BasithZahir Basith
Hi Rahul,

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;
    }
Prem_PalPrem_Pal
Try this
https://premjpal.wordpress.com/2017/01/04/salesforce-and-mailgun-integration/
Jack LasorJack Lasor