Skip to content

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

Open

Description

@RunnicFusion

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?

Activity

  1. kjac commented on Nov 28, 2018

    @kjac
    Owner

    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.

  2. RunnicFusion commented on Dec 4, 2018

    @RunnicFusion
    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);
            }
    
    
  3. RunnicFusion commented on Dec 5, 2018

    @RunnicFusion
    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));
                        }                
                    }
                }
            }
        }
    
    
    
  4. kjac commented on Dec 7, 2018

    @kjac
    Owner

    @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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions