python - 我如何从登录到管理员的用户那里发送电子邮件?
问题描述
我正在开发一个项目,以向user
登录者发送撤回请求admin
。在这种情况下,我不必向用户发送邮件,而是必须将邮件发送到管理员官方电子邮件地址以进行通知。我用过reply_to
,但它告诉我它是unexpected argument
视图.py
@login_required
def withdraw(request):
form_class = WithdrawBalance
if request.method == 'POST':
form = form_class(request.POST)
obj = form.save(commit=False)
obj.owner = request.user
obj.save()
messages.success(request, f'Your request has been submitted.')
send_mail('New Withdrawal Request',
'Hello there, A new withdrawal request has been received.',
request.user.email, ['bilalkhangood4@gmail.com'], fail_silently=False)
return redirect('index')
else:
form = form_class()
context = {'form': form}
return render(request, 'nextone/withdraw.html', context)
设置.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'bilalkhangood4@gmail.com'
EMAIL_HOST_PASSWORD = ''
EMAIL_PORT = 587
ACCOUNT_EMAIL_VERIFICATION = 'none'
EMAIL_USE_SSL = False
解决方案
如果要提供reply_to
参数,则需要使用EmailMessage。
试试这个:
from django.core.mail import EmailMessage
email = EmailMessage(
subject='New Withdrawal Request',
body='Hello there, A new withdrawal request has been received.',
from_email='bilalkhangood4@example.com',
to=[request.user.email],
reply_to=['another@example.com'],
)
email.send()
推荐阅读
- javascript - 为什么我的 div 容器中的元素会消失?
- python - python - 列出,打印
- gstreamer - 使用 libgstvaapi.so 的 gstreamer-vaapi 插件缺少 vaapih265enc
- ruby-on-rails - 添加@到friendly_id slug Ruby on Rails
- r - R中的循环For返回Null(空)值而不是预期
- python - 如何找到列表的最长连续非零子集?
- angular - 无法从另一台机器访问在本地主机上本地运行的 Angular 应用程序
- javascript - 这是使用 localStorage 的正确方法吗?
- c++ - '自定义向量
::size': 无法访问在类“CustomVector”中声明的私有成员 ' - java - 如何停止从互联网上解析schemaLocation