def test_drop_empty(self): header = SMTPAPIHeader() header.set_tos([]) header.set_substitutions(json.loads('{"subKey":["subValue"]}')) header.set_sections(json.loads('{}')) header.set_categories([]) header.set_unique_args(json.loads('{"testUnique":"uniqueValue"}')) header.add_filter('testFilter', 'filter', 'filterValue') self.assertEqual(self.dropsHeader, json.loads(header.json_string()))
def test_add(self): header = SMTPAPIHeader() header.add_to('*****@*****.**') header.add_substitution('subKey', 'subValue') header.add_section('testSection', 'sectionValue') header.add_category('testCategory') header.add_unique_arg('testUnique', 'uniqueValue') header.add_filter('testFilter', 'filter', 'filterValue') self.assertEqual(self.validHeader, json.loads(header.json_string()))
def test_set(self): header = SMTPAPIHeader() header.set_tos(["*****@*****.**"]) header.set_substitutions(json.loads('{"subKey":["subValue"]}')) header.set_sections(json.loads('{"testSection":"sectionValue"}')) header.set_categories(["testCategory"]) header.set_unique_args(json.loads('{"testUnique":"uniqueValue"}')) header.add_filter('testFilter', 'filter', 'filterValue') self.assertEqual(self.validHeader, json.loads(header.json_string()))
def send_template_email( to_email, subject, template, context, sent_by=None, hours=None): body = render_to_string(template, context) if hours and sent_by: header = SMTPAPIHeader() delay = datetime.timedelta(hours=hours) send_at = timezone.now() + delay header.set_send_at(int(send_at.timestamp())) email = EmailMultiAlternatives( subject, body, sent_by, to=[to_email], headers={"X-SMTPAPI": header.json_string()}, ) elif hours: header = SMTPAPIHeader() delay = datetime.timedelta(hours=hours) send_at = timezone.now() + delay header.set_send_at(int(send_at.timestamp())) email = EmailMultiAlternatives( subject, body, to=[to_email], headers={"X-SMTPAPI": header.json_string()}, ) elif sent_by: email = EmailMultiAlternatives( subject, body, sent_by, to=[to_email], ) else: email = EmailMultiAlternatives( subject, body, to=[to_email], ) email.attach_alternative(body, "text/html") email.send()
def build_header_json(notification): # build the header containing any relevant metadata # this is sent back to us by SendGrid via webhook events header_json = None if notification.metadata is not None: metadata = notification.metadata if 'bkrnd_nid' not in metadata: metadata['bkrnd_nid'] = notification.id header = SMTPAPIHeader() header.set_unique_args(metadata) header_json = {'X-SMTPAPI': header.json_string()} return header_json
def test_set(self): header = SMTPAPIHeader() header.set_tos(["*****@*****.**", "*****@*****.**", "*****@*****.**"]) header.set_substitutions(json.loads('{"subKey":["subValue"]}')) header.set_sections(json.loads('{"testSection":"sectionValue"}')) header.set_categories(["testCategory"]) header.set_unique_args(json.loads('{"testUnique":"uniqueValue"}')) header.set_asm_group_id(42) header.set_send_each_at([1409348513, 1409348514]) header.set_send_at(1409348515) header.add_filter('testFilter', 'filter', 'filterValue') self.assertEqual(self.validHeader, json.loads(header.json_string()))
def test_set(self): header = SMTPAPIHeader() header.set_tos( ["*****@*****.**", "*****@*****.**", "*****@*****.**"]) header.set_substitutions(json.loads('{"subKey":["subValue"]}')) header.set_sections(json.loads('{"testSection":"sectionValue"}')) header.set_categories(["testCategory"]) header.set_unique_args(json.loads('{"testUnique":"uniqueValue"}')) header.set_asm_group_id(42) header.set_send_each_at([1409348513, 1409348514]) header.set_send_at(1409348515) header.add_filter('testFilter', 'filter', 'filterValue') self.assertEqual(self.validHeader, json.loads(header.json_string()))
def test_add(self): header = SMTPAPIHeader() header.add_to('*****@*****.**') header.add_to(['*****@*****.**', '*****@*****.**']) header.add_substitution('subKey', 'subValue') header.add_section('testSection', 'sectionValue') header.add_category('testCategory') header.add_unique_arg('testUnique', 'uniqueValue') header.set_asm_group_id(42) header.add_send_each_at(1409348513) header.add_send_each_at(1409348514) header.set_send_at(1409348515) header.add_filter('testFilter', 'filter', 'filterValue') self.assertEqual(self.validHeader, json.loads(header.json_string()))
def test_drop_empty(self): header = SMTPAPIHeader() header.set_tos([]) header.set_substitutions({ "subKey": ["subValue"], "decimalKey": [decimal.Decimal("1.23456789")] }) header.set_sections(json.loads('{}')) header.set_categories([]) header.set_unique_args(json.loads('{"testUnique":"uniqueValue"}')) header.set_asm_group_id(None) header.set_send_each_at([]) header.set_ip_pool(None) header.add_filter('testFilter', 'filter', 'filterValue') self.assertEqual(self.dropsHeader, json.loads(header.json_string()))
def create_message(self, subscription, **kwargs) -> Message: """ Create the basic Message with the proper headers. Uses the SendGrid SMTPAPI. """ headers = SMTPAPIHeader() headers.set_unique_args({ "project_id": str(self.project_id), "subscription_id": str(subscription.id), }) unsubscribe_group = self.get_unsubscribe_group(subscription) if unsubscribe_group: headers.set_asm_group_id(int(unsubscribe_group)) return Message(recipients=[subscription.recipient], extra_headers={"X-SMTPAPI": headers.json_string()}, **kwargs)
#header.add_substitution('key', 'value') header.set_substitutions({'key': ['value1', 'value2']}) # [Unique Arguments](http://sendgrid.com/docs/API_Reference/SMTP_API/unique_arguments.html) #header.add_unique_arg('key', 'value') header.set_unique_args({'key':'value'}) # [Categories](http://sendgrid.com/docs/API_Reference/SMTP_API/categories.html) #header.add_category('category') header.set_categories(['category1', 'category2']) # [Sections](http://sendgrid.com/docs/API_Reference/SMTP_API/section_tags.html) #header.add_section('key', 'section') header.set_sections({'key1':'section1', 'key2':'section2'}) # [Filters](http://sendgrid.com/docs/API_Reference/SMTP_API/apps.html) header.add_filter('filter', 'setting', 'value') # [ASM Group ID](https://sendgrid.com/docs/User_Guide/advanced_suppression_manager.html) header.set_asm_group_id('value') # [IP Pools](https://sendgrid.com/docs/API_Reference/Web_API_v3/IP_Management/ip_pools.html) header.set_ip_pool("testPool") # [Scheduling Parameters](https://sendgrid.com/docs/API_Reference/SMTP_API/scheduling_parameters.html) #header.add_send_each_at(unix_timestamp) # must be a unix timestamp #header.set_send_each_at([]) # must be a unix timestamp header.set_send_at(int(time.time())) # must be a unix timestamp print(header.json_string())
# header.add_substitution('key', 'value') header.set_substitutions({'key': ['value1', 'value2']}) # [Unique Arguments](http://sendgrid.com/docs/API_Reference/SMTP_API/unique_arguments.html) # header.add_unique_arg('key', 'value') header.set_unique_args({'key': 'value'}) # [Categories](http://sendgrid.com/docs/API_Reference/SMTP_API/categories.html) # header.add_category('category') header.set_categories(['category1', 'category2']) # [Sections](http://sendgrid.com/docs/API_Reference/SMTP_API/section_tags.html) # header.add_section('key', 'section') header.set_sections({'key1': 'section1', 'key2': 'section2'}) # [Filters](http://sendgrid.com/docs/API_Reference/SMTP_API/apps.html) header.add_filter('filter', 'setting', 'value') # [ASM Group ID](https://sendgrid.com/docs/User_Guide/advanced_suppression_manager.html) header.set_asm_group_id('value') # [IP Pools](https://sendgrid.com/docs/API_Reference/Web_API_v3/IP_Management/ip_pools.html) header.set_ip_pool("testPool") # [Scheduling Parameters](https://sendgrid.com/docs/API_Reference/SMTP_API/scheduling_parameters.html) # header.add_send_each_at(unix_timestamp) # must be a unix timestamp # header.set_send_each_at([]) # must be a unix timestamp header.set_send_at(int(time.time())) # must be a unix timestamp print(header.json_string())
#header.add_substitution('key', 'value') header.set_substitutions({'key': ['value1', 'value2']}) # [Unique Arguments](http://sendgrid.com/docs/API_Reference/SMTP_API/unique_arguments.html) #header.add_unique_arg('key', 'value') header.set_unique_args({'key':'value'}) # [Categories](http://sendgrid.com/docs/API_Reference/SMTP_API/categories.html) #header.add_category('category') header.set_categories(['category1', 'category2']) # [Sections](http://sendgrid.com/docs/API_Reference/SMTP_API/section_tags.html) #header.add_section('key', 'section') header.set_sections({'key1':'section1', 'key2':'section2'}) # [Filters](http://sendgrid.com/docs/API_Reference/SMTP_API/apps.html) header.add_filter('filter', 'setting', 'value') # [ASM Group ID](https://sendgrid.com/docs/User_Guide/advanced_suppression_manager.html) header.set_asm_group_id('value') # [IP Pools](https://sendgrid.com/docs/API_Reference/Web_API_v3/IP_Management/ip_pools.html) header.set_ip_pool("testPool") # [Scheduling Parameters](https://sendgrid.com/docs/API_Reference/SMTP_API/scheduling_parameters.html) #header.add_send_each_at(unix_timestamp) # must be a unix timestamp #header.set_send_each_at([]) # must be a unix timestamp header.set_send_at(int(time.time())) # must be a unix timestamp print header.json_string()