如何在 Django 中将客户上传的文件附加到电子邮件?

问题描述 投票:0回答:1

我正在尝试制作一个表单,客户可以在其中添加文件并通过页面发送。数据库中没有保存文件,只需将其附加并发送到公司电子邮件即可。 详情请看这里

forms.py

class ContactForm(forms.Form):
    name = forms.CharField(max_length=120, widget=forms.TextInput(attrs={'placeholder': 'Your name'}))
    phone_number = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Phone number'}))
    email = forms.CharField(widget=forms.TextInput(attrs={'placeholder': '[email protected]'}))
    customer_details = forms.FileField(required=False, widget=forms.ClearableFileInput(
        attrs={'multiple': False, 'allow_empty_file': True}))
    message = forms.CharField(widget=forms.Textarea(attrs={'placeholder': 'Your message'}))

emailsender.py

from django.core.mail import send_mail


class EmailSender:
    def __send_email(self, subject, message, recipient_list):
        send_mail(
            subject=subject,
            message=message,
            from_email=EMAIL_HOST_USER,
            auth_password=EMAIL_HOST_PASSWORD,
            recipient_list=recipient_list
        )

    def send_submitted_order(self, request, offers):
        subject = f'Order {datetime.now().strftime("%Y-%m-%d %H:%M:%S.")}'
        recipient_list = [RECIPIENT_EMAIL]
        form = ContactForm(request.POST, request.FILES or None)
        if form.is_valid():
            customer_email = form.cleaned_data['email']
            customer_phone = form.cleaned_data['phone_number']
            customer_message = form.cleaned_data['message']
            **customer_details = form.cleaned_data['customer_details']**
            message = render_to_string(
                'cart/message_for_company.html',
                {'customer_message': customer_message, 'customer_email': customer_email,
                    'customer_phone': customer_phone, 'offers': offers}
            )
            self.__send_email(subject, message, recipient_list)

    def send_message_to_customer(self, request, offers):
        form = ContactForm(request.POST, request.FILES or None)
        subject = f'Order {datetime.now().strftime("%Y-%m-%d %H:%M:%S.")}'
        if form.is_valid():
            customer_email = form.cleaned_data['email']
            customer_phone = form.cleaned_data['phone_number']
            customer_message = form.cleaned_data['message']
            **customer_details = form.cleaned_data['customer_details']**
            message = render_to_string(
                'cart/message_for_customer.html',
                {'customer_message': customer_message, 'customer_email': customer_email,
                 'customer_phone': customer_phone, 'offers': offers}
            )
            recipient_list = [customer_email]
            self.__send_email(subject, message, recipient_list)

和 render_to_string 的简约模板

<h1>Order</h1>

{% for offer in offers %}
    {{ offer.name }} - {{ offer.quantity }}
        {% endfor %}
    {{ customer_message }}
    {{ customer_phone }}
    {{ customer_email }}
    {{ customer_details }}

render_to_string 不允许使用 message.attach(company_details) 创建附件。我已经阅读了有关 SOF 的所有文档和其他类似问题,但仍然无法弄清楚。

django django-forms django-templates django-email
1个回答
0
投票

没关系,已经找到答案了,稍后会发布解决方案

© www.soinside.com 2019 - 2024. All rights reserved.