Skip to content

Commit 483dddb

Browse files
author
Bilal Boussayoud
authored
feat: update tests, use-cases, examples and implementation for From personalization (#473)
1 parent 1d0fe3a commit 483dddb

File tree

5 files changed

+55
-3
lines changed

5 files changed

+55
-3
lines changed

examples/helpers/mail/example.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def kitchen_sink
3232
personalization.add_bcc(Email.new(email: '[email protected]', name: 'Example User'))
3333
# Note, the domain of the from email property specified in any personalization must
3434
# match the domain of the from email property specified at root level
35-
personalization.from = Email.new(email: '[email protected]', name: "My alias")
35+
personalization.add_from(Email.new(email: '[email protected]', name: "My alias"))
3636
personalization.subject = 'Hello World from the Personalized Twilio SendGrid Ruby Library'
3737
personalization.add_header(Header.new(key: 'X-Test', value: 'True'))
3838
personalization.add_header(Header.new(key: 'X-Mock', value: 'False'))

lib/sendgrid/helpers/mail/personalization.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
module SendGrid
44
class Personalization
5-
attr_reader :tos, :ccs, :bccs, :headers, :substitutions, :custom_args,
5+
attr_reader :tos, :from, :ccs, :bccs, :headers, :substitutions, :custom_args,
66
:dynamic_template_data
77

8-
attr_accessor :send_at, :subject, :from
8+
attr_accessor :send_at, :subject
99

1010
def initialize
1111
@tos = []
@@ -26,6 +26,10 @@ def add_to(to)
2626
@tos << to.to_json
2727
end
2828

29+
def add_from(from)
30+
@from = from.to_json
31+
end
32+
2933
def add_cc(cc)
3034
raise DuplicatePersonalizationError if duplicate?(cc)
3135

test/sendgrid/helpers/mail/test_personalizations.rb

+13
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ def test_duplicate_add_to
4545
end
4646
end
4747

48+
def test_add_from
49+
@personalization = Personalization.new
50+
@personalization.add_from(Email.new(email: '[email protected]', name: 'Example Sender'))
51+
expected_json = {
52+
'from' => {
53+
'email' => '[email protected]',
54+
'name' => 'Example Sender'
55+
}
56+
}
57+
58+
assert_equal @personalization.to_json, expected_json
59+
end
60+
4861
def test_add_cc
4962
@personalization = Personalization.new
5063
@personalization.add_cc(Email.new(email: '[email protected]', name: 'Example User'))

use-cases/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Please [open an issue](https://github.com/sendgrid/sendgrid-ruby/issues) or [mak
55
# Email Use Cases
66
* [Transactional Templates](transactional-templates.md)
77
* [Legacy Templates](legacy-templates.md)
8+
* [Personalizations](personalizations.md)
89

910
# Twilio Use Cases
1011
* [Twilio Setup](twilio-setup.md)

use-cases/personalizations.md

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Personalizations
2+
3+
This example demonstrates how to send multiple emails with personalizations. For further documentation, refer to [the SendGrid docs](https://docs.sendgrid.com/for-developers/sending-email/personalizations).
4+
5+
```ruby
6+
require 'sendgrid-ruby'
7+
include SendGrid
8+
9+
# Note that the domain for all From addresses must match
10+
mail = Mail.new
11+
mail.from = Email.new(email: '[email protected]')
12+
mail.add_content(Content.new(type: 'text/plain', value: 'Some test text'))
13+
mail.subject = 'Personalized Test Email'
14+
15+
personalization = Personalization.new
16+
personalization.add_to(Email.new(email: '[email protected]'))
17+
mail.add_personalization(personalization)
18+
19+
personalization2 = Personalization.new
20+
personalization2.add_to(Email.new(email: '[email protected]'))
21+
personalization2.add_from(Email.new(email: '[email protected]'))
22+
mail.add_personalization(personalization2)
23+
24+
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
25+
begin
26+
response = sg.client.mail._("send").post(request_body: mail.to_json)
27+
rescue Exception => e
28+
puts e.message
29+
end
30+
31+
puts response.status_code
32+
puts response.body
33+
puts response.headers
34+
```

0 commit comments

Comments
 (0)