I need to send a (potentially large) number of emails to (community) users via code using an Email Template, with a custom object as the whatId
.
- Because my
whatId
is a custom object, I am usingSingleEmailMessage
, notMassEmailMessage
. - Because I am using an Email template, I am using
setTargetObjectId()
, notsetToAddresses()
, so I must construct oneSingleEmailMessage
per recipient. - Because I am emailing to internal users, I am setting my target object id to user id values to avoid limits (see next section).
I have found the following relevant passages in the Limits Quick Reference (spring 15):
Total number of sendEmail methods allowed: 10 (p. 30)
If you use
SingleEmailMessage
to email your organization’s internal users, specifying the user’s ID insetTargetObjectId
means the email doesn’t count toward the daily limit. However, specifying internal users’ email addresses in setToAddresses means the email does count toward the limit. (p. 41)You can send an unlimited amount of email to your organization’s internal users, which includes portal users (p. 42)
The other email limits I have found are related to Email Services (inbound email), or to emails to external email address, neither of which apply.
Messaging.sendEmail
accepts a list<Messaging.Email>
of messages to send - so is there a limit on how many SingleEmailMessage
objects I can pass in that list? Is there a limit on number of emails per transaction? Some other limit I'm missing?
Let's say I need to send emails to 25,000 Community Users (extreme case, may be a possibility). Can I do this in a single call to Messaging.sendEmail()
? I'm guessing I'd hit a heap limit building my list. Any way to tell how many I can send at once? I am planning to use a Batchable
class, and was prepared to work in batches of 10, sending one email per sendEmail
invocation (first limit above) before I realized that sendEmail
accepts a list. What is a sensible batch size? If the list size is limited, is the per-transaction total limited? Batchable batches are limited to 2000 records, could I send one list of 2000? or 10 lists of 200?