在使用Django开发网站时,当新用户注册时,可以通过以下步骤发送电子邮件给管理员以获得批准或拒绝:
from django.core.mail import send_mail
from django.template.loader import render_to_string
from django.utils.html import strip_tags
def send_approval_email(user):
subject = '新用户注册待审批'
html_message = render_to_string('approval_email.html', {'user': user})
plain_message = strip_tags(html_message)
from_email = 'noreply@example.com'
to_email = 'admin@example.com'
send_mail(subject, plain_message, from_email, [to_email], html_message=html_message)
在上述代码中,send_approval_email
函数用于发送邮件,其中approval_email.html
是邮件模板文件,user
是新注册用户的信息。
send_approval_email
函数,将新用户的信息传递给该函数。def register(request):
# 处理用户注册逻辑
# ...
# 发送邮件给管理员
send_approval_email(new_user)
# 返回注册成功页面或其他逻辑
# ...
通过以上步骤,当新用户在网站上注册时,会触发发送邮件的逻辑,管理员会收到一封包含新用户注册信息的电子邮件,以便进行批准或拒绝操作。
没有搜到相关的结果
领取专属 10元无门槛券
手把手带您无忧上云