简体   繁体   English

使用Django Rest Framework和Angular从API端点发送电子邮件

[英]Send an email from an API endpoint using Django Rest Framework and Angular

I'd like to send an email once a POST has been successfully completed. 一旦POST成功完成,我想发送电子邮件。 It seems to make sense that the email would be done with a signal. 似乎有意义的是,电子邮件将通过信号完成。 I can write my own, as documented here: https://docs.djangoproject.com/en/1.6/topics/signals/#defining-and-sending-signals 我可以自己编写,如下所示: https//docs.djangoproject.com/en/1.6/topics/signals/#defining-and-sending-signals

However, what I can't figure out is: 但是,我无法弄清楚的是:

  1. Does Django Rest Framework support signals (why wouldn't it, it's just Django) Django Rest Framework是否支持信号(为什么不是它,它只是Django)
  2. What do I listen to to send the custom signal? 我该怎么发送自定义信号? What is the sender? 发件人是什么?

Suggestions welcome. 建议欢迎。

Also, is anyone doing this with Angular? 还有,有人用Angular这样做吗? Is there a simple way to do this that I'm not seeing? 有没有一种简单的方法可以做到这一点,我没有看到?

Old question for an old version of Django, but if anyone comes across it... It's not necessary to use signals. 旧版Django的旧问题,但如果有人碰到它......没有必要使用信号。 Django now has the built-in function send_mail . Django现在有内置函数send_mail Here's the documentation: 这是文档:

https://docs.djangoproject.com/en/1.8/topics/email/ https://docs.djangoproject.com/en/1.8/topics/email/

So you would use that function to create and send an email. 因此,您将使用该功能来创建和发送电子邮件。 It can even accept an HTML template as the message argument and populate it w/ Django data, which is nice. 它甚至可以接受HTML模板作为消息参数,并用Django数据填充它,这很好。

So you'd define a function that included this method, for example: 所以你要定义一个包含这个方法的函数,例如:

def email_client(request):
    id = request.POST.get('id')
    client = Client.objects.get(id=id)
    msg_html = render_to_string('templates/email.html', {'client': client})
    template_email_text = ''
    return send_mail('Lelander work samples', template_email_text, 'test@emailsender.com', ['test@emailrecipient.com'], html_message=msg_html, fail_silently=False)

And include that function in your DRF API view in post : 并在post中的DRF API视图中包含该功能:

class ClientList(generics.ListCreateAPIView):

queryset = Client.objects.all()
serializer_class = ClientSerializer

def get(self, request, *args, **kwargs):
    return self.list(request, *args, **kwargs)

def post(self, request, *args, **kwargs):
    email_client(request)
    return self.create(request, *args, **kwargs)
  1. Yes, DRF supports signals. 是的,DRF支持信号。

I don't have a good answer for question 2, mainly because I think the best place to do this is in the post_save method of your class. 对于问题2我没有一个好的答案,主要是因为我认为最好的地方是你班级的post_save方法。 That way, you are sure your object was created, you have access to the object and to the request and you don't need to bother implementing signals (which would probably require another class just for email sending). 这样,您确定您的对象已创建,您可以访问该对象和请求,并且您不需要打扰实现信号(这可能需要另一个类仅用于电子邮件发送)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM