发送带有附件的电子邮件

时间:2018-08-05 15:05:34

标签: python django

我正在尝试发送带有附件的电子邮件,直到附件部分都可以正常工作。它给我一个错误expected str, bytes or os.PathLike object, not NoneType

这是代码正在使用;

html

<form method="post">
{% csrf_token %}
    <hr>
    <span style="font-size: 5;" class="text-muted">Message details</span><br><br>
<b> Message title: </b><input type="text" id="msgtitle" name="msgtitle"><br><br>
    <b>Message text:</b><br>
     <textarea name="msgtxt" id="msgtxt" rows="10" cols="50">Enter text here...</textarea>
        <hr>
    <span style="font-size: 5;" class="text-muted">You contact details:</span><br><br>
<b>Email: </b><input type="email" id="email" name="email"><br><br>
   <b>Number: </b> <input type="number" id="number" name="number"><br><br>

    <input type="file" id="file" name="file">

<button class="btn btn-success" type="submit">Send</button>
</form>

views.py

def class_msg(request, pk):
    current_ad = get_object_or_404(AdPost, pk=pk)
    title = request.POST.get('msgtitle')
    txt = request.POST.get('msgtxt')
    email = request.POST.get('email')
    number = request.POST.get('number')
    file = request.FILES.get('file')
    if request.method == 'POST':
        email_msg = "You have recievied this message from Jehlum.com user - Message text (( " + txt + " )) contact details of sender Email: " + email + " phone number: " + number + " ."
        email = EmailMessage('new message no Jehlum ' + title, email_msg, to=[current_ad.contact_email])
        email.attach_file(file)
        email.send()
        return redirect('msg_succ')
    return render(request, 'msg/class_msg.html')

0 个答案:

没有答案