sendgrid-ruby
Advanced tools
| # Personalizations | ||
| 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). | ||
| ```ruby | ||
| require 'sendgrid-ruby' | ||
| include SendGrid | ||
| # Note that the domain for all From addresses must match | ||
| mail = Mail.new | ||
| mail.from = Email.new(email: 'test@example.com') | ||
| mail.add_content(Content.new(type: 'text/plain', value: 'Some test text')) | ||
| mail.subject = 'Personalized Test Email' | ||
| personalization = Personalization.new | ||
| personalization.add_to(Email.new(email: 'test1@example.com')) | ||
| mail.add_personalization(personalization) | ||
| personalization2 = Personalization.new | ||
| personalization2.add_to(Email.new(email: 'test2@example.com')) | ||
| personalization2.add_from(Email.new(email: 'test3@example.com')) | ||
| mail.add_personalization(personalization2) | ||
| sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) | ||
| begin | ||
| response = sg.client.mail._("send").post(request_body: mail.to_json) | ||
| rescue Exception => e | ||
| puts e.message | ||
| end | ||
| puts response.status_code | ||
| puts response.body | ||
| puts response.headers | ||
| ``` |
+6
-0
| # Change Log | ||
| All notable changes to this project will be documented in this file. | ||
| [2021-11-03] Version 6.6.0 | ||
| -------------------------- | ||
| **Library - Feature** | ||
| - [PR #473](https://github.com/sendgrid/sendgrid-ruby/pull/473): update tests, use-cases, examples and implementation for From personalization. Thanks to [@beebzz](https://github.com/beebzz)! | ||
| [2021-10-18] Version 6.5.2 | ||
@@ -5,0 +11,0 @@ -------------------------- |
@@ -35,3 +35,3 @@ require 'sendgrid-ruby' | ||
| # match the domain of the from email property specified at root level | ||
| personalization.from = Email.new(email: 'alias@example.com', name: "My alias") | ||
| personalization.add_from(Email.new(email: 'alias@example.com', name: "My alias")) | ||
| personalization.subject = 'Hello World from the Personalized Twilio SendGrid Ruby Library' | ||
@@ -38,0 +38,0 @@ personalization.add_header(Header.new(key: 'X-Test', value: 'True')) |
@@ -5,6 +5,6 @@ require 'json' | ||
| class Personalization | ||
| attr_reader :tos, :ccs, :bccs, :headers, :substitutions, :custom_args, | ||
| attr_reader :tos, :from, :ccs, :bccs, :headers, :substitutions, :custom_args, | ||
| :dynamic_template_data | ||
| attr_accessor :send_at, :subject, :from | ||
| attr_accessor :send_at, :subject | ||
@@ -30,2 +30,6 @@ def initialize | ||
| def add_from(from) | ||
| @from = from.to_json | ||
| end | ||
| def add_cc(cc) | ||
@@ -32,0 +36,0 @@ raise DuplicatePersonalizationError if duplicate?(cc) |
| module SendGrid | ||
| VERSION = '6.5.2'.freeze | ||
| VERSION = '6.6.0'.freeze | ||
| end |
@@ -48,2 +48,15 @@ require_relative '../../../../lib/sendgrid/helpers/mail/personalization' | ||
| def test_add_from | ||
| @personalization = Personalization.new | ||
| @personalization.add_from(Email.new(email: 'from1@example.com', name: 'Example Sender')) | ||
| expected_json = { | ||
| 'from' => { | ||
| 'email' => 'from1@example.com', | ||
| 'name' => 'Example Sender' | ||
| } | ||
| } | ||
| assert_equal @personalization.to_json, expected_json | ||
| end | ||
| def test_add_cc | ||
@@ -50,0 +63,0 @@ @personalization = Personalization.new |
@@ -8,2 +8,3 @@ This directory provides examples for specific use cases. | ||
| * [Legacy Templates](legacy-templates.md) | ||
| * [Personalizations](personalizations.md) | ||
@@ -10,0 +11,0 @@ # Twilio Use Cases |
Sorry, the diff of this file is too big to display