def create_message_with_attchments_local_files(cls, sender, to, cc, bcc, subject, message_html): message = MIMEMultipart() message['to'] = to message['cc'] = cc message['bcc'] = bcc message['from'] = sender message['subject'] = subject message_html += '<p>Sent from my <a href="http://www.iogrow.com/">ioGrow account </a></p>' msg = MIMEText(smart_str(message_html), 'html') message.attach(msg) path = os.path.join('static/src/img/mail_images', 'sm-iogrow-true.png') content_type, encoding = mimetypes.guess_type(path) path2 = os.path.join('static/src/img/mail_images', 'Logo-iogrow.png') content_type2, encoding2 = mimetypes.guess_type(path2) main_type, sub_type = content_type.split('/', 1) main_type2, sub_type2 = content_type2.split('/', 1) if main_type == 'image': fp = open(path, 'r') msg = MIMEImage(fp.read(), _subtype=sub_type) fp.close() if main_type2 == 'image': fp2 = open(path2, 'rb') msg2 = MIMEImage(fp2.read(), _subtype=sub_type) fp2.close() msg.add_header('Content-Disposition', 'attachment', filename="logo") msg.add_header("Content-ID", "<logo_cid>") message.attach(msg) msg2.add_header('Content-Disposition', 'attachment', filename="user") msg2.add_header("Content-ID", "<user_cid>") message.attach(msg2) return {'raw': base64.urlsafe_b64encode(message.as_string())}
def list_google_contacts(cls, credentials): auth_token = OAuth2TokenFromCredentials(credentials) gd_client = ContactsClient() auth_token.authorize(gd_client) query = gdata.contacts.client.ContactsQuery() query.max_results = 10 feed = gd_client.GetContacts(q=query) for i, entry in enumerate(feed.entry): if entry.name: print '\n%s %s' % (i + 1, smart_str(entry.name.full_name.text)) try: contact_image = gd_client.GetPhoto(entry) except: print 'ERROR: not found'
def create_message_with_attachments(cls, user, sender, to, cc, bcc, subject, message_html, files): """Create a message for an email. Args: sender: Email address of the sender. to: Email address of the receiver. subject: The subject of the email message. message_text: The text of the email message. file_dir: The directory containing the file to be attached. filename: The name of the file to be attached. Returns: An object containing a base64 encoded email object. """ message = MIMEMultipart() message['to'] = to message['cc'] = cc message['bcc'] = bcc message['from'] = sender message['subject'] = subject message_html += '<p>Sent from my <a href="http://www.iogrow.com/">ioGrow account </a></p>' msg = MIMEText(smart_str(message_html), 'html') message.attach(msg) for file_id in files: file_data = cls.get_file_from_drive(user, file_id) content_type = file_data['content_type'] if content_type is None: content_type = 'application/octet-stream' main_type, sub_type = content_type.split('/', 1) if main_type == 'text': msg = MIMEText(file_data['content'], _subtype=sub_type) elif main_type == 'image': msg = MIMEImage(file_data['content'], _subtype=sub_type) elif main_type == 'audio': msg = MIMEAudio(file_data['content'], _subtype=sub_type) else: msg = MIMEBase(main_type, sub_type) msg.set_payload(file_data['content']) msg.add_header('Content-Disposition', 'attachment', filename=file_data['file_name']) message.attach(msg) return {'raw': base64.urlsafe_b64encode(message.as_string())}
def update_edge_indexes(cls, parent_key, kind, indexed_edge): try: parent = parent_key.get() if parent: empty_string = lambda x: smart_str(x) if x else "" search_index = search.Index(name="GlobalIndex") search_document = search_index.get(str(parent_key.id())) data = {'id': parent_key.id()} if search_document: for e in search_document.fields: if e.name == kind: if isinstance(indexed_edge, basestring): indexed_edge = '%s %s' % (empty_string(e.value), indexed_edge) else: indexed_edge = '%s %s' % (empty_string(e.value), str(indexed_edge)) data[e.name] = e.value data[kind] = indexed_edge parent.put_index(data) except: print 'ERROR: an error had occurred when updating edges index'
def create_message(cls, sender, to, cc, bcc, subject, message_html): """Create a message for an email. Args: sender: Email address of the sender. to: Email address of the receiver. subject: The subject of the email message. message_text: The text of the email message. Returns: An object containing a base64 encoded email object. """ message_html += '<p>Sent from my <a href="http://www.iogrow.com/">ioGrow account </a></p>' message = MIMEText(smart_str(message_html), 'html') message['to'] = to message['cc'] = cc message['bcc'] = bcc message['from'] = sender message['subject'] = subject return {'raw': base64.urlsafe_b64encode(message.as_string())}
def update_edge_indexes(cls, parent_key, kind, indexed_edge): try: parent = parent_key.get() if parent: empty_string = lambda x: smart_str(x) if x else "" search_index = search.Index(name="GlobalIndex") search_document = search_index.get(str(parent_key.id())) data = {'id': parent_key.id()} if search_document: for e in search_document.fields: if e.name == kind: if isinstance(indexed_edge, basestring): indexed_edge = '%s %s' % (empty_string( e.value), indexed_edge) else: indexed_edge = '%s %s' % (empty_string( e.value), str(indexed_edge)) data[e.name] = e.value data[kind] = indexed_edge parent.put_index(data) except: print 'ERROR: an error had occurred when updating edges index'
def receive(self, mail_message): sender_id = mail_message.to.split("@")[0] try: bcc = mail_message.bcc.split("@")[0] except: bcc = 'undefined' user_id = 'undefined' if sender_id.isdigit(): user_id = sender_id if bcc.isdigit(): user_id = bcc user = User.get_by_gid(user_id) if user: sender_email = re.findall(r'[\w\.-]+@[\w\.-]+', mail_message.sender) if user.email == sender_email[0]: print 'authorized' html_bodies = mail_message.bodies('text/html') email_body = '' additional_emails = ' ' for content_type, body in html_bodies: decoded_html = smart_str(body.decode()) email_body += decoded_html additional_emails += ' ' + smart_str(mail_message.to) try: additional_emails += ' ' + smart_str(mail_message.bcc) except: pass try: additional_emails += ' ' + smart_str(mail_message.cc) except: pass re_emails = re.findall(r'[\w\.-]+@[\w\.-]+', email_body + additional_emails) emails = list(set(re_emails)) print 'emails' print emails for email in emails: generic_prop = ndb.GenericProperty() generic_prop._name = 'email' nodes = Node.query(generic_prop == email).fetch() if len(nodes) > 0: for node in nodes: parents_edge_list = Edge.list(start_node=node.key, kind='parents') for edge in parents_edge_list['items']: parent_key = edge.end_node # insert a note related to the parent node note_author = Userinfo() note_author.display_name = user.google_display_name note_author.photo = user.google_public_profile_photo_url note = Note(owner=user.google_user_id, organization=user.organization, author=note_author, title=mail_message.subject, content=email_body) entityKey_async = note.put_async() entityKey = entityKey_async.get_result() Edge.insert(start_node=parent_key, end_node=entityKey, kind='topics', inverse_edge='parents') EndpointsHelper.update_edge_indexes( parent_key=entityKey, kind='topics', indexed_edge=str(parent_key.id())) else: pass # We should create a lead related to this email # and attach this email with this lead else: print 'not authorized' else: print 'user doesnt exist'
def receive(self, mail_message): sender_id = mail_message.to.split("@")[0] try: bcc = mail_message.bcc.split("@")[0] except: bcc = 'undefined' user_id = 'undefined' if sender_id.isdigit(): user_id=sender_id if bcc.isdigit(): user_id=bcc user = User.get_by_gid(user_id) if user: sender_email=re.findall(r'[\w\.-]+@[\w\.-]+', mail_message.sender) if user.email==sender_email[0]: print 'authorized' html_bodies = mail_message.bodies('text/html') email_body = '' additional_emails = ' ' for content_type, body in html_bodies: decoded_html = smart_str(body.decode()) email_body+=decoded_html additional_emails+=' ' + smart_str(mail_message.to) try: additional_emails+=' ' + smart_str(mail_message.bcc) except: pass try: additional_emails+=' ' + smart_str(mail_message.cc) except: pass re_emails = re.findall(r'[\w\.-]+@[\w\.-]+', email_body + additional_emails) emails = list(set(re_emails)) print 'emails' print emails for email in emails: generic_prop = ndb.GenericProperty() generic_prop._name = 'email' nodes = Node.query(generic_prop==email).fetch() if len(nodes)>0: for node in nodes: parents_edge_list = Edge.list( start_node = node.key, kind = 'parents' ) for edge in parents_edge_list['items']: parent_key = edge.end_node # insert a note related to the parent node note_author = Userinfo() note_author.display_name = user.google_display_name note_author.photo = user.google_public_profile_photo_url note = Note( owner = user.google_user_id, organization = user.organization, author = note_author, title = mail_message.subject, content = email_body ) entityKey_async = note.put_async() entityKey = entityKey_async.get_result() Edge.insert( start_node = parent_key, end_node = entityKey, kind = 'topics', inverse_edge = 'parents' ) EndpointsHelper.update_edge_indexes( parent_key=entityKey, kind='topics', indexed_edge=str(parent_key.id()) ) else: pass # We should create a lead related to this email # and attach this email with this lead else: print 'not authorized' else: print 'user doesnt exist'