我在通过电子邮件发送FileField附件时遇到问题。这应该发送,但是没有。我是否需要在我的gmail帐户中配置一些东西才能正常工作?
settings.py
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'email@gmail.com'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_PORT = 587
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
views.py
#i.output is the FieldFile
message = 'Thanks for using our website!'
email = EmailMessage('Analysis', message, settings.EMAIL_HOST_USER, [toEmail])
email.attach(filename, i.output.read())
email.send()
我甚至收到一封来自Gmail的电子邮件,告诉我登录尝试被阻止了。任何帮助吗?谢谢!
如果你没有在.attach调用中指定mime类型,Django会尝试为你猜测mime类型——它可能会在正确猜测给定附件的mime类型时遇到困难。如果mime类型不正确,gmail可能会在收到时将其退回。或者指定mime类型:
email.attach('image_name.png', img_data, 'image/png')
或者你可以使用电子邮件。如果你想让Django尝试为你解决这个问题,可以使用attach_file方法:
email = EmailMessage(subject='Analysis', body=message,
from_email=settings.EMAIL_HOST_USER, to=[toEmail])
email.attach_file(filename)
email.send(fail_silently=not(settings.DEBUG))