// 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 #include #include #include #include #include #include #include #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 &recipients, const Aws::String &subject, const Aws::String &htmlBody, const Aws::String &textBody, const Aws::String &senderEmailAddress, const Aws::Vector &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 * * ' * */ #ifndef TESTING_BUILD int main(int argc, char **argv) { if (argc < 7) { std::cout << "Usage: run_send_email " " " ""; 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 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