0
public with sharing class AWS_SES {

  public String endpoint = 'https://email.us-east-1.amazonaws.com';
  public String method = 'POST';
  public string key;
  public string secret;

  public AWS_SES()
  {
    //Get key and secret from custom setting AWS__c

    AWS__c objKeyDetails = [ Select SecretKey__c, AccessKey__c, Name From AWS__c ] ;
    this.key = objKeyDetails.AccessKey__c;
    this.secret = objKeyDetails.SecretKey__c;
  }

  public void sendEmail(List<String> toAddresses, String fromAddress, String subject, String body)
  {
    //Creating the instance of AWS_EmailFormat class
    AWS_EmailFormat emailUtil = new AWS_EmailFormat();

    //body of that method AWS_EmailDateFormat is defined in AWS_EmailFormat class.
    String AWSEmailDateFormat = emailUtil.AWS_EmailDateFormat(Datetime.now());

    HttpRequest httpReq = new HttpRequest(); 
    httpReq.setEndpoint(endpoint);
    httpReq.setMethod(method);

    //body of that headerForAmazonAuthorization is defined in AWS_EmailFormat class.
    httpReq.setHeader('X-Amzn-Authorization', emailUtil.headerForAmazonAuthorization(this.key, emailUtil.signature(AWSEmailDateFormat, this.secret))); 
    httpReq.setHeader('Content-Type', 'application/x-www-form-urlencoded');
    httpReq.setHeader('Date', AWSEmailDateFormat);

    emailUtil.emailSend(toAddresses, fromAddress, subject, body);

    httpReq.setBody(emailUtil.encodeEmail());

    System.debug('*******BODY*******'+httpReq.getBody()); 

    Http http = new Http();
    HttpResponse response = http.send(httpReq);   

    System.debug('RESPONSE FROM AWS'+response.getBody());
    system.debug('OKAY THATS FINE THANKS!!!');  

By using that code I am able to send email to AWS from salesforce and also I have got the response from AWS. Now I want to add attachment with the email and want to send request to AWS.

How can it be possible. Please give me the suggestion

6
  • 3
    Welcome to SFSE. You really need to provide some more information. What you have tried so far? As it stands, it seems like you are asking people to write your code for you, which is likely to be ignored. Please add some code of what you have tried and explain where you are stuck and people will try to help you out. Commented Oct 19, 2013 at 21:13
  • By using that code I am able to send email to AWS from salesforce and also I have got the response from AWS....Now I want to add attachment with the email and want to send request to aws..How can it be possible..Please give me the suggetion
    – John Smith
    Commented Oct 20, 2013 at 7:24
  • Hi, I have posted the code above which I am using and I want to send attachment now to aws..Kindly help me please..
    – John Smith
    Commented Oct 20, 2013 at 7:36
  • @JohnSmith welcome to SFSE. As Dunc44 pointed out, this forum is great for helping you deal with specific issues and errors, but you'll need to do the legwork. From your post it sounds like you're asking the community to write the code for you. Instead, give it a try and if you get stuck create another question for us to help with. Commented Oct 20, 2013 at 22:34
  • Thank you..Actualy that was my problem and I am not getting any idea about how to send attachment..
    – John Smith
    Commented Oct 21, 2013 at 5:00

1 Answer 1

4

Looking at the code you provided you'll need to extend your AWS_EmailFormat class to support attachments. From the Amazon SES FAQ.

You can also send email with attachments programmatically. To include an attachment, construct a multipart email message. In this message, include a MIME part that contains an appropriate Content-Type header, along with the MIME-encoded content. Next, use the Content-Disposition header to specify whether the content is to be displayed inline or treated as an attachment.

Unfortunately it seems you'll need to build the MIME part manually yourself which could get pretty dicey with APEX and you'll be limited in the attachment size with the current Salesforce max callout size limit of 3MB. To give you an idea, a PHP developer decided to quit when trying to implement attachments with Amazon SES

After many tries, I got to the conclusion that sending emails to Amazon SES directly from the code is too much pain.

1
  • I haven't connected Salesforce to SES before, but in situations like this it can help to call a service like Lambda instead where you can load SDKs for AWS and work a little more closely with SES instead of trying to fit it all into a callout in Apex which, unfortunately, is just not robust enough for all types of HTTP callouts. @JohnSmith
    – nbrown
    Commented Sep 28, 2020 at 14:31

You must log in to answer this question.

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