How to send email attachments?

前端 未结 13 1210
梦谈多话
梦谈多话 2020-11-22 01:14

I am having problems understanding how to email an attachment using Python. I have successfully emailed simple messages with the smtplib. Could someone please e

13条回答
  •  滥情空心
    2020-11-22 01:36

    The simplest code I could get to is:

    #for attachment email
    from django.core.mail import EmailMessage
    
        def attachment_email(request):
                email = EmailMessage(
                'Hello', #subject
                'Body goes here', #body
                'MyEmail@MyEmail.com', #from
                ['SendTo@SendTo.com'], #to
                ['bcc@example.com'], #bcc
                reply_to=['other@example.com'],
                headers={'Message-ID': 'foo'},
                )
    
                email.attach_file('/my/path/file')
                email.send()
    

    It was based on the official Django documentation

提交回复
热议问题