Basically I need to build a Bulk email functionality. Need to send a single message to multiple emails through AWS ses. But completely clueless from where to begin and how to achieve this.
Is there anyone who has done it or know a little about it. do help me out.
Any help would be appreciated.
Progress so far:-
I've found this documentation. I thik this is something i need to follow.
https://docs.aws.amazon.com/ses/latest/APIReference-V2/API_SendBulkEmail.html
And most probably I need to write batch apex so how can I handle it through batch that also I would like to know from the community. Thanks.
#AWS #Apex #Experience Cloud #Salesforce Developer #Lightning Aura Components
Hi @Kushagra Rajput,
Your direction is right. Here is a simple way to build it.
1. Set up AWS SES first
- Verify your sender email or domain in SES.
- If your SES account is in sandbox mode, you can only send to verified emails, so request production access.
- Create an email template in SES. SendBulkEmail needs a template, even for a simple message.
2. Authentication from Salesforce
Use a
Named Credential with an External Credential of type AWS Signature Version 4. It signs the request for you, so you do not need to write the signing code in Apex.3. The API call
Send a POST request to /v2/email/outbound-bulk-emails (SES v2). One call accepts
up to 50 destinations, so split your email list into groups of 50.4. Use Batch Apex
- Implement Database.Batchable<SObject> and Database.AllowsCallouts. Without AllowsCallouts, the callout fails.
- Run it with a scope of 50, so each execute makes one SES call.
apex
public class BulkEmailBatch implements Database.Batchable<SObject>, Database.AllowsCallouts {
public Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator('SELECT Email FROM Contact WHERE Email != null');
}
public void execute(Database.BatchableContext bc, List<Contact> scope) {
List<Object> entries = new List<Object>();
for (Contact c : scope) {
entries.add(new Map<String, Object>{
'Destination' => new Map<String, Object>{ 'ToAddresses' => new List<String>{ c.Email } }
});
}
Map<String, Object> body = new Map<String, Object>{
'FromEmailAddress' => 'sender@yourdomain.com',
'DefaultContent' => new Map<String, Object>{
'Template' => new Map<String, Object>{ 'TemplateName' => 'YourTemplate', 'TemplateData' => '{}' }
},
'BulkEmailEntries' => entries
};
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:AWS_SES/v2/email/outbound-bulk-emails');
req.setMethod('POST');
req.setHeader('Content-Type', 'application/json');
req.setBody(JSON.serialize(body));
HttpResponse res = new Http().send(req);
// check res.getStatusCode() and log failures
}
public void finish(Database.BatchableContext bc) {}
}
Run it with Database.executeBatch(new BulkEmailBatch(), 50);
5. Things to remember
- Check the response for each entry (the API returns a status per email), and log failures.
- Respect your SES sending rate limit.
- Handle bounces and unsubscribes (SES configuration sets with SNS can help).
Hope this helps. Share where you get stuck and I can help further.