Repository navigation
Expand file tree
/
Copy pathsend_email.cpp
More file actions
144 lines (126 loc) · 5.11 KB
/
Copy pathsend_email.cpp
File metadata and controls
144 lines (126 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
/**
* Before running this C++ code example, set up your development environment, including your credentials.
*
* For more information, see the following documentation topic:
*
* https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started.html
*
* For information on the structure of the code examples and how to build and run the examples, see
* https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started-code-examples.html.
*
**/
#include <aws/core/Aws.h>
#include <aws/email/SESClient.h>
#include <aws/email/model/SendEmailRequest.h>
#include <aws/email/model/Destination.h>
#include <aws/email/model/Message.h>
#include <aws/email/model/Body.h>
#include <aws/email/model/Content.h>
#include <iostream>
#include "ses_samples.h"
// snippet-start:[cpp.example_code.ses.SendEmail]
//! Send an email to a list of recipients.
/*!
\param recipients; Vector of recipient email addresses.
\param subject: Email subject.
\param htmlBody: Email body as HTML. At least one body data is required.
\param textBody: Email body as plain text. At least one body data is required.
\param senderEmailAddress: Email address of sender. Ignored if empty string.
\param ccAddresses: Vector of cc addresses. Ignored if empty.
\param replyToAddress: Reply to email address. Ignored if empty string.
\param clientConfiguration: AWS client configuration.
\return bool: Function succeeded.
*/
bool AwsDoc::SES::sendEmail(const Aws::Vector<Aws::String> &recipients,
const Aws::String &subject,
const Aws::String &htmlBody,
const Aws::String &textBody,
const Aws::String &senderEmailAddress,
const Aws::Vector<Aws::String> &ccAddresses,
const Aws::String &replyToAddress,
const Aws::Client::ClientConfiguration &clientConfiguration) {
Aws::SES::SESClient sesClient(clientConfiguration);
Aws::SES::Model::Destination destination;
if (!ccAddresses.empty()) {
destination.WithCcAddresses(ccAddresses);
}
if (!recipients.empty()) {
destination.WithToAddresses(recipients);
}
Aws::SES::Model::Body message_body;
if (!htmlBody.empty()) {
message_body.SetHtml(
Aws::SES::Model::Content().WithCharset("UTF-8").WithData(htmlBody));
}
if (!textBody.empty()) {
message_body.SetText(
Aws::SES::Model::Content().WithCharset("UTF-8").WithData(textBody));
}
Aws::SES::Model::Message message;
message.SetBody(message_body);
message.SetSubject(
Aws::SES::Model::Content().WithCharset("UTF-8").WithData(subject));
Aws::SES::Model::SendEmailRequest sendEmailRequest;
sendEmailRequest.SetDestination(destination);
sendEmailRequest.SetMessage(message);
if (!senderEmailAddress.empty()) {
sendEmailRequest.SetSource(senderEmailAddress);
}
if (!replyToAddress.empty()) {
sendEmailRequest.AddReplyToAddresses(replyToAddress);
}
auto outcome = sesClient.SendEmail(sendEmailRequest);
if (outcome.IsSuccess()) {
std::cout << "Successfully sent message with ID "
<< outcome.GetResult().GetMessageId()
<< "." << std::endl;
}
else {
std::cerr << "Error sending message. " << outcome.GetError().GetMessage()
<< std::endl;
}
return outcome.IsSuccess();
}
// snippet-end:[cpp.example_code.ses.SendEmail]
/*
*
* main function
*
* Usage: 'Usage: run_send_email <message_body_html_data> <message_body_text_data>
* <message_subject_data> <sender_email_address> <cc_address> <reply_to_address>
* <to_addresses>'
*
*/
#ifndef TESTING_BUILD
int main(int argc, char **argv) {
if (argc < 7) {
std::cout << "Usage: run_send_email <message_body_html_data> <message_body_text_data>"
"<message_subject_data> <sender_email_address> <cc_address> <reply_to_address>"
"<to_addresses>";
return 1;
}
Aws::SDKOptions options;
Aws::InitAPI(options);
{
Aws::String messageBodyHtmlData(argv[1]);
Aws::String messageBodyTextData(argv[2]);
Aws::String messageSubjectData(argv[3]);
Aws::String senderEmailAddress(argv[4]);
Aws::String ccAddress(argv[5]);
Aws::String replyToAddress(argv[6]);
Aws::Vector<Aws::String> recipients;
for (int i = 7; i < argc; i++) {
recipients.emplace_back(argv[i]);
}
Aws::Client::ClientConfiguration clientConfiguration;
// Optional: Set to the AWS Region (overrides config file).
// clientConfig.region = "us-east-1";
AwsDoc::SES::sendEmail(recipients, messageSubjectData, messageBodyHtmlData, messageBodyTextData,
senderEmailAddress, {ccAddress}, replyToAddress, clientConfiguration);
}
Aws::ShutdownAPI(options);
return 0;
}
#endif // TESTING_BUILD