def index(self): if request.method=='GET': user_emails = db_session.query(User).filter(User.new_features_subscription == True) email_list = [] for user in user_emails: email_list.append(user.email) email_list.append(';') context = {'user_emails': email_list} return self.render('sendemail.html', **context) else: jsondata = request.get_json(force=True) users_send_email_to = db_session.query(User).filter(User.new_features_subscription == True) message = Mail() message.set_subject(jsondata['subject'].encode("utf8")) message.set_text(jsondata['message'].encode("utf8")) message.set_from('ANYWAY Team <*****@*****.**>') for user in users_send_email_to: message.add_bcc(user.email) try: status, msg = sg.send(message) except SendGridClientError: return "Error occurred while trying to send the emails" except SendGridServerError: return "Error occurred while trying to send the emails" return "Email/s Sent"
def index(self): if request.method == 'GET': user_emails = db.session.query(User).filter( User.new_features_subscription == True) email_list = [] for user in user_emails: email_list.append(user.email) email_list.append(';') context = {'user_emails': email_list} return self.render('sendemail.html', **context) else: jsondata = request.get_json(force=True) users_send_email_to = db.session.query(User).filter( User.new_features_subscription == True) message = Mail() message.set_subject(jsondata['subject'].encode("utf8")) message.set_text(jsondata['message'].encode("utf8")) message.set_from('ANYWAY Team <*****@*****.**>') for user in users_send_email_to: message.add_bcc(user.email) try: sg.send(message) except SendGridClientError: return "Error occurred while trying to send the emails" except SendGridServerError: return "Error occurred while trying to send the emails" return "Email/s Sent"
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.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_drop_to_header(self): m = Mail() m.add_to('John, Doe <*****@*****.**>') m.set_from('*****@*****.**') m.set_subject('test') m.set_text('test') m.add_bcc('John, Doe <*****@*****.**>') url = self.sg._build_body(m) print url
def test_drop_to_header(self): smtpapi = "{}" m = Mail() m.add_to("John, Doe <*****@*****.**>") m.set_from("*****@*****.**") m.set_subject("test") m.set_text("test") m.add_bcc("John, Doe <*****@*****.**>") url = self.sg._build_body(m) self.assertEqual(smtpapi, url["x-smtpapi"])
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)