Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameters in SendInBlue API (Brevo) does not work. Anyone facing the same problem?

Since yesterday I've been trying to pass parameters through the SendInBlue API but it just doesn't work. I've tried everything. I've seen many code examples, but unfortunately it doesn't work.

I even used as an example the code in C# that they put on their website and it still doesn't work.

Follow my code:

using System.Diagnostics;
using System.Net.Http.Headers;
using Newtonsoft.Json.Linq;
using sib_api_v3_sdk.Api;
using sib_api_v3_sdk.Client;
using sib_api_v3_sdk.Model;
using Task = System.Threading.Tasks.Task;

namespace cjuridico.Domain.Models.Email;

public static class CadastroEmail
{
    public static async Task EnviarEmail(string destinatario, string mensagem)
    {
        Configuration.Default.AddApiKey("api-key", "<omitted>");
        var apiInstance = new TransactionalEmailsApi();
            string SenderName = "John Doe";
            string SenderEmail = "[email protected]";
            SendSmtpEmailSender Email = new SendSmtpEmailSender(SenderName, SenderEmail);
            string ToEmail = "[email protected]";
            string ToName = "Guilherme Vianna";
            SendSmtpEmailTo smtpEmailTo = new SendSmtpEmailTo(ToEmail, ToName);
            List<SendSmtpEmailTo> To = new List<SendSmtpEmailTo>();
            To.Add(smtpEmailTo);
            string BccName = "Janice Doe";
            string BccEmail = "[email protected]";
            SendSmtpEmailBcc BccData = new SendSmtpEmailBcc(BccEmail, BccName);
            List<SendSmtpEmailBcc> Bcc = new List<SendSmtpEmailBcc>();
            Bcc.Add(BccData);
            string CcName = "John Doe";
            string CcEmail = "[email protected]";
            SendSmtpEmailCc CcData = new SendSmtpEmailCc(CcEmail, CcName);
            List<SendSmtpEmailCc> Cc = new List<SendSmtpEmailCc>();
            Cc.Add(CcData);
            string HtmlContent = "<html><body><h1>This is my first transactional email {{params.parameter}}</h1></body></html>";
            string TextContent = null;
            string Subject = "My {{params.subject}}";
            string ReplyToName = "John Doe";
            string ReplyToEmail = "[email protected]";
            SendSmtpEmailReplyTo ReplyTo = new SendSmtpEmailReplyTo(ReplyToEmail, ReplyToName);
            string AttachmentUrl = null;
            string stringInBase64 = "aGVsbG8gdGhpcyBpcyB0ZXN0";
            byte[] Content = System.Convert.FromBase64String(stringInBase64);
            string AttachmentName = "test.txt";
            SendSmtpEmailAttachment AttachmentContent = new SendSmtpEmailAttachment(AttachmentUrl, Content, AttachmentName);
            List<SendSmtpEmailAttachment> Attachment = new List<SendSmtpEmailAttachment>();
            Attachment.Add(AttachmentContent);
            JObject Headers = new JObject();
            Headers.Add("Some-Custom-Name", "unique-id-1234");
            long? TemplateId = 3;
            JObject Params = new JObject();
            Params.Add("NOME", "Guilherme");
            Params.Add("SENHA", "Guilherme");
            List<string> Tags = new List<string>();
            Tags.Add("mytag");
            SendSmtpEmailTo1 smtpEmailTo1 = new SendSmtpEmailTo1(ToEmail, ToName);
            List<SendSmtpEmailTo1> To1 = new List<SendSmtpEmailTo1>();
            To1.Add(smtpEmailTo1);
            Dictionary<string, object> _parmas = new Dictionary<string, object>();
            _parmas.Add("params", Params);
            SendSmtpEmailReplyTo1 ReplyTo1 = new SendSmtpEmailReplyTo1(ReplyToEmail, ReplyToName);
            SendSmtpEmailMessageVersions messageVersion = new SendSmtpEmailMessageVersions(To1, _parmas, Bcc, Cc, ReplyTo1, Subject);
            List<SendSmtpEmailMessageVersions> messageVersiopns = new List<SendSmtpEmailMessageVersions>();
            messageVersiopns.Add(messageVersion);
            try
            {
                var sendSmtpEmail = new SendSmtpEmail(Email, To, Bcc, Cc, HtmlContent, TextContent, Subject, ReplyTo, Attachment, Headers, TemplateId, Params, messageVersiopns, Tags);
                CreateSmtpEmail result = apiInstance.SendTransacEmail(sendSmtpEmail);
                Debug.WriteLine(result.ToJson());
                Console.WriteLine(result.ToJson());
                Console.ReadLine();
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                Console.WriteLine(e.Message);
                Console.ReadLine();
            }
        }
    }

like image 606
Guilherme Vianna Avatar asked Sep 10 '25 03:09

Guilherme Vianna


2 Answers

I recently encountered the same problem while working with my PHP code. Initially, I assumed it was due to a mistake in my code. However, I later discovered that the issue was with my template. Specifically, if you have defined {{ first_name }} in your template, simply replace it with {{ params.first_name }} and your email will begin to display the value of the parameters that were sent through the API call.

like image 151
Kulwinder Kohli Avatar answered Sep 13 '25 06:09

Kulwinder Kohli


I encountered similar problem. I dont know why but when I set parameters for example as {{user.name}} {{user.email}} etc it wouldn't work.

Solution to that was setting parameters in template as {{params.user.name}}, {{params.user.email}} etc

and then pass into SendSmtpEmail instance(_params) as anonymus object

dynamic transactionalParams = new
{
    params = new
    {
        user = new
            {
                name = "JohnDoe",
                email= "[email protected]"
            }
    }
};

then when I sent email with specified template with that parameters it worked just fine

like image 23
Liandrel Avatar answered Sep 13 '25 05:09

Liandrel