def sendInvites(payload, req): from django.core import mail from django.core.mail import EmailMessage uid = UR.getUserId(req); id_ensemble = payload["id_ensemble"] id_section = payload.get("id_section", None) admin = 0 if "admin" not in payload else payload["admin"] if not auth.canSendInvite(uid,id_ensemble): return UR.prepare_response({}, 1, "NOT ALLOWED") #extract emails in a somewhat robust fashion (i.e. using several possible delimiters) emails = parseEntities(payload["to"], [",", "\n", " "]) #remove spurious stuff: strings that don't have an "@" and trailings "<" and ">" characters, #because some emails the following format: John Doe <*****@*****.**> emails = [o.replace("<", "").replace(">", "") for o in emails if "@" in o] logging.info("to: %s, extracted: %s" % (payload["to"], emails)) #add new users to DB w/ pending status connection = mail.get_connection() emailmessages = [] for email in emails: user = auth.user_from_email(email) password="" if user is None: ckey = "".join([ random.choice(string.ascii_letters+string.digits) for i in xrange(0,32)]) password = "".join([ random.choice(string.ascii_letters+string.digits) for i in xrange(0,4)]) user = auth.addUser(email, password, ckey) invite_key = "".join([ random.choice(string.ascii_letters+string.digits) for i in xrange(0,50)]) auth.addInvite(invite_key, user.id, id_ensemble, id_section, admin) link = "http://%s/confirm_invite?invite_key=%s" % (settings.HOSTNAME, invite_key,) ensemble = M.Ensemble.objects.get(pk=id_ensemble) p = { "name": ensemble.name, "description": ensemble.description, "link": link, "contact": user.firstname if user.firstname != None else user.email } if payload["msg"] != "": p["msg_perso"] = payload["msg"] # TODO: We still include the password in the e-mail, should we stop doing that? if password != "": p["password"] = password p["email"] = email msg = render_to_string("email/msg_invite",p) bcc = [] if settings.SMTP_CC_USER is None else (settings.SMTP_CC_USER,) e = EmailMessage("You're invited on the %s channel !" % (p["name"],), msg, settings.EMAIL_FROM, (email, ), bcc,connection=connection) emailmessages.append(e) #time.sleep(SLEEPTIME) #in order not to stress out the email server connection.send_messages(emailmessages) return UR.prepare_response({"msg": "Invite for %s sent to %s" % (ensemble.name, emails,)})