Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement/how to: Use form placeholders in notification recipient and sender email address #198

Open
RunnicFusion opened this issue Nov 20, 2018 · 4 comments

Comments

@RunnicFusion
Copy link

Maybe it's a good idea to add the possibility to add support for the usages of Form values as Recipient adres and sender e-mail for the notification e-mail. Right now only the Email subject of the notification mail can be set.

For a client I need to change the recipient address dynamically. Could you tell me how to do this?

@kjac
Copy link
Owner

kjac commented Nov 28, 2018

Hi @RunnicFusion.

Sorry for the late response. You can hook into FormModel.BeforeSendMail to manipulate the MailMessage object before it's sent.

Check #146 for inspiration. The code sample isn't exactly what you're looking for, but it should get you going in the right direction.

@RunnicFusion
Copy link
Author

Hi kjac,

Thank you for your reply. I just tested it out on our project and found the following:

  • I want to change the notification address on the fly, the correct value is setup in a string which is loaded from a cookie
  • When I hookup the BeforeSendMail event I can change the mailmessage, but I can't seem to find a way to just change the internal (notification) e-mail instead on both e-mails.
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            // Form editor change notification email
            FormModel.BeforeSendMail += FormModelOnBeforeSendMail; 
        }

        private void FormModelOnBeforeSendMail(FormModel sender, FormEditorMailCancelEventArgs args)
        {
            // Not working
            //FormModel.BeforeSendMail += (o, e) =>//RoutedEventHandler
            //{
            //    o.EmailNotificationRecipients = rayonEmail;
            //};
        

            // set the from address on the email
            string rayonEmail = UmbracoLite.Controllers.API.RegionController.StaticGet() != null ? UmbracoLite.Controllers.API.RegionController.StaticGet().TeamMemberEmail : null;


            args.MailMessage.DeliveryNotificationOptions = new MailAddress(rayonEmail);
        }

@RunnicFusion
Copy link
Author

Found a way to:

  • Only send the notification email to other address
  • Only do this when a specific form field is found
  • Only do this when the override field is not empty
    public class UmbracoEvents : ApplicationEventHandler
    {
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            // Form editor change notification email
            FormModel.BeforeSendMail += FormModelOnBeforeSendMail; 
        }

        private void FormModelOnBeforeSendMail(FormModel sender, FormEditorMailCancelEventArgs args)
        {
            // set the from address on the email
            string rayonEmail = UmbracoLite.Controllers.API.RegionController.StaticGet() != null ? UmbracoLite.Controllers.API.RegionController.StaticGet().TeamMemberEmail : null;
            // Only the notification e-mail
            if(args.EmailType == "notification")
            {
                // Only change the notification receivers when field rayon is found in form
                if (!string.IsNullOrEmpty(rayonEmail) && sender.AllValueFields().Where(n => n.Name.ToLower().Trim() == "rayon" && n.Type.ToLower() == "custom.objectinfo").Count() == 1)
                {
                    var oldAddresses = args.MailMessage.To.ToList();                    
                    if (oldAddresses.Count() > 0)
                    {
                        // Remove other enteries
                        foreach (var address in oldAddresses)
                        {
                            args.MailMessage.To.Remove(address); // delete old value from to list
                            args.MailMessage.CC.Add(address); // set old value as cc list
                        }
                        // Set rayon / regio as name
                        args.MailMessage.To.Add(new MailAddress(rayonEmail));
                    }                
                }
            }
        }
    }


@kjac
Copy link
Owner

kjac commented Dec 7, 2018

@RunnicFusion awesome 😄

Looks like you found the EmailType property on the FormEditorMailCancelEventArgs 👍

Can this issue be closed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants