def test_send(self): m = Mail() m.add_to('John, Doe <*****@*****.**>') m.set_subject('test') m.set_html('WIN') m.set_text('WIN') m.set_from('*****@*****.**') m.set_asm_group_id(42) m.add_cc('*****@*****.**') m.add_bcc('*****@*****.**') m.add_substitution('subKey', 'subValue') m.add_section('testSection', 'sectionValue') m.add_category('testCategory') m.add_unique_arg('testUnique', 'uniqueValue') m.add_filter('testFilter', 'filter', 'filterValue') m.add_attachment_stream('testFile', 'fileValue') url = self.sg._build_body(m) url.pop('api_key', None) url.pop('api_user', None) url.pop('date', None) test_url = json.loads(''' { "to[]": ["*****@*****.**"], "toname[]": ["John Doe"], "html": "WIN", "text": "WIN", "subject": "test", "files[testFile]": "fileValue", "from": "*****@*****.**", "cc[]": ["*****@*****.**"], "bcc[]": ["*****@*****.**"] } ''') test_url['x-smtpapi'] = json.dumps(json.loads(''' { "sub": { "subKey": ["subValue"] }, "section": { "testSection":"sectionValue" }, "category": ["testCategory"], "unique_args": { "testUnique":"uniqueValue" }, "filters": { "testFilter": { "settings": { "filter": "filterValue" } } }, "asm_group_id": 42 } ''')) try: self.assertItemsEqual(url, test_url) except: # Python 3+ self.assertCountEqual(url, test_url)
def test_send(self): m = Mail() m.add_to('John, Doe <*****@*****.**>') m.set_subject('test') m.set_html('WIN') m.set_text('WIN') m.set_from('*****@*****.**') m.add_substitution('subKey', 'subValue') m.add_section('testSection', 'sectionValue') m.add_category('testCategory') m.add_unique_arg('testUnique', 'uniqueValue') m.add_filter('testFilter', 'filter', 'filterValue') m.add_attachment_stream('testFile', 'fileValue') self.sg.send(m) url = self.sg._build_body(m) url.pop('api_key', None) url.pop('api_user', None) url.pop('date', None) testUrl = json.loads('''{"to[]": ["*****@*****.**"], "toname[]": ["John Doe"], "html": "WIN", "text": "WIN", "subject": "test", "files[testFile]": "fileValue", "from": "*****@*****.**", "headers": "", "fromname": "", "replyto": ""}''') testUrl['x-smtpapi'] = json.dumps(json.loads('''{"sub":{"subKey":["subValue"]}, "section":{"testSection":"sectionValue"}, "category":["testCategory"], "unique_args":{"testUnique":"uniqueValue"}, "filters":{"testFilter":{"settings":{"filter":"filterValue"}}}}''')) self.assertEqual(url, testUrl)
def test_send(self): m = Mail() m.add_to('John, Doe <*****@*****.**>') m.set_subject('test') m.set_html('WIN') m.set_text('WIN') m.set_from('*****@*****.**') m.set_asm_group_id(42) m.add_cc('*****@*****.**') m.add_bcc('*****@*****.**') m.add_substitution('subKey', 'subValue') m.add_section('testSection', 'sectionValue') m.add_category('testCategory') m.add_unique_arg('testUnique', 'uniqueValue') m.add_filter('testFilter', 'filter', 'filterValue') m.add_attachment_stream('testFile', 'fileValue') url = self.sg._build_body(m) url.pop('api_key', None) url.pop('api_user', None) url.pop('date', None) test_url = json.loads(''' { "to[]": ["*****@*****.**"], "toname[]": ["John Doe"], "html": "WIN", "text": "WIN", "subject": "test", "files[testFile]": "fileValue", "from": "*****@*****.**", "cc[]": ["*****@*****.**"], "bcc[]": ["*****@*****.**"] } ''') test_url['x-smtpapi'] = json.dumps( json.loads(''' { "sub": { "subKey": ["subValue"] }, "section": { "testSection":"sectionValue" }, "category": ["testCategory"], "unique_args": { "testUnique":"uniqueValue" }, "filters": { "testFilter": { "settings": { "filter": "filterValue" } } }, "asm_group_id": 42 } ''')) self.assertEqual(url, test_url)
def test_send(self): m = Mail() m.add_to("John, Doe <*****@*****.**>") m.set_subject("test") m.set_html("WIN") m.set_text("WIN") m.set_from("*****@*****.**") m.add_substitution("subKey", "subValue") m.add_section("testSection", "sectionValue") m.add_category("testCategory") m.add_unique_arg("testUnique", "uniqueValue") m.add_filter("testFilter", "filter", "filterValue") m.add_attachment_stream("testFile", "fileValue") url = self.sg._build_body(m) url.pop("api_key", None) url.pop("api_user", None) url.pop("date", None) test_url = json.loads( """ { "to[]": ["*****@*****.**"], "toname[]": ["John Doe"], "html": "WIN", "text": "WIN", "subject": "test", "files[testFile]": "fileValue", "from": "*****@*****.**" } """ ) test_url["x-smtpapi"] = json.dumps( json.loads( """ { "to" : ["John, Doe <*****@*****.**>"], "sub": { "subKey": ["subValue"] }, "section": { "testSection":"sectionValue" }, "category": ["testCategory"], "unique_args": { "testUnique":"uniqueValue" }, "filters": { "testFilter": { "settings": { "filter": "filterValue" } } } } """ ) ) self.assertEqual(url, test_url)
def send_email_verification(user_id, resend=False): if not Configuration.PUSH_ENABLED: return sendgrid = create_sendgrid() user = User.find(user_id) verify_code = wigo_db.get_new_code({ 'type': 'verify_email', 'user_id': user_id, 'email': user.email }) verify_link = '{}://{}/c/{}'.format( 'https' if Configuration.ENVIRONMENT != 'dev' else 'http', Configuration.WEB_HOST, verify_code) logger.info('generated verify code for user "%s", "%s"' % (user.email, verify_code)) first_name = user.first_name if not first_name: first_name = user.email msg = Mail() if resend: msg.set_subject('Everyone deserves a second chance') else: msg.set_subject('Welcome to Wigo') msg.set_from('Wigo <*****@*****.**>') if user.first_name and user.last_name: msg.add_to('%s <%s>' % (user.full_name, user.email)) else: msg.add_to(user.email) msg.set_text( "Hi %s\n\nPlease click the following link to verify your email address:\n\n%s\n\n" % (first_name, verify_link)) msg.set_html( render_template('confirmation_email.html', name=first_name, confirmation_link=verify_link, resend=resend)) msg.add_unique_arg('user_id', user.id) msg.add_category('verify') msg.add_filter('opentrack', 'enable', 0) msg.add_filter('subscriptiontrack', 'enable', 1) msg.add_filter('subscriptiontrack', 'replace', '-UNSUB-') sendgrid.send(msg) logger.info('sent verification email to "%s"' % user.email)
def send_custom_email(user, subject, category, html, text, template_params=None): sendgrid = create_sendgrid() msg = Mail() msg.set_from('Wigo <*****@*****.**>') if user.first_name and user.last_name: msg.add_to('%s <%s>' % (user.full_name, user.email)) else: msg.add_to(user.email) # subject and body are required, for some reason, with the sendgrid templates msg.set_subject(Template(subject).render(user=user)) # turn on email opens tracking msg.add_filter('opentrack', 'enable', 1) msg.add_filter('subscriptiontrack', 'enable', 1) msg.add_filter('subscriptiontrack', 'replace', '-UNSUB-') params = {'user': user} if template_params: params.update(template_params) html = render_template('custom_email_inlined.html', html=Template(html).render(**params)) msg.set_html(html) if text: text = Template(text + '\n\n\nClick here to unsubscribe: -UNSUB-\n').render( **params) msg.set_text(text) msg.add_unique_arg('user_id', user.id) if category: msg.add_category(category) sendgrid.send(msg)
def get_template_id_by_name(template_name): response = sg.client.templates.get() data = json.loads(response.response_body.decode()) t_groups = data['templates'] for t_group in t_groups: templates = t_group['versions'] for template in templates: if template['name'] == template_name: return t_group['id'] template_id = get_template_id_by_name(template_name) message = Mail() message.add_filter('templates', 'enable', '1') message.add_filter('templates', 'template_id', template_id) message.set_subject(None) for to_addr in to_addrs: message.add_to(to_addr) for key, value in context.items(): message.add_substitution("%{}%".format(key), value) message.set_from('Foo <*****@*****.**>') message.set_html(' ') message.set_text(' ') message.set_subject(' ')
def get_template_id_by_name(template_name): response = sg.client.templates.get() data = json.loads(response.response_body.decode()) t_groups = data['templates'] for t_group in t_groups: templates = t_group['versions'] for template in templates: if template['name'] == template_name: return t_group['id'] template_id = get_template_id_by_name(template_name) message = Mail() message.add_filter('templates', 'enable', '1') message.add_filter('templates', 'template_id', template_id) message.set_subject(None) for to_addr in to_addrs: message.add_to(to_addr) for key, value in context.items(): message.add_substitution("%{}%".format(key), value) message.set_from('Foo <*****@*****.**>') message.set_html(' ') message.set_text(' ') message.set_subject(' ') # scrapy # xpath selection