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
Zahir BasithZahir Basith 

Has anyone was able to send attachments to mailgun from salesforce

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
Yes, you can send attachments. Please refer this link.
https://premjpal.wordpress.com/2017/01/04/salesforce-and-mailgun-integration/
anivesh muppaanivesh muppa

Hi prem,

i am having the issue on sending mail with attachment.getting response as success but attachment is not added to the email.