def send(self): if self.get_status != constants.INVITATION_PENDING: return application_type = "" for itc in constants.INVITATION_TYPE_CHOICES: if itc[0] == self.invitation_type: application_type = itc[1] subject = "BetaSmartz {application_type} sign up form url".format( application_type=application_type) inviter_type = self.inviter_object.get_inviter_type() inviter_name = self.inviter_object.get_inviter_name() invite_url = self.inviter_object.get_invite_url( self.invitation_type, self.email) context = { 'site': Site.objects.get_current(), 'subject': subject, 'invite_url': invite_url, 'inviter_name': inviter_type, 'inviter_class': inviter_name, 'application_type': application_type } send_mail(subject, '', None, [self.email], html_message=render_to_string('email/invite.html', context)) self.send_count += 1 self.save()
def register(request): error = '' if request.method == "GET": return render_to_response("usercenter_register.html", {}, context_instance=RequestContext(request)) else: username = request.POST['username'].strip() email = request.POST['email'].strip() password = request.POST['password'].strip() re_password = request.POST['re_password'].strip() if not username or not password or not email: error = u'任何字段都不能为空' if password != re_password: error = u'两次密码不一致' if User.objects.filter(username=username).count() > 0: error = u'用户名已存在' if not error: user = User.objects.create_user(username=username, email=email, password=password) user.is_active = False user.save() new_code = str(uuid.uuid4()).replace('-', '') expire_time = datetime.datetime.now() + datetime.timedelta(days=2) code_record = ActivateCode(owner=user, expire_timestamp=expire_time, code=new_code) code_record.save() activate_link = "http://%s%s" % (request.get_host(), reverse("usercenter_activate", args=[new_code])) send_mail(u'[Python部落论坛]激活邮件', u'您的激活码链接为:%s' % activate_link, '*****@*****.**', [email], fail_silently=False) else: return render_to_response("usercenter_register.html", {"error": error}, context_instance=RequestContext(request)) return redirect(reverse("login"))
def email_user(self, subject, message="", from_email=settings.DEFAULT_FROM_EMAIL, **kwargs): """ Sends an email to this User. """ send_mail(subject, message, from_email, [self.email], **kwargs)
def form_valid(self, form): user = form.save(commit=False) user.is_agent = True user.is_organizer = False user.set_password(f"{random.randint(0, 10000000)}") user.save() agent = Agent.objects.create( user=user, organization=self.request.user.userprofile) agent.save() send_mail( subject="You are invited to be an agent", message= "You were added as an agent on DJCRM, please login to start working", from_email="*****@*****.**", recipient_list=[user.email]) return super(AgentCreateView, self).form_valid(form)
def save(self, force_insert=False, force_update=False, using=None, update_fields=None): super().save(force_insert=force_insert, force_update=force_update, using=using, update_fields=update_fields) try: qc = QrCode() qc.gen_qrcode(self.user.username, 'webKeepass') self.secret_key = qc.secret_key self.qrcode_img_file = qc.img_file email_tmpl = os.path.join(settings.BASE_DIR, 'templates', 'portal','email_template.html') with open(email_tmpl, 'r') as tmpl: html = tmpl.read() ret = send_mail( subject='webKeepass MFA认证', message='', from_email='*****@*****.**', recipient_list=[self.user.email], fail_silently=False, html_message=html.format( username=self.user.username, qrimage=qc.img_str, site_url=settings.SITE_URL ) ) if not ret: logger.info(f"{self.user.username}发送mfa qrcode image失败") super().save() except Exception as e: logger.error(f"{self.user.username}创建失败,失败原因 {str(e)}") raise BaseException(f"创建失败,原因{e} {traceback.format_exc()}") return self
def email_user(self, subject, message, from_email=None): send_mail(subject, message, from_email, [self.email])
def email_user(self, subject, message, from_email=None): """ Sends an email to this User. """ send_mail(subject, message, from_email, [self.email])
def email_user(self, subject, message, from_email=None, **kwargs): """Send an email to this user.""" send_mail(subject, message, from_email, [self.email], **kwargs)
def email_user(self, subject, message, from_email=None, **kwargs): if self.email: send_mail(subject, message, from_email, [self.email], **kwargs)