Django 显示发件人电子邮件时出错
Error in Django to show the sender's email
我正在尝试查看发送给电子邮件主机的发件人电子邮件。我可以使用环境变量向 email_host_user 发送电子邮件,并允许访问雅虎帐户中安全性较低的应用程序。但是,当我执行 "from_email" 时收到错误消息:SMTPSenderRefused--(550, b'Request failed; Mailbox unavailable', 'bill@yahoo.com')--'bill@yahoo.com is just a随机发件人的电子邮件,但我无法发送到 email_host。我想我的 views.py 一定有问题?感谢您的反馈!
settings.py
EMAIL_HOST='smtp.mail.yahoo.com'
EMAIL_HOST_USER=os.environ.get('EMAIL_HOST_USER')
EMAIL_HOST_PASSWORD=os.environ.get('EMAIL_HOST_PASSWORD')
EMAIL_PORT=587
EMAIL_USE_TLS=True
EMAIL_BACKEND='django.core.mail.backends.smtp.EmailBackend'
print(os.environ.get('EMAIL_HOST_USER'))
print(os.environ.get('EMAIL_HOST_PASSWORD'))
print(os.environ.get('SECRET_KEY'))
views.py
def contact(request):
if request.method=='POST':
message=request.POST.get('message', '')
from_email=request.POST.get('from_email', '')
send_mail('Contact Form',
message,
from_email,
[settings.EMAIL_HOST_USER],
fail_silently=False
)
return render(request, 'first_app/contact.html')
contact.html
<form action="/contact" method="POST">
{% csrf_token %}
<input type="email" name="from_email" placeholder="Your email">
<textarea name="message" placeholder="Message...">
</textarea>
<input type="submit">
</form>
Yahoo 不允许您从未经授权的电子邮件中发送电子邮件。您只能使用 EMAIL_HOST_USER
电子邮件地址发送电子邮件。
您可以将 EMAIL_HOST_USER
设置为发件人,并使用通过表单提供的电子邮件地址添加 Reply-To
header。
为此你应该使用 EmailMessage
from django.core.mail import EmailMessage
def contact(request):
if request.method=='POST':
message=request.POST.get('message', '')
from_email=request.POST.get('from_email', '')
email = EmailMessage(
subject='Contact Form',
message=message,
from_email=settings.EMAIL_HOST_USER,
recipient_list=[settings.EMAIL_HOST_USER'],
reply_to=[from_email]
)
email.send(fail_silently=False)
return render(request, 'first_app/contact.html')
我正在尝试查看发送给电子邮件主机的发件人电子邮件。我可以使用环境变量向 email_host_user 发送电子邮件,并允许访问雅虎帐户中安全性较低的应用程序。但是,当我执行 "from_email" 时收到错误消息:SMTPSenderRefused--(550, b'Request failed; Mailbox unavailable', 'bill@yahoo.com')--'bill@yahoo.com is just a随机发件人的电子邮件,但我无法发送到 email_host。我想我的 views.py 一定有问题?感谢您的反馈!
settings.py
EMAIL_HOST='smtp.mail.yahoo.com'
EMAIL_HOST_USER=os.environ.get('EMAIL_HOST_USER')
EMAIL_HOST_PASSWORD=os.environ.get('EMAIL_HOST_PASSWORD')
EMAIL_PORT=587
EMAIL_USE_TLS=True
EMAIL_BACKEND='django.core.mail.backends.smtp.EmailBackend'
print(os.environ.get('EMAIL_HOST_USER'))
print(os.environ.get('EMAIL_HOST_PASSWORD'))
print(os.environ.get('SECRET_KEY'))
views.py
def contact(request):
if request.method=='POST':
message=request.POST.get('message', '')
from_email=request.POST.get('from_email', '')
send_mail('Contact Form',
message,
from_email,
[settings.EMAIL_HOST_USER],
fail_silently=False
)
return render(request, 'first_app/contact.html')
contact.html
<form action="/contact" method="POST">
{% csrf_token %}
<input type="email" name="from_email" placeholder="Your email">
<textarea name="message" placeholder="Message...">
</textarea>
<input type="submit">
</form>
Yahoo 不允许您从未经授权的电子邮件中发送电子邮件。您只能使用 EMAIL_HOST_USER
电子邮件地址发送电子邮件。
您可以将 EMAIL_HOST_USER
设置为发件人,并使用通过表单提供的电子邮件地址添加 Reply-To
header。
为此你应该使用 EmailMessage
from django.core.mail import EmailMessage
def contact(request):
if request.method=='POST':
message=request.POST.get('message', '')
from_email=request.POST.get('from_email', '')
email = EmailMessage(
subject='Contact Form',
message=message,
from_email=settings.EMAIL_HOST_USER,
recipient_list=[settings.EMAIL_HOST_USER'],
reply_to=[from_email]
)
email.send(fail_silently=False)
return render(request, 'first_app/contact.html')