Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
调用api端点Django后如何发送邮件?_Django_Django Models_Django Rest Framework - Fatal编程技术网

调用api端点Django后如何发送邮件?

调用api端点Django后如何发送邮件?,django,django-models,django-rest-framework,Django,Django Models,Django Rest Framework,我的应用程序使用React for frontend和Django&Django RestFramework。我正试图发送电子邮件。这是不可能的。所以我想在django中创建一个邮件模型 class Mail(models.Model): MAIL_SENT= ( ('Y', 'Yes'), ('N', 'No'), ) send_to= models.CharField(max_length=120) reply_to= mode

我的应用程序使用React for frontend和Django&Django RestFramework。我正试图发送电子邮件。这是不可能的。所以我想在django中创建一个邮件模型

class Mail(models.Model):
    MAIL_SENT= (
        ('Y', 'Yes'),
        ('N', 'No'),
    )
    send_to= models.CharField(max_length=120)
    reply_to= models.CharField(max_length=120)
    message= models.TextField()
    subject= models.TextField()
    mail_sent= models.CharField(max_length=1, default="Y", choices=MAIL_SENT)

    def __self__(self):
        return self.send_to
我想做的是在使用post方法时为Mail和send方法创建一个API端点。
有人能帮我吗?

Django提供如下发送邮件功能:

from django.core.mail import send_mail

send_mail(
    'Subject here',
    'Here is the message.',
    'from@example.com',
    ['to@example.com'],
    fail_silently=False,
)

docs链接:

Django提供如下发送邮件功能:

from django.core.mail import send_mail

send_mail(
    'Subject here',
    'Here is the message.',
    'from@example.com',
    ['to@example.com'],
    fail_silently=False,
)

文档链接:

您可以使用django
发送邮件
功能

例如:

from django.core.mail import send_mail

send_mail(
    'Subject here',
    'Here is the message.',
    'from@example.com',
    ['to@example.com'],
    fail_silently=False,
)
您必须使用您的
邮箱
,默认的
邮箱
密码

参考:

================================

所以您可以像这样创建端点

from django.core.mail import send_mail
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status


class MailSender(APIView):

    def post(self, request, format=None):
        to_email = request.data.get("to_email")
        send_mail(
            'Subject here',
            'Here is the message.',
            'from@example.com',
            ['to_email'],
            fail_silently=False,
        )
        return Response({"message": "e-mail has been sent successfully"}, 
                        status=status.HTTP_200_OK)

您还可以使用
线程发送邮件,然后返回响应。

您可以使用django
发送邮件功能

例如:

from django.core.mail import send_mail

send_mail(
    'Subject here',
    'Here is the message.',
    'from@example.com',
    ['to@example.com'],
    fail_silently=False,
)
您必须使用您的
邮箱
,默认的
邮箱
密码

参考:

================================

所以您可以像这样创建端点

from django.core.mail import send_mail
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status


class MailSender(APIView):

    def post(self, request, format=None):
        to_email = request.data.get("to_email")
        send_mail(
            'Subject here',
            'Here is the message.',
            'from@example.com',
            ['to_email'],
            fail_silently=False,
        )
        return Response({"message": "e-mail has been sent successfully"}, 
                        status=status.HTTP_200_OK)

您也可以使用
线程发送邮件
,然后返回回复。

是的,您可以在请求结束点时从视图功能indjango发送邮件。请共享您的视图集代码。您可以重写create/perform_create方法来发送电子邮件(应该使用线程发送)。是的,您可以在请求结束点时从视图功能indjango发送邮件。请共享您的视图集代码。您可以重写create/perform_create方法来发送电子邮件(应该使用线程发送)。