【问题标题】:Django rest framework: sendgrid email with attachment without model only filefield to open file and send button to send emailDjango rest 框架:sendgrid email with attachment without model only filefield to open file and send button to send email
【发布时间】:2020-02-17 17:05:54
【问题描述】:

我是 Django Rest 框架的新手。
我正在尝试发送带有附件的电子邮件。

这是我的代码。

model.py


class EmailModel(models.Model):
    upload_file = models.FileField(upload_to='location/location/files', blank=False)
    class Meta:
        verbose_name = 'Applicant CSV Upload'
        verbose_name_plural = 'Applicant CSV Upload'


admin.py

@admin.register(EmailModel)
class EmailAdmin(admin.ModelAdmin):
    class Meta:
      model = EmailModel

View.py


def send_email():
    email = EmailMessage(
        'Title',
        ('abc', 'abc@gmail.com', '123123123'),
        'abc@gmail.com',
        ['abc@gmail.com']
    )
    email.attach_file(EmailViewSet.upload_file)
    email.send()

class EmailViewSet(viewsets.ModelViewSet):
    queryset = EmailModel.objects.all()
    serializer_class = EmailSerializer
    def create(self, request, *args, **kwargs):
        send_mail(' Test Subject here', 'Test here is the message.', 'abc@gmail.com', ['abc@gmail.com'], fail_silently=False)
        response = super(EmailViewSet, self).create(request, *args, **kwargs)
        send_email()  # sending mail
        data = [{'location': request.data.get('location'), 'image': file} for file in request.FILES.getlist('image')]
        serializer = self.get_serializer(data=data, many=True)
        serializer.is_valid(raise_exception=True)
        self.perform_create(serializer)
        headers = self.get_success_headers(serializer.data)
        message = EmailMessage(subject='Applicant data', body='PFA', from_email='abc@gmail.com',
                               to='abc@gmail.com', bcc='abc@gmail.com', connection=None,
                               attachments=data, headers=self.data, cc='abc@gmail.com', reply_to=None)
        # Attach file
        # with open(attachment, 'rb') as file:
        #     message.attachments = [
        #     (attachment, file.read(), 'application/pdf')
        # ]
        return response, message.send(), Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)

Serializer.py

class EmailSerializer(serializers.ModelSerializer):
    class Meta:
        model = EmailModel
        fields = ('upload_file',)

settings.py


EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey' # this is exactly the value 'apikey'
EMAIL_HOST_PASSWORD = 'here i am using my sendgrid api key directy' # this is your API key
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
DEFAULT_FROM_EMAIL = 'here i am using same gmail id on which i have created my send grid account'

在 view.py 和 serializer.py 我已经提到了我尝试发送电子邮件的每一种方法,这就是为什么它如此混乱。任何方法都不起作用。 甚至create 方法根本不会调用。

这显示在我的 api 管理员上,我想更改保存按钮文本以发送。

  1. 我不想创建模型。这是为在管理员上显示此文件而创建的,我需要模型。
  2. 也不想将文件保存在文件夹中。这是节省。
  3. filefiled 只需打开文件并在我的硬编码电子邮件地址上按保存/发送按钮时通过电子邮件发送该文件。

【问题讨论】:

标签: python django django-rest-framework sendgrid


【解决方案1】:

如果您想发送带有附件的邮件,您已经在这里Sending emails with attachment in django询问了更多详细信息

【讨论】:

  • 我不在 django 工作,我在 Django Rest Framework 工作。
【解决方案2】:

您可以创建一个不需要模型的自定义管理页面。 This question 解决了这个问题。

现在,当您创建自定义页面时,您可以在视图中简单地使用 sendgrid 提供的 python API 并做任何您想做的事情。这是相同的python documentation

【讨论】:

  • 为什么我的覆盖创建在 View.py 和 serializer.py 中不起作用
  • 您附上的屏幕截图是您的 ModelAdmin 视图,不会调用您的 Emailviewset。
猜你喜欢
  • 2022-11-20
  • 1970-01-01
  • 2022-12-02
  • 2022-12-27
  • 2022-12-02
  • 2021-12-09
  • 2019-10-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多