Is there any chance someone has a working example ...
# box-products
r
Is there any chance someone has a working example of send-grid-protocol (https://github.com/elpete/send-grid-protocol) working with a template and body tokens? I do have it working with plain/html emails, just not templates. Feels like I am missing something simple.
Copy code
component {

	property name = "wirebox" inject = "wirebox";
	property name ="dsn" inject="coldbox:setting:dsn";
	property name = "mailService" inject = "MailService@cbmailservices";

	function index (event, rc, prc) {

		variables.mailService
			.newMail(
				to: "<mailto:robert@email.com|robert@email.com>",
				subject: "Test email",
				type: "plain"
			)
			.setBodyTokens({
				"[%message%]": "You are here"
			})
			.setBody(" Message: @message@ ")
			.send();

		writeDump(var = "here", abort = 1);
	}

}
Not having a lot of luck with mail body replacing tokens either. The above code generates "Message: @message@" in the email
I did manage to get the body tokens working when passing in the body. Just have to get the templateId working now
i
Setting the body to the templateID doesn't work? Plus type = template instead of plain
Copy code
mail.setBody( templateId );
r
It is failing silently (no output, no email)
(I did set the type to template and provide the templateId)
My thought was perhaps I am passing the tokens for the template incorrectly
Dumping out the cfhttp response from the module, it appears to be an issue with substitutions and dynamic templating.
The issue is the v3 api is expecting the dynamic template data to be in the
dynamic_template_data
structure in the personalizations. Substitutions don't seem to be valid for dynamic templates
e
@Robert Zehnder not familiar with that library. @mjclemente's SendGrid.cfc on forgebox works great though.
Copy code
component output=false {

  variables.sendgrid = new com.sendgridcfc.sendgrid( apiKey = server.system.environment.SENDGRID_API_KEY );

  public struct function sendTemplate(
      required string templateID,
      required struct messageData,
    ) output = false {
  
    // mesageData example
    // local.messageData = {
    //   "emailTo" : arguments.emailTo,
    //   "emailFrom" : arguments.emailFrom,
    //   "emailFromName" : arguments.emailFromName,
    //   "cc" : "",
    //   "bcc" : "",
    //   "attachment" : "",
    //   "subject" : arguments.subject
    // }
  
    local.envelope = local.envelope = new com.sendgridcfc.helpers.mail()
        .from({
            name = arguments.messageData.emailFromName?:"",
            email = arguments.messageData.emailFrom
        })
        .to(arguments.messageData.emailTo)
        .templateId(arguments.templateID)
        .withDynamicTemplateData(messageData);

    return send(envelope, <http://arguments.messageData.cc?:%22%22|arguments.messageData.cc?:"">, arguments.messageData.bcc?:"", arguments.messageData.attachment?:"");
  
  }
  
  private struct function send(
      required envelope,
      string cc = "",
      string bcc = "",
      string attachment = ""
  ) output = false {
  
      if (len(trim(<http://arguments.cc|arguments.cc>))) {
          envelope.addCC(<http://arguments.cc|arguments.cc>);
      }
  
      if (len(trim(arguments.bcc))) {
          envelope.addBCC(arguments.bcc);
      }
  
      if (len(trim(arguments.attachment))) {
          envelope.attachFile(arguments.attachment);
      }
  
      return sendgrid.sendMail(envelope);
  
  }

}
r
I figured out the issue. I will see if I can get a pull request off to @elpete to get it fixed. Today has been one of those days
🙌 1