2023-11-01 13:10:21 -04:00
// 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.
*
**/
2021-11-15 13:15:43 +00:00
# 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>
2023-11-01 13:10:21 -04:00
# 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 ) ;
2021-11-15 13:15:43 +00:00
Aws : : SES : : Model : : Destination destination ;
2023-11-01 13:10:21 -04:00
if ( ! ccAddresses . empty ( ) ) {
destination . WithCcAddresses ( ccAddresses ) ;
}
if ( ! recipients . empty ( ) ) {
destination . WithToAddresses ( recipients ) ;
2021-11-15 13:15:43 +00:00
}
2023-11-01 13:10:21 -04:00
Aws : : SES : : Model : : Body message_body ;
if ( ! htmlBody . empty ( ) ) {
message_body . SetHtml (
Aws : : SES : : Model : : Content ( ) . WithCharset ( " UTF-8 " ) . WithData ( htmlBody ) ) ;
}
2021-11-15 13:15:43 +00:00
2023-11-01 13:10:21 -04:00
if ( ! textBody . empty ( ) ) {
message_body . SetText (
Aws : : SES : : Model : : Content ( ) . WithCharset ( " UTF-8 " ) . WithData ( textBody ) ) ;
}
2021-11-15 13:15:43 +00:00
2023-11-01 13:10:21 -04:00
Aws : : SES : : Model : : Message message ;
2021-11-15 13:15:43 +00:00
message . SetBody ( message_body ) ;
2023-11-01 13:10:21 -04:00
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 ) ;
}
2021-11-15 13:15:43 +00:00
2023-11-01 13:10:21 -04:00
auto outcome = sesClient . SendEmail ( sendEmailRequest ) ;
2021-11-15 13:15:43 +00:00
2023-11-01 13:10:21 -04:00
if ( outcome . IsSuccess ( ) ) {
std : : cout < < " Successfully sent message with ID "
< < outcome . GetResult ( ) . GetMessageId ( )
< < " . " < < std : : endl ;
2021-11-15 13:15:43 +00:00
}
2023-11-01 13:10:21 -04:00
else {
std : : cerr < < " Error sending message. " < < outcome . GetError ( ) . GetMessage ( )
< < std : : endl ;
}
return outcome . IsSuccess ( ) ;
}
// snippet-end:[cpp.example_code.ses.SendEmail]
2021-11-15 13:15:43 +00:00
2023-11-01 13:10:21 -04:00
/*
*
* 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 ) ;
2021-11-15 13:15:43 +00:00
{
2023-11-01 13:10:21 -04:00
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 ) ;
2021-11-15 13:15:43 +00:00
}
2023-11-01 13:10:21 -04:00
Aws : : ShutdownAPI ( options ) ;
return 0 ;
2021-11-15 13:15:43 +00:00
}
2023-11-01 13:10:21 -04:00
# endif // TESTING_BUILD