def send_notification_mail(self): """ Send a notification mail to sales department and thank you email to lead whenever new opportunity is created. """ Config = Pool().get('sale.configuration') sale_config = Config(1) # Prepare the content for email for lead lead_receivers = [self.party.email] lead_message = render_email( from_email=sale_config.sale_opportunity_email, to=', '.join(lead_receivers), subject="Thank You for your query", text_template='crm/emails/lead_thank_you_mail.jinja', lead=self ) # Prepare the content for email for sale department sale_subject = "[Openlabs CRM] New lead created by %s" % \ (self.party.name) sale_receivers = [ member.email for member in self.company.sales_team if member.email ] sender = config.get('email', 'from') sale_message = render_email( from_email=sender, to=', '.join(sale_receivers), subject=sale_subject, text_template='crm/emails/sale_notification_text.jinja', lead=self ) # Send mail. server = get_smtp_server() if sale_receivers: # Send to sale department server.sendmail( sender, sale_receivers, sale_message.as_string() ) if lead_receivers: # Send to lead server.sendmail( sender, lead_receivers, lead_message.as_string() ) server.quit()
def test_0070_render_email_unicode(self): ''' Render email with unicode in Subject, From and To. ''' with Transaction().start(DB_NAME, USER, CONTEXT): self.setup_defaults() app = self.get_app() sender = u'Cédric Krier <*****@*****.**>' with app.test_request_context('/'): email_message = render_email(sender, u'*****@*****.**', u'Dummy subject øf email', text_template='from-local.html', cc=u'*****@*****.**') self.assertEqual( decode_header(email_message['From'])[0], (sender.encode('UTF-8'), 'utf-8')) self.assertEqual( decode_header(email_message['Subject'])[0], (u'Dummy subject øf email'.encode('UTF-8'), 'utf-8')) self.assertEqual( decode_header(email_message['To'])[0], (u'*****@*****.**'.encode('UTF-8'), None))
def test_0070_render_email_unicode(self): ''' Render email with unicode in Subject, From and To. ''' with Transaction().start(DB_NAME, USER, CONTEXT): self.setup_defaults() app = self.get_app() sender = u'Cédric Krier <*****@*****.**>' with app.test_request_context('/'): email_message = render_email( sender, u'*****@*****.**', u'Dummy subject øf email', text_template='from-local.html', cc=u'*****@*****.**' ) self.assertEqual( decode_header(email_message['From'])[0], (sender.encode('UTF-8'), 'utf-8') ) self.assertEqual( decode_header(email_message['Subject'])[0], (u'Dummy subject øf email'.encode('UTF-8'), 'utf-8') ) self.assertEqual( decode_header(email_message['To'])[0], (u'*****@*****.**'.encode('UTF-8'), None) )
def send_mail(self, receivers=None): """Send mail when task created. :param receivers: Receivers of email. """ EmailQueue = Pool().get("email.queue") subject = "[#%s %s] - %s" % (self.id, self.parent.rec_name, self.rec_name) if not receivers: receivers = [p.email for p in self.participants if p.email] if self.created_by.email in receivers: receivers.remove(self.created_by.email) if not receivers: return message = render_email( from_email=CONFIG["smtp_from"], to=", ".join(receivers), subject=subject, text_template="project/emails/project_text_content.jinja", html_template="project/emails/project_html_content.jinja", task=self, updated_by=request.nereid_user.display_name, ) # Send mail. EmailQueue.queue_mail(CONFIG["smtp_from"], receivers, message.as_string())
def test_0090_render_email_html_only(self): ''' Render email html part alone ''' with Transaction().start(DB_NAME, USER, CONTEXT): self.setup_defaults() app = self.get_app() sender = u'Sender <*****@*****.**>' with app.test_request_context('/'): email_message = render_email( sender, u'*****@*****.**', u'Dummy subject of email', html_template='from-local.html', ) self.assertEqual( decode_header(email_message['Subject'])[0], ('Dummy subject of email', None) ) # Message type should be text/html self.assertFalse(email_message.is_multipart()) self.assertEqual( email_message.get_content_type(), 'text/html' )
def send_mail(self, receivers=None): """Send mail when task created. :param receivers: Receivers of email. """ EmailQueue = Pool().get('email.queue') subject = "[#%s %s] - %s" % (self.id, self.parent.rec_name, self.rec_name) if not receivers: receivers = [p.email for p in self.participants if p.email] if self.created_by.email in receivers: receivers.remove(self.created_by.email) if not receivers: return message = render_email( from_email=CONFIG['smtp_from'], to=', '.join(receivers), subject=subject, text_template='project/emails/project_text_content.jinja', html_template='project/emails/project_html_content.jinja', task=self, updated_by=request.nereid_user.display_name) # Send mail. EmailQueue.queue_mail(CONFIG['smtp_from'], receivers, message.as_string())
def test_0080_render_email_text_only(self): ''' Render email text part alone ''' self.setup_defaults() app = self.get_app() sender = u'Sender <*****@*****.**>' with app.test_request_context('/'): email_message = render_email( sender, u'*****@*****.**', u'Dummy subject of email', text_template='from-local.html', ) self.assertEqual( decode_header(email_message['Subject'])[0], ('Dummy subject of email', None) ) # Message type should be text/plain self.assertFalse(email_message.is_multipart()) self.assertEqual( email_message.get_content_type(), 'text/plain' )
def test_0100_render_email_text_n_html_only(self): ''' Render email text and html parts, but no attachments ''' self.setup_defaults() app = self.get_app() sender = u'Sender <*****@*****.**>' with app.test_request_context('/'): email_message = render_email( sender, u'*****@*****.**', u'Dummy subject of email', text_template='from-local.html', html_template='from-local.html', ) self.assertEqual( decode_header(email_message['Subject'])[0], ('Dummy subject of email', None)) # Message type should be multipart/alternative self.assertTrue(email_message.is_multipart()) self.assertEqual(email_message.get_content_type(), 'multipart/alternative') # Ensure that there are two subparts self.assertEqual(len(email_message.get_payload()), 2) # Ensure that the subparts are 1 text and html part payload_types = set( [p.get_content_type() for p in email_message.get_payload()]) self.assertEqual(set(['text/plain', 'text/html']), payload_types)
def send_notification_mail(self): """ Send a notification mail to sales department and thank you email to lead whenever new opportunity is created. """ Config = Pool().get('sale.configuration') config = Config(1) # Prepare the content for email for lead lead_receivers = [self.party.email] lead_message = render_email( from_email=config.sale_opportunity_email, to=', '.join(lead_receivers), subject="Thank You for your query", text_template='crm/emails/lead_thank_you_mail.jinja', lead=self) # Prepare the content for email for sale department sale_subject = "[Openlabs CRM] New lead created by %s" % \ (self.party.name) sale_receivers = [ member.email for member in self.company.sales_team if member.email ] sale_message = render_email( from_email=CONFIG['smtp_from'], to=', '.join(sale_receivers), subject=sale_subject, text_template='crm/emails/sale_notification_text.jinja', lead=self) # Send mail. server = get_smtp_server() if sale_receivers: # Send to sale department server.sendmail(CONFIG['smtp_from'], sale_receivers, sale_message.as_string()) if lead_receivers: # Send to lead server.sendmail(CONFIG['smtp_from'], lead_receivers, lead_message.as_string()) server.quit()
def test_0110_email_with_attachments(self): ''' Send an email with text, html and an attachment ''' self.setup_defaults() app = self.get_app() sender = u'Sender <*****@*****.**>' with app.test_request_context('/'): email_message = render_email( sender, u'*****@*****.**', u'Dummy subject of email', text_template='from-local.html', html_template='from-local.html', attachments={'filename.pdf': 'my glorious PDF content'}, ) self.assertEqual( decode_header(email_message['Subject'])[0], ('Dummy subject of email', None)) # Message type should be multipart/alternative self.assertTrue(email_message.is_multipart()) self.assertEqual(email_message.get_content_type(), 'multipart/mixed') # Ensure that there are two subparts self.assertEqual(len(email_message.get_payload()), 2) # Ensure that the subparts are 1 alternative and # octet-stream part payload_types = set( [p.get_content_type() for p in email_message.get_payload()]) self.assertEqual( set(['multipart/alternative', 'application/octet-stream']), payload_types) # Drill into the alternative part and ensure that there is # both the text part and html part in it. for part in email_message.get_payload(): if part.get_content_type() == 'multipart/alternative': # Ensure that there are two subparts # 1. text/plain # 2. text/html self.assertEqual(len(email_message.get_payload()), 2) payload_types = set( [p.get_content_type() for p in part.get_payload()]) self.assertEqual(set(['text/plain', 'text/html']), payload_types) break else: self.fail('Alternative part not found')
def send_gift_card_as_email(self): """ Send gift card as an attachment in the email """ EmailQueue = Pool().get('email.queue') GiftCardReport = Pool().get('gift_card.gift_card', type='report') ModelData = Pool().get('ir.model.data') Group = Pool().get('res.group') group_id = ModelData.get_id( "gift_card", "gift_card_email_receivers" ) bcc_emails = map( lambda user: user.email, filter(lambda user: user.email, Group(group_id).users) ) if not self.recipient_email: # pragma: no cover return # Try to generate report twice # This is needed as sometimes `unoconv` fails to convert report to pdf for try_count in range(2): try: val = GiftCardReport.execute([self.id], {}) break except: # pragma: no cover if try_count == 0: continue else: return subject = self._get_subject_for_email() html_template, text_template = self._get_email_templates() sender = config.get('email', 'from') email_gift_card = render_email( sender, self.recipient_email, subject, html_template=html_template, text_template=text_template, attachments={"%s.%s" % (val[3], val[0]): val[1]}, card=self, ) EmailQueue.queue_mail( sender, [self.recipient_email] + bcc_emails, email_gift_card.as_string() ) self.is_email_sent = True self.save()
def send_gift_card_as_email(self): """ Send gift card as an attachment in the email """ EmailQueue = Pool().get('email.queue') GiftCardReport = Pool().get('gift_card.gift_card', type='report') ModelData = Pool().get('ir.model.data') Group = Pool().get('res.group') group_id = ModelData.get_id("gift_card", "gift_card_email_receivers") bcc_emails = map( lambda user: user.email, filter(lambda user: user.email, Group(group_id).users)) if not self.recipient_email: # pragma: no cover return # Try to generate report twice # This is needed as sometimes `unoconv` fails to convert report to pdf for try_count in range(2): try: val = GiftCardReport.execute([self.id], {}) break except: # pragma: no cover if try_count == 0: continue else: return subject = self._get_subject_for_email() html_template, text_template = self._get_email_templates() sender = config.get('email', 'from') email_gift_card = render_email( sender, self.recipient_email, subject, html_template=html_template, text_template=text_template, attachments={"%s.%s" % (val[3], val[0]): val[1]}, card=self, ) EmailQueue.queue_mail(sender, [self.recipient_email] + bcc_emails, email_gift_card.as_string()) self.is_email_sent = True self.save()
def test_0060_render_email(self): ''' Render Email with template from local searchpath ''' self.setup_defaults() app = self.get_app() sender = u'Sender <*****@*****.**>' with app.test_request_context('/'): email_message = render_email(sender, u'*****@*****.**', u'Dummy subject of email', text_template='from-local.html', cc=u'*****@*****.**') self.assertEqual( decode_header(email_message['From'])[0], (sender, None)) self.assertEqual( decode_header(email_message['Subject'])[0], ('Dummy subject of email', None))
def test_0060_render_email(self): ''' Render Email with template from local searchpath ''' with Transaction().start(DB_NAME, USER, CONTEXT): self.setup_defaults() app = self.get_app() with app.test_request_context('/'): email_message = unicode(render_email( '*****@*****.**', '*****@*****.**', 'Dummy subject of email', text_template='from-local.html', cc='*****@*****.**' )) self.assertTrue( 'From: [email protected]' in email_message ) self.assertTrue( 'Subject: Dummy subject of email' in email_message )
def test_0100_render_email_text_n_html_only(self): ''' Render email text and html parts, but no attachments ''' with Transaction().start(DB_NAME, USER, CONTEXT): self.setup_defaults() app = self.get_app() sender = u'Sender <*****@*****.**>' with app.test_request_context('/'): email_message = render_email( sender, u'*****@*****.**', u'Dummy subject of email', text_template='from-local.html', html_template='from-local.html', ) self.assertEqual( decode_header(email_message['Subject'])[0], ('Dummy subject of email', None) ) # Message type should be multipart/alternative self.assertTrue(email_message.is_multipart()) self.assertEqual( email_message.get_content_type(), 'multipart/alternative' ) # Ensure that there are two subparts self.assertEqual( len(email_message.get_payload()), 2 ) # Ensure that the subparts are 1 text and html part payload_types = set([ p.get_content_type() for p in email_message.get_payload() ]) self.assertEqual( set(['text/plain', 'text/html']), payload_types )
def test_0060_render_email(self): ''' Render Email with template from local searchpath ''' self.setup_defaults() app = self.get_app() sender = u'Sender <*****@*****.**>' with app.test_request_context('/'): email_message = render_email( sender, u'*****@*****.**', u'Dummy subject of email', text_template='from-local.html', cc=u'*****@*****.**' ) self.assertEqual( decode_header(email_message['From'])[0], (sender, None) ) self.assertEqual( decode_header(email_message['Subject'])[0], ('Dummy subject of email', None) )
def test_0080_render_email_text_only(self): ''' Render email text part alone ''' self.setup_defaults() app = self.get_app() sender = u'Sender <*****@*****.**>' with app.test_request_context('/'): email_message = render_email( sender, u'*****@*****.**', u'Dummy subject of email', text_template='from-local.html', ) self.assertEqual( decode_header(email_message['Subject'])[0], ('Dummy subject of email', None)) # Message type should be text/plain self.assertFalse(email_message.is_multipart()) self.assertEqual(email_message.get_content_type(), 'text/plain')
def invitation_new_user_handler(nereid_user_id): """When the invite is sent to a new user, he is sent an invitation key with the url which guides the user to registration page This method checks if the invitation key is present in the url If yes, search for the invitation with this key, attache the user to the invitation and project to the user If not, perform normal operation """ try: Invitation = Pool().get('project.work.invitation') Project = Pool().get('project.work') NereidUser = Pool().get('nereid.user') Activity = Pool().get('nereid.activity') except KeyError: # Just return silently. This KeyError is cause if the module is not # installed for a specific database but exists in the python path # and is loaded by the tryton module loader warnings.warn( "nereid-project module installed but not in database", DeprecationWarning, stacklevel=2 ) return invitation_code = request.args.get('invitation_code') if not invitation_code: return invitation, = Invitation.search([ ('invitation_code', '=', invitation_code) ], limit=1) if not invitation: return Invitation.write([invitation], { 'nereid_user': nereid_user_id, 'invitation_code': None }) nereid_user = NereidUser(nereid_user_id) subject = '[%s] %s Accepted the invitation to join the project' \ % (invitation.project.rec_name, nereid_user.display_name) receivers = [ m.email for m in invitation.project.members if m.user.email and m.role == 'admin' ] email_message = render_email( text_template='project/emails/invite_2_project_accepted_text.html', subject=subject, to=', '.join(receivers), from_email=CONFIG['smtp_from'], invitation=invitation ) server = get_smtp_server() server.sendmail(CONFIG['smtp_from'], receivers, email_message.as_string()) server.quit() Project.write( [invitation.project], { 'members': [ ('create', [{ 'user': [nereid_user_id] }]) ] } ) Activity.create([{ 'actor': nereid_user_id, 'object_': 'project.work, %d' % invitation.project.id, 'verb': 'joined_project', 'project': invitation.project.id, }])
def invitation_new_user_handler(nereid_user_id): """When the invite is sent to a new user, he is sent an invitation key with the url which guides the user to registration page This method checks if the invitation key is present in the url If yes, search for the invitation with this key, attache the user to the invitation and project to the user If not, perform normal operation """ EmailQueue = Pool().get("email.queue") try: Invitation = Pool().get("project.work.invitation") Project = Pool().get("project.work") NereidUser = Pool().get("nereid.user") Activity = Pool().get("nereid.activity") except KeyError: # Just return silently. This KeyError is cause if the module is not # installed for a specific database but exists in the python path # and is loaded by the tryton module loader warnings.warn("nereid-project module installed but not in database", DeprecationWarning, stacklevel=2) return invitation_code = request.args.get("invitation_code") if not invitation_code: return invitation, = Invitation.search([("invitation_code", "=", invitation_code)], limit=1) if not invitation: return Invitation.write([invitation], {"nereid_user": nereid_user_id, "invitation_code": None}) nereid_user = NereidUser(nereid_user_id) subject = "[%s] %s Accepted the invitation to join the project" % ( invitation.project.rec_name, nereid_user.display_name, ) receivers = [m.user.email for m in invitation.project.members if m.user.email and m.role == "admin"] sender = config.get("email", "from") email_message = render_email( text_template="project/emails/invite_2_project_accepted_text.html", subject=subject, to=", ".join(receivers), from_email=sender, invitation=invitation, ) EmailQueue.queue_mail(sender, receivers, email_message.as_string()) Project.write([invitation.project], {"members": [("create", [{"user": nereid_user_id}])]}) Activity.create( [ { "actor": nereid_user_id, "object_": "project.work, %d" % invitation.project.id, "verb": "joined_project", "project": invitation.project.id, } ] )
def invitation_new_user_handler(nereid_user_id): """When the invite is sent to a new user, he is sent an invitation key with the url which guides the user to registration page This method checks if the invitation key is present in the url If yes, search for the invitation with this key, attache the user to the invitation and project to the user If not, perform normal operation """ EmailQueue = Pool().get('email.queue') try: Invitation = Pool().get('project.work.invitation') Project = Pool().get('project.work') NereidUser = Pool().get('nereid.user') Activity = Pool().get('nereid.activity') except KeyError: # Just return silently. This KeyError is cause if the module is not # installed for a specific database but exists in the python path # and is loaded by the tryton module loader warnings.warn("nereid-project module installed but not in database", DeprecationWarning, stacklevel=2) return invitation_code = request.args.get('invitation_code') if not invitation_code: return invitation, = Invitation.search( [('invitation_code', '=', invitation_code)], limit=1) if not invitation: return Invitation.write([invitation], { 'nereid_user': nereid_user_id, 'invitation_code': None }) nereid_user = NereidUser(nereid_user_id) subject = '[%s] %s Accepted the invitation to join the project' \ % (invitation.project.rec_name, nereid_user.display_name) receivers = [ m.user.email for m in invitation.project.members if m.user.email and m.role == 'admin' ] email_message = render_email( text_template='project/emails/invite_2_project_accepted_text.html', subject=subject, to=', '.join(receivers), from_email=CONFIG['smtp_from'], invitation=invitation) EmailQueue.queue_mail(CONFIG['smtp_from'], receivers, email_message.as_string()) Project.write([invitation.project], {'members': [('create', [{ 'user': nereid_user_id }])]}) Activity.create([{ 'actor': nereid_user_id, 'object_': 'project.work, %d' % invitation.project.id, 'verb': 'joined_project', 'project': invitation.project.id, }])
def test_0110_email_with_attachments(self): ''' Send an email with text, html and an attachment ''' with Transaction().start(DB_NAME, USER, CONTEXT): self.setup_defaults() app = self.get_app() sender = u'Sender <*****@*****.**>' with app.test_request_context('/'): email_message = render_email( sender, u'*****@*****.**', u'Dummy subject of email', text_template='from-local.html', html_template='from-local.html', attachments={'filename.pdf': 'my glorious PDF content'}, ) self.assertEqual( decode_header(email_message['Subject'])[0], ('Dummy subject of email', None) ) # Message type should be multipart/alternative self.assertTrue(email_message.is_multipart()) self.assertEqual( email_message.get_content_type(), 'multipart/mixed' ) # Ensure that there are two subparts self.assertEqual( len(email_message.get_payload()), 2 ) # Ensure that the subparts are 1 alternative and # octet-stream part payload_types = set([ p.get_content_type() for p in email_message.get_payload() ]) self.assertEqual( set(['multipart/alternative', 'application/octet-stream']), payload_types ) # Drill into the alternative part and ensure that there is # both the text part and html part in it. for part in email_message.get_payload(): if part.get_content_type() == 'multipart/alternative': # Ensure that there are two subparts # 1. text/plain # 2. text/html self.assertEqual( len(email_message.get_payload()), 2 ) payload_types = set([ p.get_content_type() for p in part.get_payload() ]) self.assertEqual( set(['text/plain', 'text/html']), payload_types ) break else: self.fail('Alternative part not found')