首页 > 解决方案 > 我如何从登录到管理员的用户那里发送电子邮件?

问题描述

我正在开发一个项目,以向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

标签: pythonhtmldjangoemail

解决方案


如果要提供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()

推荐阅读