@@ -63,9 +63,9 @@ def test_send_mail(self):
63
63
self .
assertEqual (
data [
'from' ], {
'email' :
"[email protected] " })
64
64
self .assertEqual (data ['personalizations' ], [{
65
65
'to' : [{
'email' :
"[email protected] " }],
66
+ # make sure the backend assigned the anymail_id for event tracking and notification
67
+ 'custom_args' : {'anymail_id' : 'mocked-uuid-1' },
66
68
}])
67
- # make sure the backend assigned the anymail_id for event tracking and notification
68
- self .assertEqual (data ['custom_args' ]['anymail_id' ], 'mocked-uuid-1' )
69
69
70
70
def test_name_addr (self ):
71
71
"""Make sure RFC2822 name-addr format (with display-name) is allowed
@@ -115,6 +115,8 @@ def test_email_message(self):
115
115
{
'email' :
"[email protected] " ,
'name' :
'"Also CC"' }],
116
116
'bcc' : [{
'email' :
"[email protected] " },
117
117
{
'email' :
"[email protected] " ,
'name' :
'"Also BCC"' }],
118
+ # make sure custom Message-ID also added to custom_args
119
+ 'custom_args' : {'anymail_id' : 'mocked-uuid-1' },
118
120
}])
119
121
120
122
self .
assertEqual (
data [
'from' ], {
'email' :
"[email protected] " })
@@ -125,8 +127,6 @@ def test_email_message(self):
125
127
'X-MyHeader' : "my value" ,
126
128
'Message-ID' :
"<[email protected] >" ,
127
129
})
128
- # make sure custom Message-ID also added to custom_args
129
- self .assertEqual (data ['custom_args' ]['anymail_id' ], 'mocked-uuid-1' )
130
130
131
131
def test_html_message (self ):
132
132
text_content = 'This is an important message.'
@@ -454,14 +454,17 @@ def test_merge_data(self):
454
454
self .assertEqual (data ['personalizations' ], [
455
455
{
'to' : [{
'email' :
'[email protected] ' }],
456
456
'cc' : [{
'email' :
'[email protected] ' }],
# all recipients get the cc
457
+ 'custom_args' : {'anymail_id' : 'mocked-uuid-1' },
457
458
'dynamic_template_data' : {
458
459
'name' : "Alice" , 'group' : "Developers" , 'site' : "ExampleCo" }},
459
460
{
'to' : [{
'email' :
'[email protected] ' ,
'name' :
'"Bob"' }],
460
461
'cc' : [{
'email' :
'[email protected] ' }],
462
+ 'custom_args' : {'anymail_id' : 'mocked-uuid-2' },
461
463
'dynamic_template_data' : {
462
464
'name' : "Bob" , 'group' : "Users" , 'site' : "ExampleCo" }},
463
465
{
'to' : [{
'email' :
'[email protected] ' }],
464
466
'cc' : [{
'email' :
'[email protected] ' }],
467
+ 'custom_args' : {'anymail_id' : 'mocked-uuid-3' },
465
468
'dynamic_template_data' : {
466
469
'group' : "Users" , 'site' : "ExampleCo" }},
467
470
])
@@ -477,6 +480,7 @@ def test_explicit_dynamic_template(self):
477
480
data = self .get_api_call_json ()
478
481
self .assertEqual (data ['personalizations' ], [
479
482
{
'to' : [{
'email' :
'[email protected] ' }],
483
+ 'custom_args' : {'anymail_id' : 'mocked-uuid-1' },
480
484
'dynamic_template_data' : {"test" : "data" }}])
481
485
482
486
self .message .template_id = "d-apparently-not-legacy"
@@ -486,6 +490,7 @@ def test_explicit_dynamic_template(self):
486
490
data = self .get_api_call_json ()
487
491
self .assertEqual (data ['personalizations' ], [
488
492
{
'to' : [{
'email' :
'[email protected] ' }],
493
+ 'custom_args' : {'anymail_id' : 'mocked-uuid-2' },
489
494
'substitutions' : {"<%test%>" : "data" }}])
490
495
491
496
def test_legacy_merge_data (self ):
@@ -514,13 +519,16 @@ def test_legacy_merge_data(self):
514
519
self .assertEqual (data ['personalizations' ], [
515
520
{
'to' : [{
'email' :
'[email protected] ' }],
516
521
'cc' : [{
'email' :
'[email protected] ' }],
# all recipients get the cc
522
+ 'custom_args' : {'anymail_id' : 'mocked-uuid-1' },
517
523
'substitutions' : {':name' : "Alice" , ':group' : "Developers" ,
518
524
':site' : "ExampleCo" }}, # merge_global_data merged
519
525
{
'to' : [{
'email' :
'[email protected] ' ,
'name' :
'"Bob"' }],
520
526
'cc' : [{
'email' :
'[email protected] ' }],
527
+ 'custom_args' : {'anymail_id' : 'mocked-uuid-2' },
521
528
'substitutions' : {':name' : "Bob" , ':group' : "Users" , ':site' : "ExampleCo" }},
522
529
{
'to' : [{
'email' :
'[email protected] ' }],
523
530
'cc' : [{
'email' :
'[email protected] ' }],
531
+ 'custom_args' : {'anymail_id' : 'mocked-uuid-3' },
524
532
'substitutions' : {':group' : "Users" , ':site' : "ExampleCo" }},
525
533
])
526
534
self .assertNotIn ('sections' , data ) # 'sections' no longer used for merge_global_data
@@ -538,9 +546,11 @@ def test_legacy_merge_field_format_setting(self):
538
546
data = self .get_api_call_json ()
539
547
self .assertEqual (data ['personalizations' ], [
540
548
{
'to' : [{
'email' :
'[email protected] ' }],
549
+ 'custom_args' : {'anymail_id' : 'mocked-uuid-1' },
541
550
'substitutions' : {':name' : "Alice" , ':group' : "Developers" , # keys changed to :field
542
551
':site' : "ExampleCo" }},
543
552
{
'to' : [{
'email' :
'[email protected] ' ,
'name' :
'"Bob"' }],
553
+ 'custom_args' : {'anymail_id' : 'mocked-uuid-2' },
544
554
'substitutions' : {':name' : "Bob" , ':site' : "ExampleCo" }}
545
555
])
546
556
@@ -557,8 +567,10 @@ def test_legacy_merge_field_format_esp_extra(self):
557
567
data = self .get_api_call_json ()
558
568
self .assertEqual (data ['personalizations' ], [
559
569
{
'to' : [{
'email' :
'[email protected] ' }],
570
+ 'custom_args' : {'anymail_id' : 'mocked-uuid-1' },
560
571
'substitutions' : {'*|name|*' : "Alice" , '*|group|*' : "Developers" , '*|site|*' : "ExampleCo" }},
561
572
{
'to' : [{
'email' :
'[email protected] ' ,
'name' :
'"Bob"' }],
573
+ 'custom_args' : {'anymail_id' : 'mocked-uuid-2' },
562
574
'substitutions' : {'*|name|*' : "Bob" , '*|site|*' : "ExampleCo" }}
563
575
])
564
576
# Make sure our esp_extra merge_field_format doesn't get sent to SendGrid API:
@@ -587,11 +599,11 @@ def test_merge_metadata(self):
587
599
data = self .get_api_call_json ()
588
600
self .assertEqual (data ['personalizations' ], [
589
601
{
'to' : [{
'email' :
'[email protected] ' }],
590
- 'custom_args' : {'order_id' : '123' }},
602
+ # anymail_id added to other custom_args
603
+ 'custom_args' : {'anymail_id' : 'mocked-uuid-1' , 'order_id' : '123' }},
591
604
{
'to' : [{
'email' :
'[email protected] ' ,
'name' :
'"Bob"' }],
592
- 'custom_args' : {'order_id' : '678' , 'tier' : 'premium' }},
605
+ 'custom_args' : {'anymail_id' : 'mocked-uuid-2' , ' order_id' : '678' , 'tier' : 'premium' }},
593
606
])
594
- self .assertEqual (data ['custom_args' ], {'anymail_id' : 'mocked-uuid-1' })
595
607
596
608
def test_metadata_with_merge_metadata (self ):
597
609
# Per SendGrid docs: "personalizations[x].custom_args will be merged
@@ -608,12 +620,11 @@ def test_metadata_with_merge_metadata(self):
608
620
data = self .get_api_call_json ()
609
621
self .assertEqual (data ['personalizations' ], [
610
622
{
'to' : [{
'email' :
'[email protected] ' }],
611
- 'custom_args' : {'order_id' : '123' }},
623
+ 'custom_args' : {'anymail_id' : 'mocked-uuid-1' , ' order_id' : '123' }},
612
624
{
'to' : [{
'email' :
'[email protected] ' ,
'name' :
'"Bob"' }],
613
- 'custom_args' : {'order_id' : '678' , 'tier' : 'premium' }},
625
+ 'custom_args' : {'anymail_id' : 'mocked-uuid-2' , ' order_id' : '678' , 'tier' : 'premium' }},
614
626
])
615
- self .assertEqual (data ['custom_args' ],
616
- {'tier' : 'basic' , 'batch' : 'ax24' , 'anymail_id' : 'mocked-uuid-1' })
627
+ self .assertEqual (data ['custom_args' ], {'tier' : 'basic' , 'batch' : 'ax24' })
617
628
618
629
def test_merge_metadata_with_merge_data (self ):
619
630
# (using dynamic templates)
@@ -641,16 +652,17 @@ def test_merge_metadata_with_merge_data(self):
641
652
'cc' : [{
'email' :
'[email protected] ' }],
# all recipients get the cc
642
653
'dynamic_template_data' : {
643
654
'name' : "Alice" , 'group' : "Developers" , 'site' : "ExampleCo" },
644
- 'custom_args' : {'order_id' : '123' }},
655
+ 'custom_args' : {'anymail_id' : 'mocked-uuid-1' , ' order_id' : '123' }},
645
656
{
'to' : [{
'email' :
'[email protected] ' ,
'name' :
'"Bob"' }],
646
657
'cc' : [{
'email' :
'[email protected] ' }],
647
658
'dynamic_template_data' : {
648
659
'name' : "Bob" , 'group' : "Users" , 'site' : "ExampleCo" },
649
- 'custom_args' : {'order_id' : '678' , 'tier' : 'premium' }},
660
+ 'custom_args' : {'anymail_id' : 'mocked-uuid-2' , ' order_id' : '678' , 'tier' : 'premium' }},
650
661
{
'to' : [{
'email' :
'[email protected] ' }],
651
662
'cc' : [{
'email' :
'[email protected] ' }],
652
663
'dynamic_template_data' : {
653
- 'group' : "Users" , 'site' : "ExampleCo" }},
664
+ 'group' : "Users" , 'site' : "ExampleCo" },
665
+ 'custom_args' : {'anymail_id' : 'mocked-uuid-3' }},
654
666
])
655
667
656
668
def test_merge_metadata_with_legacy_template (self ):
@@ -677,15 +689,15 @@ def test_merge_metadata_with_legacy_template(self):
677
689
self .assertEqual (data ['personalizations' ], [
678
690
{
'to' : [{
'email' :
'[email protected] ' }],
679
691
'cc' : [{
'email' :
'[email protected] ' }],
# all recipients get the cc
680
- 'custom_args' : {'order_id' : '123' },
692
+ 'custom_args' : {'anymail_id' : 'mocked-uuid-1' , ' order_id' : '123' },
681
693
'substitutions' : {':name' : "Alice" , ':group' : "Developers" , ':site' : "ExampleCo" }},
682
694
{
'to' : [{
'email' :
'[email protected] ' ,
'name' :
'"Bob"' }],
683
695
'cc' : [{
'email' :
'[email protected] ' }],
684
- 'custom_args' : {'order_id' : '678' , 'tier' : 'premium' },
696
+ 'custom_args' : {'anymail_id' : 'mocked-uuid-2' , ' order_id' : '678' , 'tier' : 'premium' },
685
697
'substitutions' : {':name' : "Bob" , ':group' : "Users" , ':site' : "ExampleCo" }},
686
698
{
'to' : [{
'email' :
'[email protected] ' }],
687
699
'cc' : [{
'email' :
'[email protected] ' }],
688
- # no custom_args
700
+ 'custom_args' : { 'anymail_id' : 'mocked-uuid-3' },
689
701
'substitutions' : {':group' : "Users" , ':site' : "ExampleCo" }},
690
702
])
691
703
@@ -756,8 +768,10 @@ def test_esp_extra_pesonalizations(self):
756
768
data = self .get_api_call_json ()
757
769
self .assertEqual (data ['personalizations' ], [
758
770
{
'to' : [{
'email' :
'[email protected] ' ,
'name' :
'"First recipient"' }],
771
+ 'custom_args' : {'anymail_id' : 'mocked-uuid-1' },
759
772
'future_feature' : "works" },
760
773
{
'to' : [{
'email' :
'[email protected] ' }],
774
+ 'custom_args' : {'anymail_id' : 'mocked-uuid-2' },
761
775
'future_feature' : "works" }, # merged into *every* recipient
762
776
])
763
777
@@ -770,6 +784,7 @@ def test_esp_extra_pesonalizations(self):
770
784
data = self .get_api_call_json ()
771
785
self .assertEqual (data ['personalizations' ], [
772
786
{
'to' : [{
'email' :
'[email protected] ' }],
787
+ 'custom_args' : {'anymail_id' : 'mocked-uuid-3' },
773
788
'future_feature' : "works" },
774
789
])
775
790
@@ -784,10 +799,22 @@ def test_send_attaches_anymail_status(self):
784
799
self .assertEqual (msg .anymail_status .status , {'queued' })
785
800
self .assertEqual (msg .anymail_status .message_id , 'mocked-uuid-1' )
786
801
self .
assertEqual (
msg .
anymail_status .
recipients [
'[email protected] ' ].
status ,
'queued' )
787
- self .
assertEqual (
msg .
anymail_status .
recipients [
'[email protected] ' ].
message_id ,
788
- msg .anymail_status .message_id )
802
+ self .
assertEqual (
msg .
anymail_status .
recipients [
'[email protected] ' ].
message_id ,
'mocked-uuid-1' )
789
803
self .assertEqual (msg .anymail_status .esp_response .content , self .DEFAULT_RAW_RESPONSE )
790
804
805
+ def test_batch_recipients_get_unique_message_ids (self ):
806
+ """In a batch send, each recipient should get a distinct own message_id"""
807
+ msg = mail .
EmailMessage (
'Subject' ,
'Message' ,
'[email protected] ' ,
808
+
809
+
810
+ msg .merge_data = {} # force batch send
811
+ msg .send ()
812
+ self .assertEqual (msg .anymail_status .message_id , {'mocked-uuid-1' , 'mocked-uuid-2' })
813
+ self .
assertEqual (
msg .
anymail_status .
recipients [
'[email protected] ' ].
message_id ,
'mocked-uuid-1' )
814
+ self .
assertEqual (
msg .
anymail_status .
recipients [
'[email protected] ' ].
message_id ,
'mocked-uuid-2' )
815
+ # cc's (and bcc's) get copied for all batch recipients, but we can only communicate one message_id:
816
+ self .
assertEqual (
msg .
anymail_status .
recipients [
'[email protected] ' ].
message_id ,
'mocked-uuid-2' )
817
+
791
818
@override_settings (ANYMAIL_SENDGRID_GENERATE_MESSAGE_ID = False )
792
819
def test_disable_generate_message_id (self ):
793
820
msg = mail .
EmailMessage (
'Subject' ,
'Message' ,
'[email protected] ' , [
'[email protected] ' ],)
0 commit comments