sendEmail method

Future<SendEmailResponse> sendEmail(
  1. SendEmailRequest request
)

Sends an email through SES.

Emails without attachments use the standard SendEmail flow. Emails with attachments use SendRawEmail.

Returns a SendEmailResponse when SES accepts the message.

Example:

final response = await client.sendEmail(
  SendEmailRequest(
    from: EmailAddress('noreply@example.com', name: 'Archery'),
    to: [EmailAddress('jane@example.com')],
    subject: 'Welcome',
    htmlBody: '<h1>Hello Jane</h1>',
  ),
);

print(response.messageId);

Implementation

Future<SendEmailResponse> sendEmail(SendEmailRequest request) async {
  try {
    final body = request.attachments != null && request.attachments!.isNotEmpty ? _buildRawEmailRequest(request) : _buildEmailRequest(request);
    final response = await _makeRequest(method: 'POST', path: '/', body: body);

    if (response.statusCode == 200) {
      // Parse XML response
      final xml = response.body;
      final messageId = _extractFromXml(xml, 'MessageId');
      final requestId = _extractFromXml(xml, 'RequestId');

      if (messageId.isEmpty || requestId.isEmpty) {
        throw Exception('Invalid response from SES: $xml');
      }

      return SendEmailResponse(messageId: messageId, requestId: requestId, timestamp: DateTime.now());
    } else {
      final error = _extractErrorFromXml(response.body);
      throw Exception('Failed to send email (${response.statusCode}): $error');
    }
  } catch (e) {
    // todo -archeryLogger
    rethrow;
  }
}