How to Send Email Using AWS SNS: Simple Guide
To send an email using
AWS SNS, create an SNS topic, subscribe an email endpoint to it, and then publish a message to that topic. The subscribed email will receive the message as a notification.Syntax
Here is the basic syntax to send an email using AWS SNS:
CreateTopic: Make a new SNS topic to group messages.Subscribe: Add an email address as a subscriber to the topic.Publish: Send a message to the topic, which SNS forwards to all subscribers.
python
import boto3 sns = boto3.client('sns') # Create a topic response = sns.create_topic(Name='MyEmailTopic') topic_arn = response['TopicArn'] # Subscribe an email address sns.subscribe(TopicArn=topic_arn, Protocol='email', Endpoint='example@example.com') # Publish a message sns.publish(TopicArn=topic_arn, Message='Hello from SNS email!')
Example
This example shows how to create an SNS topic, subscribe an email, and send a message. The email owner must confirm the subscription by clicking a link in the confirmation email before receiving messages.
python
import boto3 import time sns = boto3.client('sns') # Step 1: Create a topic response = sns.create_topic(Name='MyEmailTopic') topic_arn = response['TopicArn'] print(f'Topic ARN: {topic_arn}') # Step 2: Subscribe an email address email_address = 'your-email@example.com' subscribe_response = sns.subscribe( TopicArn=topic_arn, Protocol='email', Endpoint=email_address ) subscription_arn = subscribe_response['SubscriptionArn'] print(f'Subscription ARN (pending confirmation): {subscription_arn}') print('Please check your email and confirm the subscription.') # Wait for user to confirm subscription input('Press Enter after confirming subscription...') # Step 3: Publish a message publish_response = sns.publish( TopicArn=topic_arn, Message='This is a test email sent via AWS SNS!' ) print('Message published. Message ID:', publish_response['MessageId'])
Output
Topic ARN: arn:aws:sns:us-east-1:123456789012:MyEmailTopic
Subscription ARN (pending confirmation): pending confirmation
Please check your email and confirm the subscription.
Message published. Message ID: 1234abcd-56ef-78gh-90ij-klmnopqrstuv
Common Pitfalls
- Not confirming subscription: The email recipient must click the confirmation link sent by SNS, or they won't receive messages.
- Using wrong protocol: Use
emailoremail-jsonas protocol for email subscriptions. - Publishing before confirmation: Messages sent before subscription confirmation are not delivered.
- Missing permissions: Ensure your AWS credentials have SNS publish and subscribe permissions.
python
import boto3 sns = boto3.client('sns') # Wrong protocol example (will not send email) sns.subscribe(TopicArn='arn:aws:sns:region:account-id:topic', Protocol='sms', Endpoint='1234567890') # Correct protocol example sns.subscribe(TopicArn='arn:aws:sns:region:account-id:topic', Protocol='email', Endpoint='example@example.com')
Quick Reference
Remember these key steps to send email with SNS:
- Create an SNS topic.
- Subscribe email addresses with
emailprotocol. - Confirm subscription via email link.
- Publish messages to the topic.
- Check AWS IAM permissions for SNS actions.
Key Takeaways
Create an SNS topic and subscribe email addresses using the 'email' protocol.
Recipients must confirm their subscription via the email link before receiving messages.
Publish messages only after subscription confirmation to ensure delivery.
Ensure your AWS credentials have permissions for SNS publish and subscribe actions.
Use the AWS SDK (like boto3 for Python) to automate SNS email sending.