def verify_emails(self, ids, context={}): obj = self.browse(ids[0]) i = 0 domain_cache = {} last_smtp_host = None last_smtp_time = None for target in get_model("mkt.target").search_browse([["list_id", "=", obj.id]], order="email,id"): i += 1 if target.email_status == "verified": continue print("verifying email %s (%d)" % (target.email, i)) try: email_status = "error_syntax" if not utils.check_email_syntax(target.email): raise Exception("Invalid email syntax") email_status = "error_dns" domain = utils.get_email_domain(target.email) if domain in domain_cache: mx_records = domain_cache[domain] else: mx_records = utils.get_mx_records(domain) domain_cache[domain] = mx_records if not mx_records: raise Exception("MX record not found") email_status = "error_smtp" host = mx_records[0][1] if last_smtp_host and last_smtp_time and host == last_smtp_host and time.time() - last_smtp_time < 5: print("sleeping before connecting again to %s..." % host) time.sleep(5) last_smtp_host = host last_smtp_time = time.time() serv = smtplib.SMTP(timeout=15) try: serv.connect(host) except: raise Exception("Failed to connect to SMTP server") status, _ = serv.helo() if status != 250: serv.quit() raise Exception("Invalid SMTP HELO response code: %s" % status) serv.mail("") status, _ = serv.rcpt(target.email) if status != 250: serv.quit() raise Exception("Invalid SMTP RCPT response code: %s" % status) serv.quit() email_status = "verified" email_error = None except Exception as e: email_error = str(e) import traceback traceback.print_exc() finally: target.write({"email_status": email_status, "email_error": email_error}) db = database.get_connection() db.commit()
def send_email_mailgun(self, ids, context={}): print("send_emails_mailgun", ids) obj = self.browse(ids)[0] if obj.state not in ("draft", "to_send"): return try: mailbox = obj.mailbox_id if not mailbox: raise Exception("Missing mailbox") account = mailbox.account_id if account.type != "mailgun": raise Exception("Invalid email account type") url = "https://api.mailgun.net/v2/%s/messages" % account.user to_addrs = [] for a in obj.to_addrs.split(","): a = a.strip() if not utils.check_email_syntax(a): raise Exception("Invalid email syntax: %s" % a) to_addrs.append(a) if not to_addrs: raise Exception("Missing recipient address") data = { "from": obj.from_addr, "to": to_addrs, "subject": obj.subject, } if obj.cc_addrs: data["cc"] = [a.strip() for a in obj.cc_addrs.split(",")] data["html"] = obj.body or "<html><body></body></html>" files = [] for attach in obj.attachments: path = utils.get_file_path(attach.file) f = open(path, "rb") files.append(("attachment", f)) r = requests.post(url, auth=("api", account.password), data=data, files=files, timeout=15) try: res = json.loads(r.text) msg_id = res["id"] except: raise Exception("Invalid mailgun response: %s" % r.text) obj.write({"state": "sent", "message_id": msg_id}) except Exception as e: print("WARNING: failed to send email %s" % obj.id) import traceback traceback.print_exc() obj.write({"state": "error", "error_message": str(e)})
def send_email_mailgun(self, ids, context={}): print("send_emails_mailgun", ids) obj = self.browse(ids)[0] if obj.state not in ("draft","to_send"): return try: mailbox = obj.mailbox_id if not mailbox: raise Exception("Missing mailbox") account = mailbox.account_id if account.type != "mailgun": raise Exception("Invalid email account type") url = "https://api.mailgun.net/v2/%s/messages" % account.user to_addrs = [] for a in obj.to_addrs.split(","): a = a.strip() if not utils.check_email_syntax(a): raise Exception("Invalid email syntax: %s" % a) to_addrs.append(a) if not to_addrs: raise Exception("Missing recipient address") data = { "from": obj.from_addr, "to": to_addrs, "subject": obj.subject, } if obj.cc_addrs: data["cc"] = [a.strip() for a in obj.cc_addrs.split(",")] data["html"] = obj.body or "<html><body></body></html>" files = [] for attach in obj.attachments: path = utils.get_file_path(attach.file) f = open(path, "rb") files.append(("attachment", f)) r = requests.post(url, auth=("api", account.password), data=data, files=files,timeout=15) try: res = json.loads(r.text) msg_id = res["id"] except: raise Exception("Invalid mailgun response: %s" % r.text) obj.write({"state": "sent", "message_id": msg_id}) except Exception as e: print("WARNING: failed to send email %s" % obj.id) import traceback traceback.print_exc() obj.write({"state": "error", "error_message": str(e)})
def post(self): db = get_connection() try: email = self.get_argument("email", None) if not email: raise Exception("Missing email") if not utils.check_email_syntax(email): raise Exception("Wrong Email syntax") res = get_model("base.user").search([["login", "=", email]]) if not res: raise Exception("Email not found") forgot_id = get_model("cms.forgot.passwd").create({"email": email}) forgot_obj = get_model("cms.forgot.passwd").browse(forgot_id) forgot_obj.trigger("forgot") ctx = self.context ctx["success"] = True ctx["message"] = "Reset password request was sent to your email. Please follow the instruction in email." content = render("cms_forgot_passwd", ctx) ctx["content"] = content html = render("cms_layout", ctx) self.write(html) db.commit() except Exception as e: try: ctx = self.context ctx["error"] = True ctx["message"] = str(e) ctx["email"] = email content = render("cms_forgot_passwd", ctx) ctx["content"] = content html = render("cms_layout", ctx) self.write(html) db.commit() except: import traceback traceback.print_exc() db.rollback()
def post(self): db=get_connection() try: email=self.get_argument("email",None) if not email: raise Exception("Missing email") if not utils.check_email_syntax(email): raise Exception("Wrong Email syntax") res=get_model("base.user").search([["login","=",email]]) if not res: raise Exception("Email not found") forgot_id = get_model("cms.forgot.passwd").create({"email": email}) forgot_obj = get_model("cms.forgot.passwd").browse(forgot_id) forgot_obj.trigger("forgot") ctx=self.context ctx["success"]=True ctx["message"]="Reset password request was sent to your email. Please follow the instruction in email."; content=render("cms_forgot_passwd",ctx) ctx["content"]=content html=render("cms_layout",ctx) self.write(html) db.commit() except Exception as e: try: ctx=self.context ctx["error"]=True ctx["message"]=str(e) ctx["email"]=email content=render("cms_forgot_passwd",ctx) ctx["content"]=content html=render("cms_layout",ctx) self.write(html) db.commit() except: import traceback traceback.print_exc() db.rollback()
def check_email(self,ids,context={}): for obj in self.browse(ids): if not obj.email: continue if not utils.check_email_syntax(obj.email): raise Exception("Invalid email for contact '%s'"%obj.name)
def check_email(self, ids, context={}): for obj in self.browse(ids): if not obj.email: continue if not utils.check_email_syntax(obj.email): raise Exception("Invalid email for contact '%s'" % obj.name)
def post(self): db = get_connection() try: if self.get_argument("commit", None): cart_id = self.get_argument("cart_id") cart_id = int(cart_id) fnames = [ "email", "bill_first_name", "bill_last_name", "bill_company", "bill_address", "bill_address2", "bill_city", "bill_postal_code", "bill_country_id", "bill_district_id", "bill_subdistrict_id", "bill_province_id", "bill_phone", "ship_to_bill", "ship_first_name", "ship_last_name", "ship_company", "ship_address", "ship_address2", "ship_city", "ship_postal_code", "ship_country_id", "ship_province_id", "ship_district_id", "ship_subdistrict_id", "ship_phone", ] form_vals = {} for n in fnames: v = self.get_argument(n, None) f = get_model("ecom.cart")._fields[n] if isinstance(f, fields.Boolean): v = v and True or False elif isinstance(f, fields.Many2One): v = int(v) if v else None form_vals[n] = v print("FORM VALS", form_vals) field_errors = {} try: req_fields = [ "email", "bill_first_name", "bill_last_name", "bill_address", "bill_postal_code", "bill_country_id", #"bill_city", #"bill_province_id", #"bill_district_id", #"bill_subdistrict_id", "bill_phone", ] if not form_vals.get("ship_to_bill"): req_fields += [ "ship_first_name", "ship_last_name", "ship_address", "ship_postal_code", "ship_country_id", #"ship_city", #"ship_province_id", #"ship_district_id", #"ship_subdistrict_id", "ship_phone", ] missing = [] for n in req_fields: if not form_vals.get(n): missing.append(n) field_errors[n] = True if missing: print("missing",missing) raise Exception("Some required fields are missing") email = form_vals["email"] user_id = get_active_user() cart = get_model("ecom.cart").browse(cart_id) if not utils.check_email_syntax(form_vals["email"]): raise Exception("Invalid email syntax!!") if user_id: user = get_model("base.user").browse(user_id) if email != user.email: raise Exception("Email does not match logged in account email (%s)" % user.mail) else: res = get_model("base.user").search([["email", "=", email]]) if res: raise Exception("An account already exists with that email, please login first") except Exception as e: error_message = str(e) ctx = self.context ctx["form_vals"] = form_vals ctx["error_message"] = error_message if error_message.startswith("We apologize for the inconvenient"): ctx["hide_checkout"] = "yes" ctx["field_errors"] = field_errors content = render("ecom_checkout", ctx) ctx["content"] = content html = render("cms_layout", ctx) self.write(html) return if form_vals.get("ship_to_bill"): form_vals["ship_first_name"] = form_vals["bill_first_name"] form_vals["ship_last_name"] = form_vals["bill_last_name"] form_vals["ship_address"] = form_vals["bill_address"] form_vals["ship_address2"] = form_vals["bill_address2"] form_vals["ship_company"] = form_vals["bill_company"] form_vals["ship_postal_code"] = form_vals["bill_postal_code"] form_vals["ship_country_id"] = form_vals["bill_country_id"] form_vals["ship_city"] = form_vals["bill_city"] form_vals["ship_province_id"] = form_vals["bill_province_id"] form_vals["ship_district_id"] = form_vals["bill_district_id"] form_vals["ship_subdistrict_id"] = form_vals["bill_subdistrict_id"] form_vals["ship_phone"] = form_vals["bill_phone"] get_model("ecom.cart").write([cart_id], form_vals) methods = get_model("ecom.cart").get_ship_methods([cart_id]) if methods: get_model("ecom.cart").write([cart_id], {"ship_method_id": methods[0]["method_id"]}) self.redirect("/ecom_checkout2") db.commit() except: import traceback traceback.print_exc() db.rollback()
def post(self): db=get_connection() try: try: cart_id=self.get_argument("cart_id",None) fields=["first_name","last_name","email","password","re_password"] form_vals={} if cart_id: cart_id=int(cart_id) cart=get_model("ecom.cart").browse(cart_id) password=self.get_argument("password",None) form_vals={ "first_name": cart.bill_first_name, "last_name": cart.bill_last_name, "email": cart.email, "password": password, "re_password": password, } else: cart_id=self.get_cookie("cart_id") #In case of have a cart and register with register form for n in fields: form_vals[n]=self.get_argument(n,None) field_errors={} for n in fields: if not form_vals.get(n): field_errors[n]=True if field_errors: raise Exception("Some required fields are missing") website=self.context["website"] if not website.user_profile_id.id: raise Exception("Missing user profile in website settings") res=get_model("base.user").search([["login","=",form_vals["email"]]]) if res: raise Exception("An account with this email already exists") if len(form_vals["password"])<6: raise Exception("Password is too short (Minimum 6 Characters)") if form_vals["password"] != form_vals["re_password"]: raise Exception("Password and Re-Type Password does not match!") vals={ "name": form_vals["first_name"]+" "+form_vals["last_name"], "login": form_vals["email"], "password": form_vals["password"], "email": form_vals["email"], "profile_id": website.user_profile_id.id, } user_id=get_model("base.user").create(vals) get_model("base.user").trigger([user_id],"create_user",context={"password": form_vals["password"]}) if not website.contact_categ_id.id: raise Exception("Missing contact category in website settings") if not utils.check_email_syntax(form_vals["email"]): raise Exception("Invalid email syntax!!") res=get_model("contact").search([["email","=",form_vals["email"]],["categ_id","=",website.contact_categ_id.id]]) if res: contact_id=res[0] else: vals={ "type": "person", "first_name": form_vals["first_name"], "last_name": form_vals["last_name"], "email": form_vals["email"], "categ_id": website.contact_categ_id.id, "account_receivable_id": website.account_receivable_id.id, "customer" : True, } contact_id=get_model("contact").create(vals) get_model("contact").trigger([contact_id],"ecom_register") get_model("contact").write([contact_id],{"user_id":user_id}) get_model("base.user").write([user_id],{"contact_id":contact_id}) tmpl=website.create_account_email_tmpl_id if tmpl: data={ "email": form_vals["email"], "first_name": form_vals["first_name"], "last_name": form_vals["last_name"], "new_password": form_vals["password"], } tmpl.create_email(data) dbname=get_active_db() token=new_token(dbname,user_id) print("commit") db.commit() self.set_cookie("user_id",str(user_id)) self.set_cookie("token",token) print("redirect") if cart_id: if user_id: set_active_user(user_id) cart_id=int(cart_id) get_model("ecom.cart").set_default_address([cart_id]) self.next_page() except Exception as e: db=get_connection() error_message=str(e) ctx=self.context ctx["form_vals"]=form_vals ctx["error_message"]=error_message ctx["field_errors"]=field_errors content=render("cms_register",ctx) ctx["content"]=content html=render("cms_layout",ctx) self.write(html) print("commit") db.commit() except: import traceback traceback.print_exc() print("rollback") db.rollback()
def post(self): db = get_connection() try: if self.get_argument("commit", None): cart_id = self.get_argument("cart_id") cart_id = int(cart_id) fnames = [ "email", "bill_first_name", "bill_last_name", "bill_company", "bill_address", "bill_address2", "bill_city", "bill_postal_code", "bill_country_id", "bill_district_id", "bill_subdistrict_id", "bill_province_id", "bill_phone", "ship_to_bill", "ship_first_name", "ship_last_name", "ship_company", "ship_address", "ship_address2", "ship_city", "ship_postal_code", "ship_country_id", "ship_province_id", "ship_district_id", "ship_subdistrict_id", "ship_phone", ] form_vals = {} for n in fnames: v = self.get_argument(n, None) f = get_model("ecom.cart")._fields[n] if isinstance(f, fields.Boolean): v = v and True or False elif isinstance(f, fields.Many2One): v = int(v) if v else None form_vals[n] = v print("FORM VALS", form_vals) field_errors = {} try: req_fields = [ "email", "bill_first_name", "bill_last_name", "bill_address", "bill_postal_code", "bill_country_id", #"bill_city", #"bill_province_id", #"bill_district_id", #"bill_subdistrict_id", "bill_phone", ] if not form_vals.get("ship_to_bill"): req_fields += [ "ship_first_name", "ship_last_name", "ship_address", "ship_postal_code", "ship_country_id", #"ship_city", #"ship_province_id", #"ship_district_id", #"ship_subdistrict_id", "ship_phone", ] missing = [] for n in req_fields: if not form_vals.get(n): missing.append(n) field_errors[n] = True if missing: print("missing", missing) raise Exception("Some required fields are missing") email = form_vals["email"] user_id = get_active_user() cart = get_model("ecom.cart").browse(cart_id) if not utils.check_email_syntax(form_vals["email"]): raise Exception("Invalid email syntax!!") if user_id: user = get_model("base.user").browse(user_id) if email != user.email: raise Exception( "Email does not match logged in account email (%s)" % user.mail) else: res = get_model("base.user").search( [["email", "=", email]]) if res: raise Exception( "An account already exists with that email, please login first" ) except Exception as e: error_message = str(e) ctx = self.context ctx["form_vals"] = form_vals ctx["error_message"] = error_message if error_message.startswith( "We apologize for the inconvenient"): ctx["hide_checkout"] = "yes" ctx["field_errors"] = field_errors content = render("ecom_checkout", ctx) ctx["content"] = content html = render("cms_layout", ctx) self.write(html) return if form_vals.get("ship_to_bill"): form_vals["ship_first_name"] = form_vals["bill_first_name"] form_vals["ship_last_name"] = form_vals["bill_last_name"] form_vals["ship_address"] = form_vals["bill_address"] form_vals["ship_address2"] = form_vals["bill_address2"] form_vals["ship_company"] = form_vals["bill_company"] form_vals["ship_postal_code"] = form_vals[ "bill_postal_code"] form_vals["ship_country_id"] = form_vals["bill_country_id"] form_vals["ship_city"] = form_vals["bill_city"] form_vals["ship_province_id"] = form_vals[ "bill_province_id"] form_vals["ship_district_id"] = form_vals[ "bill_district_id"] form_vals["ship_subdistrict_id"] = form_vals[ "bill_subdistrict_id"] form_vals["ship_phone"] = form_vals["bill_phone"] get_model("ecom.cart").write([cart_id], form_vals) methods = get_model("ecom.cart").get_ship_methods([cart_id]) if methods: get_model("ecom.cart").write( [cart_id], {"ship_method_id": methods[0]["method_id"]}) self.redirect("/ecom_checkout2") db.commit() except: import traceback traceback.print_exc() db.rollback()