/// <summary> /// Gets the HTML preview. /// </summary> /// <param name="communication">The communication.</param> /// <param name="person">The person.</param> /// <returns></returns> public override string GetHtmlPreview( Model.Communication communication, Person person ) { var rockContext = new RockContext(); // Requery the Communication object communication = new CommunicationService( rockContext ).Get( communication.Id ); var globalAttributes = Rock.Web.Cache.GlobalAttributesCache.Read(); var mergeValues = Rock.Web.Cache.GlobalAttributesCache.GetMergeFields( null ); if ( person != null ) { mergeValues.Add( "Person", person ); var recipient = communication.Recipients.Where( r => r.PersonId == person.Id ).FirstOrDefault(); if ( recipient != null ) { // Add any additional merge fields created through a report foreach ( var mergeField in recipient.AdditionalMergeValues ) { if ( !mergeValues.ContainsKey( mergeField.Key ) ) { mergeValues.Add( mergeField.Key, mergeField.Value ); } } } } string message = communication.GetChannelDataValue( "Message" ); return message.ResolveMergeFields( mergeValues ); }
/// <summary> /// Sends the specified communication. /// </summary> /// <param name="communication">The communication.</param> /// <exception cref="System.NotImplementedException"></exception> public override void Send( Rock.Model.Communication communication ) { var rockContext = new RockContext(); // Requery the Communication object communication = new CommunicationService( rockContext ).Get( communication.Id ); if ( communication != null && communication.Status == Model.CommunicationStatus.Approved && communication.Recipients.Where( r => r.Status == Model.CommunicationRecipientStatus.Pending ).Any() && (!communication.FutureSendDateTime.HasValue || communication.FutureSendDateTime.Value.CompareTo(RockDateTime.Now) <= 0)) { var globalAttributes = Rock.Web.Cache.GlobalAttributesCache.Read(); string fromAddress = communication.GetChannelDataValue( "FromAddress" ); string replyTo = communication.GetChannelDataValue( "ReplyTo" ); // Check to make sure sending domain is a safe sender var safeDomains = globalAttributes.GetValue( "SafeSenderDomains" ).SplitDelimitedValues().ToList(); var emailParts = fromAddress.Split( new char[] { '@' }, StringSplitOptions.RemoveEmptyEntries ); if (emailParts.Length != 2 || !safeDomains.Contains(emailParts[1],StringComparer.OrdinalIgnoreCase)) { if (string.IsNullOrWhiteSpace(replyTo)) { replyTo = fromAddress; } fromAddress = globalAttributes.GetValue("OrganizationEmail"); } // From MailMessage message = new MailMessage(); message.From = new MailAddress( fromAddress, communication.GetChannelDataValue( "FromName" ) ); // Reply To if ( !string.IsNullOrWhiteSpace( replyTo ) ) { message.ReplyToList.Add( new MailAddress( replyTo ) ); } // CC string cc = communication.GetChannelDataValue( "CC" ); if ( !string.IsNullOrWhiteSpace( cc ) ) { foreach ( string ccRecipient in cc.SplitDelimitedValues() ) { message.CC.Add( new MailAddress( ccRecipient ) ); } } // BCC string bcc = communication.GetChannelDataValue( "BCC" ); if ( !string.IsNullOrWhiteSpace( bcc ) ) { foreach ( string bccRecipient in bcc.SplitDelimitedValues() ) { message.Bcc.Add( new MailAddress( bccRecipient ) ); } } message.IsBodyHtml = true; message.Priority = MailPriority.Normal; var smtpClient = GetSmtpClient(); // Add Attachments string attachmentIds = communication.GetChannelDataValue( "Attachments" ); if ( !string.IsNullOrWhiteSpace( attachmentIds ) ) { var binaryFileService = new BinaryFileService( rockContext ); foreach(string idVal in attachmentIds.SplitDelimitedValues()) { int binaryFileId = int.MinValue; if (int.TryParse(idVal, out binaryFileId)) { var binaryFile = binaryFileService.Get(binaryFileId); if ( binaryFile != null ) { Stream stream = new MemoryStream( binaryFile.Data.Content ); message.Attachments.Add( new Attachment( stream, binaryFile.FileName ) ); } } } } var recipientService = new CommunicationRecipientService( rockContext ); var globalConfigValues = Rock.Web.Cache.GlobalAttributesCache.GetMergeFields( null ); bool recipientFound = true; while ( recipientFound ) { var recipient = recipientService.Get( communication.Id, CommunicationRecipientStatus.Pending ).FirstOrDefault(); if ( recipient != null ) { if ( string.IsNullOrWhiteSpace( recipient.Person.Email ) ) { recipient.Status = CommunicationRecipientStatus.Failed; recipient.StatusNote = "No Email Address"; } else { message.To.Clear(); message.Headers.Clear(); message.AlternateViews.Clear(); message.To.Add( new MailAddress( recipient.Person.Email, recipient.Person.FullName ) ); // Create merge field dictionary var mergeObjects = recipient.CommunicationMergeValues( globalConfigValues ); // Subject message.Subject = communication.Subject.ResolveMergeFields( mergeObjects ); // Add any additional headers that specific SMTP provider needs AddAdditionalHeaders( message, recipient ); // Add text view first as last view is usually treated as the preferred view by email readers (gmail) string plainTextBody = Rock.Communication.Channel.Email.ProcessTextBody( communication, globalAttributes, mergeObjects ); if ( !string.IsNullOrWhiteSpace( plainTextBody ) ) { AlternateView plainTextView = AlternateView.CreateAlternateViewFromString( plainTextBody, new ContentType( MediaTypeNames.Text.Plain ) ); message.AlternateViews.Add( plainTextView ); } // Add Html view string htmlBody = Rock.Communication.Channel.Email.ProcessHtmlBody( communication, globalAttributes, mergeObjects ); if ( !string.IsNullOrWhiteSpace( htmlBody ) ) { AlternateView htmlView = AlternateView.CreateAlternateViewFromString( htmlBody, new ContentType( MediaTypeNames.Text.Html ) ); message.AlternateViews.Add( htmlView ); } try { smtpClient.Send( message ); recipient.Status = CommunicationRecipientStatus.Delivered; string statusNote = StatusNote; if (!string.IsNullOrWhiteSpace(statusNote)) { recipient.StatusNote = statusNote; } recipient.TransportEntityTypeName = this.GetType().FullName; } catch ( Exception ex ) { recipient.Status = CommunicationRecipientStatus.Failed; recipient.StatusNote = "SMTP Exception: " + ex.Message; } } rockContext.SaveChanges(); } else { recipientFound = false; } } } }
/// <summary> /// Gets the HTML preview. /// </summary> /// <param name="communication">The communication.</param> /// <param name="person">The person.</param> /// <returns></returns> public override string GetHtmlPreview( Model.Communication communication, Person person ) { var rockContext = new RockContext(); StringBuilder sbContent = new StringBuilder(); var globalAttributes = Rock.Web.Cache.GlobalAttributesCache.Read(); var mergeValues = Rock.Web.Cache.GlobalAttributesCache.GetMergeFields( null ); // Requery the Communication object communication = new CommunicationService( rockContext ).Get( communication.Id ); mergeValues.Add( "Communication", communication ); if ( person != null ) { mergeValues.Add( "Person", person ); var recipient = communication.Recipients.Where( r => r.PersonId == person.Id ).FirstOrDefault(); if ( recipient != null ) { // Add any additional merge fields created through a report foreach ( var mergeField in recipient.AdditionalMergeValues ) { if ( !mergeValues.ContainsKey( mergeField.Key ) ) { mergeValues.Add( mergeField.Key, mergeField.Value ); } } } } // Body string htmlContent = communication.GetChannelDataValue( "HtmlMessage" ); sbContent.Append( Email.ProcessHtmlBody( communication, globalAttributes, mergeValues ) ); // Attachments StringBuilder sbAttachments = new StringBuilder(); string attachmentIds = communication.GetChannelDataValue( "Attachments" ); if ( !string.IsNullOrWhiteSpace( attachmentIds ) ) { sbContent.Append( "<br/><br/>" ); var binaryFileService = new BinaryFileService( rockContext ); foreach ( string idVal in attachmentIds.SplitDelimitedValues() ) { int binaryFileId = int.MinValue; if ( int.TryParse( idVal, out binaryFileId ) ) { var binaryFile = binaryFileService.Get( binaryFileId ); if ( binaryFile != null ) { sbContent.AppendFormat( "<a target='_blank' href='{0}GetFile.ashx?id={1}'>{2}</a><br/>", System.Web.VirtualPathUtility.ToAbsolute( "~" ), binaryFile.Id, binaryFile.FileName ); } } } } return sbContent.ToString(); }
/// <summary> /// Sends the specified communication. /// </summary> /// <param name="communication">The communication.</param> /// <exception cref="System.NotImplementedException"></exception> public override void Send( Rock.Model.Communication communication ) { var rockContext = new RockContext(); // Requery the Communication communication = new CommunicationService( rockContext ).Get( communication.Id ); if ( communication != null && communication.Status == Model.CommunicationStatus.Approved && communication.Recipients.Where( r => r.Status == Model.CommunicationRecipientStatus.Pending ).Any() && ( !communication.FutureSendDateTime.HasValue || communication.FutureSendDateTime.Value.CompareTo( RockDateTime.Now ) <= 0 ) ) { string fromPhone = string.Empty; string fromValue = communication.GetChannelDataValue( "FromValue" ); int fromValueId = int.MinValue; if ( int.TryParse( fromValue, out fromValueId ) ) { fromPhone = DefinedValueCache.Read( fromValueId ).Value; } if ( !string.IsNullOrWhiteSpace( fromPhone ) ) { string accountSid = GetAttributeValue( "SID" ); string authToken = GetAttributeValue( "Token" ); var twilio = new TwilioRestClient( accountSid, authToken ); var recipientService = new CommunicationRecipientService( rockContext ); var globalConfigValues = GlobalAttributesCache.GetMergeFields( null ); bool recipientFound = true; while ( recipientFound ) { var recipient = recipientService.Get( communication.Id, CommunicationRecipientStatus.Pending ).FirstOrDefault(); if ( recipient != null ) { try { var phoneNumber = recipient.Person.PhoneNumbers .Where( p => p.IsMessagingEnabled ) .FirstOrDefault(); if ( phoneNumber != null ) { // Create merge field dictionary var mergeObjects = recipient.CommunicationMergeValues( globalConfigValues ); string message = communication.GetChannelDataValue( "Message" ); message = message.ResolveMergeFields( mergeObjects ); string twilioNumber = phoneNumber.Number; if ( !string.IsNullOrWhiteSpace( phoneNumber.CountryCode ) ) { twilioNumber = "+" + phoneNumber.CountryCode + phoneNumber.Number; } var response = twilio.SendMessage( fromPhone, twilioNumber, message ); recipient.Status = CommunicationRecipientStatus.Delivered; recipient.TransportEntityTypeName = this.GetType().FullName; recipient.UniqueMessageId = response.Sid; } else { recipient.Status = CommunicationRecipientStatus.Failed; recipient.StatusNote = "No Phone Number with Messaging Enabled"; } } catch ( Exception ex ) { recipient.Status = CommunicationRecipientStatus.Failed; recipient.StatusNote = "Twilio Exception: " + ex.Message; } rockContext.SaveChanges(); } else { recipientFound = false; } } } } }