def validate(self): self.set_lead_name() self.set_title() self._prev = frappe._dict({ "contact_date": frappe.db.get_value("Lead", self.name, "contact_date") if (not cint(self.is_new())) else None, "ends_on": frappe.db.get_value("Lead", self.name, "ends_on") if (not cint(self.is_new())) else None, "contact_by": frappe.db.get_value("Lead", self.name, "contact_by") if (not cint(self.is_new())) else None, }) self.set_status() self.check_email_id_is_unique() if self.email_id: if not self.flags.ignore_email_validation: validate_email_address(self.email_id, True) if self.email_id == self.lead_owner: frappe.throw(_("Lead Owner cannot be same as the Lead")) if self.email_id == self.contact_by: frappe.throw(_("Next Contact By cannot be same as the Lead Email Address")) if self.is_new() or not self.image: self.image = has_gravatar(self.email_id) if self.contact_date and getdate(self.contact_date) < getdate(nowdate()): frappe.throw(_("Next Contact Date cannot be in the past")) if self.ends_on and self.contact_date and (self.ends_on < self.contact_date): frappe.throw(_("Ends On date cannot be before Next Contact Date."))
def set_sender_full_name(self): if not self.sender_full_name and self.sender: if self.sender == "Administrator": self.sender_full_name = frappe.db.get_value( "User", "Administrator", "full_name") self.sender = frappe.db.get_value("User", "Administrator", "email") elif self.sender == "Guest": self.sender_full_name = self.sender self.sender = None else: if self.sent_or_received == "Sent": validate_email_address(self.sender, throw=True) sender_name, sender_email = parse_addr(self.sender) if sender_name == sender_email: sender_name = None self.sender = sender_email self.sender_full_name = sender_name if not self.sender_full_name: self.sender_full_name = frappe.db.get_value( "User", self.sender, "full_name") if not self.sender_full_name: first_name, last_name = frappe.db.get_value( "Contact", filters={"email_id": sender_email}, fieldname=["first_name", "last_name"]) or [None, None] self.sender_full_name = (first_name or "") + (last_name or "") if not self.sender_full_name: self.sender_full_name = sender_email
def set_sender_full_name(self): if not self.sender_full_name and self.sender: if self.sender == "Administrator": self.sender_full_name = frappe.db.get_value("User", "Administrator", "full_name") self.sender = frappe.db.get_value("User", "Administrator", "email") elif self.sender == "Guest": self.sender_full_name = self.sender self.sender = None else: if self.sent_or_received=='Sent': validate_email_address(self.sender, throw=True) sender_name, sender_email = parse_addr(self.sender) if sender_name == sender_email: sender_name = None self.sender = sender_email self.sender_full_name = sender_name if not self.sender_full_name: self.sender_full_name = frappe.db.get_value('User', self.sender, 'full_name') if not self.sender_full_name: first_name, last_name = frappe.db.get_value('Contact', filters={'email_id': sender_email}, fieldname=['first_name', 'last_name'] ) or [None, None] self.sender_full_name = (first_name or '') + (last_name or '') if not self.sender_full_name: self.sender_full_name = sender_email
def validate(self): self.check_email_id_is_unique() if self.email_id: validate_email_address(self.email_id, True) if not self.applicant_name and self.email_id: guess = self.email_id.split('@')[0] self.applicant_name = ' '.join([p.capitalize() for p in guess.split('.')])
def validate(self): """Validate email id and check POP3/IMAP and SMTP connections is enabled.""" if self.email_id: validate_email_address(self.email_id, True) if frappe.local.flags.in_patch or frappe.local.flags.in_test: return if not frappe.local.flags.in_install and not frappe.local.flags.in_patch: try: if self.use_imap: if self.use_ssl: test = imaplib.IMAP4_SSL(self.email_server, port=get_port(self)) else: test = imaplib.IMAP4(self.email_server, port=get_port(self)) else: if self.use_ssl: test = poplib.POP3_SSL(self.email_server, port=get_port(self)) else: test = poplib.POP3(self.email_server, port=get_port(self)) except Exception: frappe.throw(_("Incoming email account not correct")) return None finally: try: if self.use_imap: test.logout() else: test.quit() except Exception: pass try: if self.use_ssl_for_outgoing: print(self.smtp_port) if not self.smtp_port: self.smtp_port = 465 sess = smtplib.SMTP_SSL((self.smtp_server or "").encode('utf-8'), cint(self.smtp_port) or None) else: if self.use_tls and not self.smtp_port: self.smtp_port = 587 sess = smtplib.SMTP(cstr(self.smtp_server or ""), cint(self.smtp_port) or None) sess.quit() except Exception as e: frappe.throw(_("Outgoing email account not correct")) return None return
def validate(self): """Validate Email Address and check POP3/IMAP and SMTP connections is enabled.""" if self.email_id: validate_email_address(self.email_id, True) if self.login_id_is_different: if not self.login_id: frappe.throw(_("Login Id is required")) else: self.login_id = None duplicate_email_account = frappe.get_all("Email Account", filters={ "email_id": self.email_id, "name": ("!=", self.name) }) if duplicate_email_account: frappe.throw( _("Email ID must be unique, Email Account already exists \ for {0}".format(frappe.bold(self.email_id)))) if frappe.local.flags.in_patch or frappe.local.flags.in_test: return #if self.enable_incoming and not self.append_to: # frappe.throw(_("Append To is mandatory for incoming mails")) if (not self.awaiting_password and not frappe.local.flags.in_install and not frappe.local.flags.in_patch): if self.password or self.smtp_server in ('127.0.0.1', 'localhost'): if self.enable_incoming: self.get_incoming_server() self.no_failed = 0 if self.enable_outgoing: self.check_smtp() else: if self.enable_incoming or (self.enable_outgoing and not self.no_smtp_authentication): frappe.throw( _("Password is required or select Awaiting Password")) if self.notify_if_unreplied: if not self.send_notification_to: frappe.throw( _("{0} is mandatory").format( self.meta.get_label("send_notification_to"))) for e in self.get_unreplied_notification_emails(): validate_email_address(e, True) if self.enable_incoming and self.append_to: valid_doctypes = [d[0] for d in get_append_to()] if self.append_to not in valid_doctypes: frappe.throw( _("Append To can be one of {0}").format( comma_or(valid_doctypes)))
def validate(self): if not self.applicant_name: self.applicant_name = self.first_name + " " + self.last_name if self.email_id: validate_email_address(self.email_id, True) if not self.applicant_name and self.email_id: guess = self.email_id.split('@')[0] self.applicant_name = ' '.join( [p.capitalize() for p in guess.split('.')])
def validate(self): if self.email_id: validate_email_address(self.email_id, True) if self.employee_referral: self.set_status_for_employee_referral() if not self.applicant_name and self.email_id: guess = self.email_id.split("@")[0] self.applicant_name = " ".join([p.capitalize() for p in guess.split(".")])
def validate_emails(self): """Cleanup list of emails""" if "," in self.email_to: self.email_to.replace(",", "\n") valid = [] for email in self.email_to.split(): if email: validate_email_address(email, True) valid.append(email) self.email_to = "\n".join(valid)
def validate_emails(self): '''Cleanup list of emails''' if ',' in self.email_to: self.email_to.replace(',', '\n') valid = [] for email in self.email_to.split(): if email: validate_email_address(email, True) valid.append(email) self.email_to = '\n'.join(valid)
def validate_email_id(self): if self.email_id: if not self.flags.ignore_email_validation: validate_email_address(self.email_id, throw=True) if self.email_id == self.lead_owner: frappe.throw(_("Lead Owner cannot be same as the Lead")) if self.email_id == self.contact_by: frappe.throw(_("Next Contact By cannot be same as the Lead Email Address")) if self.is_new() or not self.image: self.image = has_gravatar(self.email_id)
def get_list_of_recipients(self, doc, context): recipients = [] cc = [] bcc = [] for recipient in self.recipients: if recipient.condition: if not frappe.safe_eval(recipient.condition, None, context): continue if recipient.receiver_by_document_field: fields = recipient.receiver_by_document_field.split(",") # fields from child table if len(fields) > 1: for d in doc.get(fields[1]): email_id = d.get(fields[0]) if validate_email_address(email_id): recipients.append(email_id) # field from parent doc else: email_ids_value = doc.get(fields[0]) if validate_email_address(email_ids_value): email_ids = email_ids_value.replace(",", "\n") recipients = recipients + email_ids.split("\n") if recipient.cc and "{" in recipient.cc: recipient.cc = frappe.render_template(recipient.cc, context) if recipient.cc: recipient.cc = recipient.cc.replace(",", "\n") cc = cc + recipient.cc.split("\n") if recipient.bcc and "{" in recipient.bcc: recipient.bcc = frappe.render_template(recipient.bcc, context) if recipient.bcc: recipient.bcc = recipient.bcc.replace(",", "\n") bcc = bcc + recipient.bcc.split("\n") # For sending emails to specified role if recipient.receiver_by_role: emails = get_info_based_on_role(recipient.receiver_by_role, "email") for email in emails: recipients = recipients + email.split("\n") if self.send_to_all_assignees: recipients = recipients + get_assignees(doc) return list(set(recipients)), list(set(cc)), list(set(bcc))
def add_subscribers(name, email_list): if not isinstance(email_list, (list, tuple)): email_list = email_list.replace(",", "\n").split("\n") count = 0 for email in email_list: email = email.strip() parsed_email = validate_email_address(email, False) if parsed_email: if not frappe.db.get_value("Email Group Member", {"email_group": name, "email": parsed_email}): frappe.get_doc({ "doctype": "Email Group Member", "email_group": name, "email": parsed_email }).insert(ignore_permissions = frappe.flags.ignore_permissions) count += 1 else: pass else: frappe.msgprint(_("{0} is not a valid Email Address").format(email)) frappe.msgprint(_("{0} subscribers added").format(count)) return frappe.get_doc("Email Group", name).update_total_subscribers()
def partner_login(): data = json.loads(frappe.request.data) if not validate_email_address(data["email"]): return {'invalid email address'} user = frappe.db.get_value("User", filters={"email": data["email"]}) # if data['email'].split('@')[1] in {'dell.com', 'vmware.com', 'rsa.com', 'yahoo.com'}: # return {'invalid email domain'} if user: user = frappe.get_doc('User', data['email']) user.new_password = otp = ''.join(random.sample('0123456789', 5)) user.save(ignore_permissions=True) frappe.sendmail( recipients=data['email'], subject='Registration OTP - Dell Quality Connect:Virtual 2020', template='welcome', args={ 'msg': f'Thank you for registering for Quality Connect:Virtual 2020. Your authorization OTP is {otp}', }) else: user = frappe.get_doc({ "doctype": "User", "email": data['email'], "first_name": data['email'], "enabled": 1, "user_type": "System User", }) user.new_password = otp = ''.join(random.sample('0123456789', 5)) user.flags.no_welcome_mail = True user.flags.ignore_permissions = True user.insert(ignore_permissions=True) user.add_roles("Dell User") dell_user = frappe.get_doc({ 'doctype': 'Dell User', 'email': data['email'], 'first_name': data['email'], 'user_type': 'Partner', 'sing_up_date': now_datetime(), }) dell_user.insert(ignore_permissions=True) frappe.sendmail( recipients=data['email'], subject='Registration OTP - Dell Quality Connect:Virtual 2020', template='welcome', args={ 'msg': f'Thank you for registering for Quality Connect:Virtual 2020. Your authorization OTP is {otp}', }) return {'user inserted'}
def validate(self): """validate the Email Addresses""" from frappe.utils import validate_email_address if not self.sender: self.sender = self.email_account.default_sender validate_email_address(strip(self.sender), True) self.reply_to = validate_email_address(strip(self.reply_to) or self.sender, True) self.replace_sender() self.replace_sender_name() self.recipients = [strip(r) for r in self.recipients] self.cc = [strip(r) for r in self.cc] self.bcc = [strip(r) for r in self.bcc] for e in self.recipients + (self.cc or []) + (self.bcc or []): validate_email_address(e, True)
def set_sender_full_name(self): if not self.sender_full_name and self.sender: if self.sender == "Administrator": self.sender_full_name = frappe.db.get_value( "User", "Administrator", "full_name") self.sender = frappe.db.get_value("User", "Administrator", "email") elif self.sender == "Guest": self.sender_full_name = self.sender self.sender = None else: if self.sent_or_received == 'Sent': validate_email_address(self.sender, throw=True) sender_name, sender_email = parse_addr(self.sender) if sender_name == sender_email: sender_name = None self.sender = sender_email self.sender_full_name = sender_name or frappe.db.exists( "Contact", {"email_id": sender_email}) or sender_email
def confirm_subscription(email): from frappe.utils.verified_command import verify_request if not verify_request(): return # Default user message message = frappe._dict({ "title": _("newsletter:dialog:title:newsletter_subscription"), "html": _('newsletter:dialog:body:error_adding_email_"{0}".').format(email), "primary_label": _("dialog:body:go_to_homepage"), }) group_name = "EGD Subscriptions" if not frappe.db.exists("Email Group", group_name): frappe.get_doc({ "doctype": "Email Group", "title": group_name, }).insert(ignore_permissions=True) from frappe.sessions import get_geo_from_ip country_code = "" if frappe.local.request_ip: geo = get_geo_from_ip(frappe.local.request_ip) if geo and "country" in geo: country_code = geo["country"]["iso_code"] from frappe.utils import validate_email_address email = email.strip() email_valid = validate_email_address(email, False) if email_valid: if not frappe.db.get_value("Email Group Member", { "email_group": group_name, "email": email_valid }): frappe.get_doc({ "doctype": "Email Group Member", "email_group": group_name, "email": email_valid, "country": country_code, "ip": frappe.local.request_ip, }).insert(ignore_permissions=True) frappe.get_doc("Email Group", group_name).update_total_subscribers() frappe.db.commit() message.html = _('newsletter:dialog:body:email_"{0}"_subscribed_ok' ).format(email) else: message.html = _( 'newsletter:dialog:body:email_"{0}"_subscribed_previously' ).format(email) frappe.respond_as_web_page(**message)
def validate_email_id(self): if self.notify_by_email: if self.recipients: email_list = split_emails(self.recipients.replace("\n", "")) from frappe.utils import validate_email_address for email in email_list: if not validate_email_address(email): frappe.throw(_("{0} is an invalid email address in 'Recipients'").format(email)) else: frappe.throw(_("'Recipients' not specified"))
def set_sender_full_name(self): if not self.sender_full_name and self.sender: if self.sender == "Administrator": self.sender_full_name = frappe.db.get_value( "User", "Administrator", "full_name") self.sender = frappe.db.get_value("User", "Administrator", "email") elif self.sender == "Guest": self.sender_full_name = self.sender self.sender = None else: if self.sent_or_received == 'Sent': validate_email_address(self.sender, throw=True) sender_name, sender_email = parse_addr(self.sender) if sender_name == sender_email: sender_name = None self.sender = sender_email self.sender_full_name = sender_name or get_fullname( frappe.session.user ) if frappe.session.user != 'Administrator' else None
def execute(): ''' update/delete the email group member with the wrong email ''' email_group_members = frappe.get_all("Email Group Member", fields=["name", "email"]) for member in email_group_members: validated_email = validate_email_address(member.email) if (validated_email==member.email): pass else: try: frappe.db.set_value("Email Group Member", member.name, "email", validated_email) except Exception: frappe.delete_doc(doctype="Email Group Member", name=member.name, force=1, ignore_permissions=True)
def get_valid_recipients(self): """Get recipients from Email Group""" recipients_list = [] for email_group in get_email_groups(self.name): for d in frappe.db.get_all( "Email Group Member", ["name", "email"], { "unsubscribed": 0, "email_group": email_group.email_group }): if not validate_email_address(d.email): doc_link = '<a href="#Form/Email Group Member/{0}">{1}</a>'.format( d.name, d.email) frappe.msgprint( _('Invalid Email Address {0} in Email Group {1}'). format(doc_link, email_group.email_group)) else: recipients_list.append(d.email) return list(set(recipients_list))
def monthly_updates(): translators = frappe.db.sql_list( "select DISTINCT(contributor) from `tabContributed Translation`") translators = [ email for email in translators if validate_email_address(email) ] message = frappe.get_template( "/templates/emails/translator_update.md").render({"frappe": frappe}) # refer unsubscribe against the administrator # document for test frappe.sendmail(recipients=translators, sender="ERPNext Translator <*****@*****.**>", subject="Montly Update", message=message, reference_doctype="User", reference_name="Administrator")
def validate_email(doc): """Validate Email Addresses of Recipients and CC""" if not (doc.communication_type=="Communication" and doc.communication_medium == "Email") or doc.flags.in_receive: return # validate recipients for email in split_emails(doc.recipients): validate_email_address(email, throw=True) # validate CC for email in split_emails(doc.cc): validate_email_address(email, throw=True) for email in split_emails(doc.bcc): validate_email_address(email, throw=True)
def get_list_of_recipients(self, doc, context): recipients = [] cc = [] bcc = [] for recipient in self.recipients: if recipient.condition: if not frappe.safe_eval(recipient.condition, None, context): continue if recipient.email_by_document_field: email_ids_value = doc.get(recipient.email_by_document_field) if validate_email_address(email_ids_value): email_ids = email_ids_value.replace(",", "\n") recipients = recipients + email_ids.split("\n") # else: # print "invalid email" if recipient.cc and "{" in recipient.cc: recipient.cc = frappe.render_template(recipient.cc, context) if recipient.cc: recipient.cc = recipient.cc.replace(",", "\n") cc = cc + recipient.cc.split("\n") if recipient.bcc and "{" in recipient.bcc: recipient.bcc = frappe.render_template(recipient.bcc, context) if recipient.bcc: recipient.bcc = recipient.bcc.replace(",", "\n") bcc = bcc + recipient.bcc.split("\n") #For sending emails to specified role if recipient.email_by_role: emails = get_emails_from_role(recipient.email_by_role) for email in emails: recipients = recipients + email.split("\n") if not recipients and not cc and not bcc: return None, None, None return list(set(recipients)), list(set(cc)), list(set(bcc))
def test_valid_email(self): # Edge cases self.assertFalse(validate_email_address('')) self.assertFalse(validate_email_address(None)) # Valid addresses self.assertTrue(validate_email_address('*****@*****.**')) self.assertTrue( validate_email_address('[email protected], [email protected]')) # Invalid address self.assertFalse(validate_email_address('someone')) self.assertFalse(validate_email_address('*****@*****.**')) # Invalid with throw self.assertRaises(frappe.InvalidEmailAddressError, validate_email_address, 'someone.com', throw=True)
def validate_email(self): if self.company_email: validate_email_address(self.company_email, True) if self.personal_email: validate_email_address(self.personal_email, True)
def validate_email_type(self, email): from frappe.utils import validate_email_address validate_email_address(email.strip(), True)
def validate(self): self.route = "newsletters/" + self.name if self.send_from: validate_email_address(self.send_from, True)
def validate(self): validate_email_address(self.email_address) validate_phone_number(self.phone)
def validate(self): from frappe.utils import validate_email_address if self.email: validate_email_address(self.email.strip(), True)