Send bulk emails through AWS SES.
This script is an extension (and eventually a replacement) to sendSESEmail.py,
and a more generalized version of it's functionalities. Eventually, we want
to validate the funtionalities of this script in use cases where
sendSESEmail.py is being used, and make the email sending implementation in
this script default.
This attempts to simplify the grouping of email recipients
(to, cc and bcc) by respecting the limit of number of recipients that can be
used in a single aws ses send_email api call.
Uses the AWS SES call send-mail.
Has the following important parameters:
Body contents: (at least one of the following has to be specified.)
- text -- the plain text version of the email.
- html -- the HTML-formatted version of the email.
- subject -- the subject of the email. This is required.
- efrom -- the from email. This is required.
The efrom requires verification by AWS SES (either by email address or by domain.)
- replyto -- the reply to email. (submitform.pl & submitsecure.pl)
IMPORTANT: Please inspect the batch_recipients function to see how email
groups are constructed.
Email destinations: (at least one of the following has to be specified.)
Destinations are space-separated like a@comp.dom b@comp.dom ...
- to -- a list of to recipients. (Who the email is actually addressed to.)
- bcc -- a list of bcc recipients. (List isn't visible in email.)
- cc -- the cc for the email. (Visible list in email.)
Amazon requires the from email to be verified or the domain verified.
| def sendSESEmailBulk.send_email_batch |
( |
|
email_group, |
|
|
|
context, |
|
|
|
args |
|
) |
| |
This sends emails to a batch of to, cc and bcc recipients.
Args:
email_group: group of to, cc and bcc email recipients; total
should be less than or equal to AWS_SES_EMAIL_LIMIT
Returns:
batch_return: a dictionary of status, error and response force
each email group
Definition at line 213 of file sendSESEmailBulk.py.
213 def send_email_batch(email_group, context, args):
215 This sends emails to a batch of to, cc and bcc recipients. 218 email_group: group of to, cc and bcc email recipients; total 219 should be less than or equal to AWS_SES_EMAIL_LIMIT 221 batch_return: a dictionary of status, error and response force 238 client = boto3.client(
'ses', region_name=AWS_REGION)
243 if email_group[0]: destination[
"ToAddresses"] = email_group[0]
244 if email_group[1]: destination[
"CcAddresses"] = email_group[1]
245 if email_group[2]: destination[
"BccAddresses"] = email_group[2]
250 message[
"Subject"] = {}
251 message[
"Subject"][
"Data"] = args.subject
252 message[
"Subject"][
"Charset"] = charset
254 if (args.plaintext !=
None):
256 message[
"Body"][
"Text"] = {}
257 message[
"Body"][
"Text"] [
"Data"] = args.plaintext
258 message[
"Body"][
"Text"] [
"Charset"] = charset
260 if (args.htmltext !=
None):
261 if "Body" not in message:
262 message [
"Body"] = {}
264 message[
"Body"][
"Html"] = {}
265 message[
"Body"][
"Html"][
"Data"] = args.htmltext
266 message[
"Body"][
"Html"][
"Charset"] = charset
268 if (args.replyto !=
None):
270 response = client.send_email (
272 Destination = destination,
274 ReplyToAddresses = args.replyto,
275 ConfigurationSetName=
'ses-detail-logging',
279 response = client.send_email (
281 Destination = destination,
283 ConfigurationSetName=
'ses-detail-logging',
286 except ClientError
as e:
288 batch_return[
"status"] = 902
289 batch_return[
"error"] = str(e)
291 except Exception
as e:
293 batch_return[
'status'] = 903
294 batch_return[
'error'] = e
297 batch_return[
'response'] = response
302 LOGGER.info(batch_return)
304 LOGGER.error(batch_return)