/// <summary> /// Executes the specified workflow. /// </summary> /// <param name="rockContext">The rock context.</param> /// <param name="action">The action.</param> /// <param name="entity">The entity.</param> /// <param name="errorMessages">The error messages.</param> /// <returns></returns> public override bool Execute(RockContext rockContext, WorkflowAction action, object entity, out List <string> errorMessages) { errorMessages = new List <string>(); var mergeFields = GetMergeFields(action); string to = GetAttributeValue(action, AttributeKey.To); string fromValue = GetAttributeValue(action, AttributeKey.From); string replyTo = GetAttributeValue(action, AttributeKey.ReplyTo); string subject = GetAttributeValue(action, AttributeKey.Subject); string body = GetAttributeValue(action, AttributeKey.Body); string cc = GetActionAttributeValue(action, AttributeKey.Cc); string bcc = GetActionAttributeValue(action, AttributeKey.Bcc); var attachmentOneGuid = GetAttributeValue(action, AttributeKey.AttachmentOne, true).AsGuid(); var attachmentTwoGuid = GetAttributeValue(action, AttributeKey.AttachmentTwo, true).AsGuid(); var attachmentThreeGuid = GetAttributeValue(action, AttributeKey.AttachmentThree, true).AsGuid(); var attachmentList = new List <BinaryFile>(); if (!attachmentOneGuid.IsEmpty()) { attachmentList.Add(new BinaryFileService(rockContext).Get(attachmentOneGuid)); } if (!attachmentTwoGuid.IsEmpty()) { attachmentList.Add(new BinaryFileService(rockContext).Get(attachmentTwoGuid)); } if (!attachmentThreeGuid.IsEmpty()) { attachmentList.Add(new BinaryFileService(rockContext).Get(attachmentThreeGuid)); } var attachments = attachmentList.ToArray(); bool createCommunicationRecord = GetAttributeValue(action, AttributeKey.SaveCommunicationHistory).AsBoolean(); string fromEmailAddress = string.Empty; string fromName = string.Empty; Guid? fromGuid = fromValue.AsGuidOrNull(); if (fromGuid.HasValue) { var attribute = AttributeCache.Get(fromGuid.Value, rockContext); if (attribute != null) { string fromAttributeValue = action.GetWorkflowAttributeValue(fromGuid.Value); if (!string.IsNullOrWhiteSpace(fromAttributeValue)) { if (attribute.FieldType.Class == "Rock.Field.Types.PersonFieldType") { Guid personAliasGuid = fromAttributeValue.AsGuid(); if (!personAliasGuid.IsEmpty()) { var person = new PersonAliasService(rockContext).Queryable() .Where(a => a.Guid.Equals(personAliasGuid)) .Select(a => a.Person) .FirstOrDefault(); if (person != null && !string.IsNullOrWhiteSpace(person.Email)) { fromEmailAddress = person.Email; fromName = person.FullName; } } } else { fromEmailAddress = fromAttributeValue; } } } } else { fromEmailAddress = fromValue.ResolveMergeFields(mergeFields); } string replyToEmailAddress = string.Empty; Guid? replyToGuid = replyTo.AsGuidOrNull(); // If there is a "Reply To" value for the attribute, use that to get the "Reply To" email. if (replyToGuid.HasValue) { var attribute = AttributeCache.Get(replyToGuid.Value, rockContext); if (attribute != null) { string replyToAttributeValue = action.GetWorkflowAttributeValue(replyToGuid.Value); if (!string.IsNullOrWhiteSpace(replyToAttributeValue)) { if (attribute.FieldType.Class == "Rock.Field.Types.PersonFieldType") { Guid personAliasGuid = replyToAttributeValue.AsGuid(); if (!personAliasGuid.IsEmpty()) { var personEmail = new PersonAliasService(rockContext).Queryable() .Where(a => a.Guid.Equals(personAliasGuid)) .Select(a => a.Person.Email) .FirstOrDefault(); if (personEmail.IsNotNullOrWhiteSpace()) { replyToEmailAddress = personEmail; } } } else { replyToEmailAddress = replyToAttributeValue; } } } } else { replyToEmailAddress = replyTo.ResolveMergeFields(mergeFields); } // To Email recipients list. if (GetEmailsFromAttributeValue(RecipientType.SendTo, to, action, mergeFields, rockContext, out string toDelimitedEmails, out List <RockEmailMessageRecipient> toRecipients)) { // CC emails recipients list. GetEmailsFromAttributeValue(RecipientType.CC, cc, action, mergeFields, rockContext, out string ccDelimitedEmails, out List <RockEmailMessageRecipient> ccRecipients); List <string> ccEmails = BuildEmailList(ccDelimitedEmails, mergeFields, ccRecipients); // BCC emails recipients list. GetEmailsFromAttributeValue(RecipientType.BCC, bcc, action, mergeFields, rockContext, out string bccDelimitedEmails, out List <RockEmailMessageRecipient> bccRecipients); List <string> bccEmails = BuildEmailList(bccDelimitedEmails, mergeFields, bccRecipients); if (!string.IsNullOrWhiteSpace(toDelimitedEmails)) { Send(toDelimitedEmails, fromEmailAddress, fromName, replyToEmailAddress, subject, body, ccEmails, bccEmails, mergeFields, createCommunicationRecord, attachments, out errorMessages); } else if (toRecipients != null) { Send(toRecipients, fromEmailAddress, fromName, replyToEmailAddress, subject, body, ccEmails, bccEmails, createCommunicationRecord, attachments, out errorMessages); } } errorMessages.ForEach(m => action.AddLogEntry(m, true)); return(true); }