Skip to content

How to use a template to send mail? #446

Closed

Description

I am based on this demo:
https://github.com/sendgrid/sendgrid-java/blob/master/USE_CASES.md#transactional-templates
But, get this error result:
Request returned status Code 400Body:{"errors":[{"message":"Substitutions may not be used with dynamic templating","field":"personalizations.0.substitutions","help":"http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.personalizations.substitutions"}]}

  • sendgrid-java Version: 4.0.1
  • Java Version: 1.8

How should I modify it? Thank you!

Activity

  1. added
    type: question
    status: help wanted
    difficulty: unknown or n/a
    on Jul 26, 2018
  2. thinkingserious commented on Jul 26, 2018

    @thinkingserious
    Contributor

    Hello @xiandingwang,

    If you are using the latest versions of our templates, please see this issue.

    That example still works with our legacy templates.

    With Best Regards,

    Elmer

  3. pdtyreus commented on Jul 29, 2018

    @pdtyreus

    I worked around this for now by extending Personalization.

        private static class DynamicTemplatePersonalization extends Personalization {
    
            @JsonProperty(value = "dynamic_template_data")
            private Map<String, Object> dynamicTemplateData;
    
            @JsonProperty("dynamic_template_data")
            public Map<String, Object> getDynamicTemplateData() {
                if (dynamicTemplateData == null) {
                    return Collections.<String, Object>emptyMap();
                }
                return dynamicTemplateData;
            }
    
            public void addDynamicTemplateData(String key, Object value) {
                if (dynamicTemplateData == null) {
                    dynamicTemplateData = new HashMap<String, Object>();
                    dynamicTemplateData.put(key, value);
                } else {
                    dynamicTemplateData.put(key, value);
                }
            }
    
        }

    then you can use it with the Helper

            Mail mail = new Mail();
            //use extended class instead of Personalization here
            DynamicTemplatePersonalization personalization = new DynamicTemplatePersonalization();
            Email to = new Email();
            to.setName(name);
            to.setEmail(email);
            personalization.addTo(to);
            personalization.addDynamicTemplateData("token", token);
            mail.addPersonalization(personalization);

    I think it would be trivial to submit a PR to update Personalization but I don't have time to review the CLA right now.

  4. ghost closed this as completedon Jul 30, 2018
  5. thinkingserious commented on Jul 30, 2018

    @thinkingserious
    Contributor

    Hi @pdtyreus,

    Thanks for helping out!

    If you have a moment, a code review on #449 would be most appreciated :)

    With Best Regards,

    Elmer

  6. amalajith commented on Aug 21, 2020

    @amalajith

    Easier way without needing extra class. Import the Personalization class in com.sendgrid.****

            Email from = new Email("no-reply@example.com");
            String subject = "Password reset";
    
            Email to = new Email(email);
            to.setName(name);
    
            Mail mail = new Mail();
            mail.setFrom(from);
            mail.setSubject(subject);
            mail.setTemplateId("your-template-id");
    
    
            Personalization personalization = new Personalization();
            personalization.addTo(to);
            personalization.addDynamicTemplateData("name", name);
            personalization.addDynamicTemplateData("passwordResetCode", passwordResetCode);
    
            mail.addPersonalization(personalization);
    
            SendGrid sg = new SendGrid("your-key");
            Request request = new Request();
            try {
                request.setMethod(Method.POST);
                request.setEndpoint("mail/send");
                request.setBody(mail.build());
    //            Response response = sg.api(request);
                sg.api(request);
            } catch (IOException ex) {
                throw new IOException(ex);
            }
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    difficulty: unknown or n/astatus: help wantedtype: question

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions