如何从AWS SNS主题向特定订阅者发送电子邮件

huangapple go评论133阅读模式
英文:

How to send email to specific subscriber from the topic in AWS SNS

问题

我已在AWS SNS中创建了一个主题。该主题包含许多订阅者,其协议为电子邮件,终端点为他们的电子邮件地址。我在Django中有一个API,将向当前用户发送电子邮件。为此,我将从主题中获取所有订阅者并匹配电子邮件地址。如果找到匹配项,我想向该特定订阅者发送电子邮件。如果找不到匹配项,我将创建一个新的订阅者,并向其发送电子邮件。我想知道如何做到这一点。在self.sns_client.publish()方法中应该写什么。

英文:

I have created one topic in AWS SNS. This topic contains many subscribers whose Protocol is Email and whose Endpoint is their email address. I have one API in Django which will send the email to the current user. So for this, I will fetch all the subscribers from the topic and match the email. If a match is found, I want to send an email to that particular subscriber. If a match is not found, I will create a new subscriber with that email address and send an email to it. I want to know how can we do that. What should be written in self.sns_client.publish() method.

答案1

得分: 1

你的方法听起来应该能够实现。

你可以通过调用 list_subscriptions_by_topic() 来开始。

你可以使用 subscribe() 来添加一个订阅者。

虽然Amazon SNS并不是专门设计用于"发送给单个订阅者",但你可以使用Amazon SNS消息过滤来根据消息属性来筛选接收者。然后,当你想要发布"单个接收者"的消息时,你可以将消息属性设置为一个特定的值,只匹配该订阅者。

每个现有和将来的订阅者都需要一个筛选策略,用于确定他们是否应该接收每个消息。你可以传递一个包含特定电子邮件地址或 'everyone' 的属性。然后,筛选策略可以检查该属性是否匹配特定的电子邮件地址或 'everyone'

此外,请注意Amazon SNS消息不会生成漂亮的电子邮件。在投入太多时间之前,请确保你对从SNS发送的电子邮件内容感到满意。

英文:

Your approach sounds like it would work.

You would start by calling list_subscriptions_by_topic().

You can add a subscriber with subscribe().

While Amazon SNS is not specifically designed to "send to a single subscriber" you could use Amazon SNS message filtering to filter the recipients against message attributes. Then, when you want to publish the 'single recipient' message you would set the message attributes to a particular value that would match only that subscriber.

Every existing and future subscriber would need a Filter Policy that determines whether they should receive each message. You could probably pass an attribute that either contains a specific email address or 'everyone'. Then, the filter policy could check that the attribute either matches a specific email address or 'everyone'.

Also, be aware that Amazon SNS messages do not make pretty emails. Make sure you are satisfied with the content of an email sent from SNS before investing too much time in this approach.

huangapple
  • 本文由 发表于 2023年6月6日 13:23:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76411633.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定