public async Task SendEmailWithDocumentPersonalisationTest()
        {
            byte[] pdfContents;

            try
            {
                pdfContents = File.ReadAllBytes("../../../IntegrationTests/test_files/one_page_pdf.pdf");
            }
            catch (DirectoryNotFoundException)
            {
                pdfContents = File.ReadAllBytes("IntegrationTests/test_files/one_page_pdf.pdf");
            }

            Dictionary <String, dynamic> personalisation = new Dictionary <String, dynamic>
            {
                { "name", NotificationClient.PrepareUpload(pdfContents) }
            };

            EmailNotificationResponse response =
                await this.client.SendEmailAsync(FUNCTIONAL_TEST_EMAIL, EMAIL_TEMPLATE_ID, personalisation);

            Assert.IsNotNull(response.id);
            Assert.IsNotNull(response.template.id);
            Assert.IsNotNull(response.template.uri);
            Assert.IsNotNull(response.template.version);
            Assert.AreEqual(response.content.subject, TEST_EMAIL_SUBJECT);
            Assert.IsTrue(response.content.body.Contains("https://documents."));
        }
        public void SendEmailNotificationWithDocumentGeneratesExpectedRequest()
        {
            Dictionary <string, dynamic> personalisation = new Dictionary <string, dynamic>
            {
                { "document", NotificationClient.PrepareUpload(Encoding.UTF8.GetBytes("%PDF-1.5 testpdf")) }
            };
            JObject expected = new JObject
            {
                { "email_address", Constants.fakeEmail },
                { "template_id", Constants.fakeTemplateId },
                { "personalisation", new JObject
                  {
                      { "document", new JObject
                        {
                            { "file", "JVBERi0xLjUgdGVzdHBkZg==" }
                        } }
                  } },
                { "reference", Constants.fakeNotificationReference }
            };

            MockRequest(Constants.fakeTemplatePreviewResponseJson,
                        client.SEND_EMAIL_NOTIFICATION_URL,
                        AssertValidRequest,
                        HttpMethod.Post,
                        AssertGetExpectedContent, expected.ToString(Formatting.None));

            EmailNotificationResponse response = client.SendEmail(Constants.fakeEmail, Constants.fakeTemplateId, personalisation, Constants.fakeNotificationReference);
        }
 public void PrepareUploadWithLargeDocumentGeneratesAnError()
 {
     Assert.That(
         () => { NotificationClient.PrepareUpload(new byte[3 * 1024 * 1024]); },
         Throws.ArgumentException
         );
 }
Beispiel #4
0
        /// <summary>
        /// Send Email with specified template using template data
        /// </summary>
        /// <param name="EmailTo">To email id</param>
        /// <param name="TemplateId">Gov Notifier Template Id</param>
        /// <param name="TemplateData">key value pair of the data in template</param>
        /// <returns></returns>

        public static void SendEmail(GovNotifierEmailPdfInParams inparam)
        {
            logger.Debug($@"Inside SendEmail for Id = {inparam.Id}");
            StringBuilder DebugInfo = new StringBuilder("Initiated for Pdf generation.\r\n");

            try
            {
                Data.StatusUpdate(inparam.Id, "2", "In progress", DebugInfo.Append("Inside SendEmail to start Generating pdf.\r\n").ToString());
                logger.Debug($@"In progress with for GeneratePdfDocument");
                Document document = new BuildDoc().GeneratePdfDocument(inparam.ContactId, inparam.TenancyAgreementRef, inparam.StartDate);

                if (document != null)
                {
                    Data.StatusUpdate(inparam.Id, "3", "Document Created", DebugInfo.Append("Document created.\r\n").ToString());
                    logger.Debug($@"Document Created");
                    byte[] docbytes = document.Draw();
                    logger.Debug($@"Document Size = {docbytes.Length}");
                    NotificationClient client = new NotificationClient(_apiKey);
                    var TemplateDataDict      = JsonConvert.DeserializeObject <Dictionary <string, string> >(inparam.TemplateData);
                    logger.Debug($@"Creating Personalization");
                    Dictionary <string, dynamic> personalisation = new Dictionary <string, dynamic>();
                    foreach (KeyValuePair <string, string> pair in TemplateDataDict)
                    {
                        personalisation.Add(pair.Key.ToString(), pair.Value.ToString());
                        logger.Debug($@"Adding Personalization for key = {pair.Key.ToString()} value = {pair.Value.ToString()}");
                    }
                    personalisation.Add("link_to_document", NotificationClient.PrepareUpload(docbytes));
                    logger.Debug($@"Linked document to template");
                    Data.StatusUpdate(inparam.Id, "4", "Sending email", DebugInfo.Append("Emailing with pdf attachment.\r\n").ToString());

                    EmailNotificationResponse response = client.SendEmail(inparam.EmailTo, inparam.TemplateId, personalisation);
                    logger.Debug($@"Email Sent successfully to Gov Notifier with attachments {response.id}");
                    Data.StatusUpdate(inparam.Id, "0", "Email Sent Successfully", DebugInfo.Append("Completed.\r\n").ToString());
                }
            }
            catch (Exception ex)
            {
                Data.StatusUpdate(inparam.Id, "-1", "Error Occurred", DebugInfo.Append("Error occured : \r\n").ToString() + ex.Message);
                logger.Error(ex, "Error occured in Send Email $$$$$ " + ex.Message);
            }
        }