2021-11-15 13:15:43 +00:00
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
# include <aws/core/Aws.h>
# include <aws/sns/SNSClient.h>
# include <aws/sns/model/SubscribeRequest.h>
# include <iostream>
2023-03-31 10:46:57 -04:00
# include "sns_samples.h"
2021-11-15 13:15:43 +00:00
// snippet-start:[sns.cpp.subscribe_email.code]
2023-03-31 10:46:57 -04:00
//! Subscribe to an Amazon Simple Notification Service (Amazon SNS) topic with delivery to an email address.
/*!
\param topicARN: An SNS topic Amazon Resource Name (ARN).
\param emailAddress: An email address.
\param clientConfiguration: AWS client configuration.
\return bool: Function succeeded.
2021-11-15 13:15:43 +00:00
*/
2023-03-31 10:46:57 -04:00
bool AwsDoc : : SNS : : subscribeEmail ( const Aws : : String & topicARN ,
const Aws : : String & emailAddress ,
const Aws : : Client : : ClientConfiguration & clientConfiguration ) {
Aws : : SNS : : SNSClient snsClient ( clientConfiguration ) ;
2021-11-15 13:15:43 +00:00
2023-03-31 10:46:57 -04:00
Aws : : SNS : : Model : : SubscribeRequest request ;
request . SetTopicArn ( topicARN ) ;
request . SetProtocol ( " email " ) ;
request . SetEndpoint ( emailAddress ) ;
2021-11-15 13:15:43 +00:00
2023-03-31 10:46:57 -04:00
const Aws : : SNS : : Model : : SubscribeOutcome outcome = snsClient . Subscribe ( request ) ;
2021-11-15 13:15:43 +00:00
2023-03-31 10:46:57 -04:00
if ( outcome . IsSuccess ( ) ) {
std : : cout < < " Subscribed successfully. " < < std : : endl ;
std : : cout < < " Subscription ARN ' " < < outcome . GetResult ( ) . GetSubscriptionArn ( )
< < " '. " < < std : : endl ;
}
else {
std : : cerr < < " Error while subscribing " < < outcome . GetError ( ) . GetMessage ( )
< < std : : endl ;
}
2021-11-15 13:15:43 +00:00
2023-03-31 10:46:57 -04:00
return outcome . IsSuccess ( ) ;
}
// snippet-end:[sns.cpp.subscribe_email.code]
2021-11-15 13:15:43 +00:00
2023-03-31 10:46:57 -04:00
/*
*
* main function
*
* Usage: 'run_subscribe_email <topic_arn> <email_address>'
*
* Prerequisites: An existing SNS topic and its ARN.
*
*/
# ifndef TESTING_BUILD
int main ( int argc , char * * argv ) {
if ( argc ! = 3 ) {
std : : cout < < " Usage: run_subscribe_email <topic_arn> <email_address> "
< < std : : endl ;
return 1 ;
2021-11-15 13:15:43 +00:00
}
2023-03-31 10:46:57 -04:00
Aws : : SDKOptions options ;
Aws : : InitAPI ( options ) ;
2021-11-15 13:15:43 +00:00
{
2023-03-31 10:46:57 -04:00
Aws : : String topicArn = argv [ 1 ] ;
Aws : : String emailAddress = argv [ 2 ] ;
Aws : : Client : : ClientConfiguration clientConfig ;
// Optional: Set to the AWS Region (overrides config file).
// clientConfig.region = "us-east-1";
AwsDoc : : SNS : : subscribeEmail ( topicArn , emailAddress , clientConfig ) ;
2021-11-15 13:15:43 +00:00
}
2023-03-31 10:46:57 -04:00
Aws : : ShutdownAPI ( options ) ;
return 0 ;
2021-11-15 13:15:43 +00:00
}
2023-03-31 10:46:57 -04:00
# endif // TESTING_BUILD