0

I've been going thumbing through the documentation and searching the internet to find documenation on how to add attachments to created templates. I'm using darrencauthon's CSharp-Sparkpost to handle the API calls. So far what I have is not working. Does anyone have a working solution (possible?) or a better solution for C#? I'm not opposed to using a different library. This is the link to CSharp-Sparkpost

Here's what I've got so far:

var t = new Transmission();

t.Content.From.Email = "from@thisperson.com";
t.Content.TemplateId = "my-template-email";

new Recipient
{
    Address = new Address { Email = recipient }
}
.Apply(t.Recipients.Add);


new Attachment
{
    Data = //CSVDATA,
    Name = "Table.csv",
    Type = "text/csv"
}.Apply(t.Content.Attachments.Add);


var client = new SparkPost.Client(Util.GetPassword("sparkpostapikey"));
client.Transmissions.Send(t).Wait();

I've verified that I can send this attachment without a template and also verified that I can send this template without the attachment. So... the Email is getting sent; however, the content received is only the template and substitution data. No attachment with the template email.

Fus Ro Dah
  • 331
  • 5
  • 22

3 Answers3

2

Using Darren's library, and combining the requirements for my project, this is the solution I've come up with. I'm just making an additional API call to grab the template Html so I can build the transmission without having to send the template_id. Still using the CSharp-Sparkpost library to make all of the calls. I modified Darren's example SendInline program as such:

    static async Task ExecuteEmailer()
    {
        var settings = ConfigurationManager.AppSettings;
        var fromAddr = settings["fromaddr"];
        var toAddr = settings["toaddr"];

        var trans = new Transmission();

        var to = new Recipient
        {
            Address = new Address
            {
                Email = toAddr
            },
            SubstitutionData = new Dictionary<string, object>
            {
                {"firstName", "Stranger"}
            }
        };

        trans.Recipients.Add(to);

        trans.SubstitutionData["firstName"] = "Sir or Madam";

        trans.Content.From.Email = fromAddr;
        trans.Content.Subject = "SparkPost sending attachment using template";
        trans.Content.Text = "Greetings {{firstName or 'recipient'}}\nHello from C# land.";

        //Add attachments to transmission object
        trans.Content.Attachments.Add(new Attachment()
        {
            Data = Convert.ToBase64String(System.IO.File.ReadAllBytes(@"C:\PathToFile\ExcelFile.xlsx")),
            Name = "ExcelFile.xlsx",
            Type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
        });

        Console.Write("Sending mail...");

        var client = new Client(settings["apikey"]);
        client.CustomSettings.SendingMode = SendingModes.Sync;

        //retrieve template html and set Content.Html
        var templateResponse = await client.Templates.Retrieve("template-email-test");
        trans.Content.Html = templateResponse.TemplateContent.Html;

        //Send transmission 
        var response = client.Transmissions.Send(trans);

        Console.WriteLine("done");
    }

Template with attached file

Fus Ro Dah
  • 331
  • 5
  • 22
1

I'm Darren Cauthon, the primary author of this library.

I have attachment support in my acceptance tests, which are run before each release. The link is below, but the code should be as simple as:

// C#

var attachment = File.Create<Attachment>("testtextfile.txt");
transmission.Content.Attachments.Add(attachment);

https://github.com/darrencauthon/csharp-sparkpost/blob/3a8cb1efbb8c9a0448c71c126ce7f88759867fb0/src/SparkPost.Acceptance/TransmissionSteps.cs#L56

the_storyteller
  • 2,335
  • 1
  • 26
  • 37
Darren
  • 987
  • 5
  • 6
1

Oh actually, I see now -- you are talking about adding attachments to templates, not attachments.

My answer to that is that back when I developed this library, attachment on templates was not supported by SparkPost itself.

My library allows you to try it, but that's because every template and non-template emails are considered "transmissions." So if you create a transmission, it has the option of adding attachments... but if you send the transmission with a template id, the attachment is ignored.

I could throw an error, or somehow design the API around this limitation, but what if they stopped ignoring the attachment but my library threw an error? I designed the library for flexibility as the SparkPost web API grew, and I didn't want my library to get in the way.

If you want to test if you're sending the attachment right, send your transmission without a transmission id, and instead with a subject and email body. If the email goes through and you get an attachment, then you know it's because of this template/attachment restriction from SparkPost.

NOTE: I'm putting this answer on Stack Overflow, and it's possible that this dated message will no longer be valid in the future.

Darren
  • 987
  • 5
  • 6
  • I just verified on my end, and the restriction is still in place. – Darren Jun 26 '17 at 16:33
  • Here is a link to the test scenario. Everything is the same as the previous attachment test, except that a template-id is passed instead of a subject/body. And the email will send, but no attachment will be included. This tells me that it is an issue with SparkPost, not the library. https://github.com/darrencauthon/csharp-sparkpost/pull/147/files#diff-0736aecbf66547ac271ced9c212cede1R32 – Darren Jun 26 '17 at 16:43
  • Thanks for looking into this again. I've put together a work around now knowing the limitations of the current state of the Sparkpost API. I'm requesting a `GET`, extracting the html for the template, then using your library like : `transmission.Content.Html = htmlFromGet` – Fus Ro Dah Jun 26 '17 at 19:19
  • I could just as easily get the Html to replace from any other source, I just had some templates there and it wasn't hard to hook in a request call to the api to retrieve them. – Fus Ro Dah Jun 26 '17 at 19:21