'Extract Email Addresses from given doctype and add them to the current list'

def import_from(self, doctype):

    meta = frappe.get_meta(doctype)
    email_field = [d.fieldname for d in meta.fields if ((d.fieldtype in (u'Data', u'Small    Text', u'Text', u'Code')) and (d.options == u'Email'))][0]
    unsubscribed_field = (u'unsubscribed' if meta.get_field(u'unsubscribed') else None)
    added = 0
    for user in frappe.db.get_all(doctype, [email_field, (unsubscribed_field or u'name')]):
        try:
            email = (parse_addr(user.get(email_field))[1] if user.get(email_field) else None)
            if email:
                frappe.get_doc({u'doctype': u'Email    Group    Member', u'email_group': self.name, u'email': email, u'unsubscribed': (user.get(unsubscribed_field) if unsubscribed_field else 0)}).insert(ignore_permissions=True)
                added += 1
        except frappe.UniqueValidationError:
            pass
    frappe.msgprint(_(u'{0}    subscribers    added').format(added))
    return self.update_total_subscribers()
