private string GetNotificationAlbumText(NotifySubscribersInput input, string unsubscribeArtistUrl, string unsubscribeAllUrl) { string text = $"Good news, {input.Artist.Name} just released a new album: {input.Album.Name}.\r\n\r\n" + $"Click the following link to listen: {Album.GetSpotifyUrl(input.Album.SpotifyId)}\r\n\r\n\r\n\r\n" + $"To unsubscribe from new albums for this artist, click the following link: {unsubscribeArtistUrl}\r\n" + $"To unsubscribe from all new albums from us, click the following link: {unsubscribeAllUrl}\r\n" + _templateManager.GetEmailTextFooter(); return(text); }
private async Task <string> GetNotificationAlbumHtml(NotifySubscribersInput input, string unsubscribeArtistUrl, string unsubscribeAllUrl) { string albumImageWidth = "280"; var getTemplateInput = new GetTemplateInput { TemplateType = TemplateTypes.Alert, SimpleVariableValues = new Dictionary <string, string> { { TemplateVariables.Heading, $"New album from {input.Artist.Name}" } }, BodyParagraphs = new List <BodyParagraph> { new BodyParagraph { HtmlText = $"Good news, {input.Artist.Name} just released a new album: {HttpUtility.HtmlEncode(input.Album.Name)}." }, new BodyParagraph { HtmlText = $"<img alt=\"{HttpUtility.HtmlEncode(input.Album.Name)}\" src=\"{input.Album.Image.Url}\" width=\"{albumImageWidth}\" height=\"{albumImageWidth}\" />" }, new BodyParagraph { HtmlText = "Click Here to Listen", ButtonUrl = $"{Album.GetSpotifyUrl(input.Album.SpotifyId)}" } }, FooterLines = new List <FooterLine> { new FooterLine { HtmlText = _templateManager.GetEmailLink(unsubscribeArtistUrl, "Unsubscribe", null, "12px") + " from new albums for this artist." }, new FooterLine { HtmlText = _templateManager.GetEmailLink(unsubscribeAllUrl, "Unsubscribe", null, "12px") + " from all new albums from us." } } }; var template = await _templateManager.GetHtmlEmailTemplate(getTemplateInput); return(template.ToString()); }
public async Task <NotifySubscribersOutput> NotifySubscribers(NotifySubscribersInput input) { if (input.Album == null) { throw new ArgumentException("Album must not be null", "Album"); } if (input.Artist == null) { throw new ArgumentException("Artist must not be null", "Artist"); } try { foreach (var subscription in input.Subscriptions) { var subscriberDto = await _crudServices.ReadSingleAsync <SubscriberDto>(s => s.Id == subscription.SubscriberId); if (subscriberDto == null) { Logger.LogError("Subscriber with Id {0} not found in database", subscription.SubscriberId); continue; } if (!subscriberDto.EmailAddressVerified) { Logger.LogInformation("Skipping notification email for Subscriber with Id {0}, email address not verified", subscription.SubscriberId); continue; } Logger.LogInformation("Sending notification to {0} for SpotifyAlbumId: {1}", subscriberDto.EmailAddress, input.Album.SpotifyId); string unsubscribeFromArtistUrl = GetUnsubscribeUrl(subscriberDto.UnsubscribeToken, subscription.ArtistId); string unsubscribeFromAllUrl = GetUnsubscribeUrl(subscriberDto.UnsubscribeToken, null); string emailText = GetNotificationAlbumText(input, unsubscribeFromArtistUrl, unsubscribeFromAllUrl); string emailHtml = await GetNotificationAlbumHtml(input, unsubscribeFromArtistUrl, unsubscribeFromAllUrl); var email = new EmailMessage { BodyHtml = emailHtml, BodyText = emailText, Subject = $"New album from {input.Artist.Name}", ToAddresses = new List <EmailAddress> { new EmailAddress { Address = subscriberDto.EmailAddress } } }; await _emailManager.SendEmail(email); } return(new NotifySubscribersOutput()); } catch (Exception ex) { Logger.LogError(ex, "SpotifyAlbumId: " + input.Album.SpotifyId); return(new NotifySubscribersOutput { ErrorMessage = ex.Message }); } }