python - 如何从登录管理员的用户发送电子邮件?

标签 python html django email

我正在开发一个项目,用于向登录admin用户发送提款请求。在这种情况下,我不必向用户发送邮件,而是必须向管理员官方电子邮件地址发送邮件以获取通知。我使用了reply_to,但它告诉我这是意外的参数

views.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)

settings.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()

关于python - 如何从登录管理员的用户发送电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57603590/

相关文章:

python - xhtml2pdf 未正确转换 css django

python - Pygame 在表面上绘图与直接在显示器上绘图

python - 如何在Python中检查数据帧是否包含字符串?

python - 将超链接添加到由 pandas dataframe to_excel 方法创建的 excel 表

javascript - 替换jquery中的span html文本

jquery - 让我的翻转 div 响应

python - 在数据框中查找非单调行

html - css中嵌套容器的高度

python - 测试模型序列化器

python - 添加自定义 Django 模型验证