Email Module - sending to multiple recipients

Topics: Customizing Orchard, Writing modules
Sep 22, 2011 at 8:01 PM

I created a "contact us" widget that allows the user to specify the recipient's email address to which all inquiries will be sent.  So far it works if I specify only 1 email address in the text field.  What I want to do is allow the user to enter multiple email addresses separated by a ";".  When the system prepares the message, it will parse the string and add the multiple recipients.  From looking at the code, I can modify the EmailMessageEventHandler to something like this:

        public void Sending(MessageContext context) {
            var contentItem = _contentManager.Get(context.Recipient.Id);
            if ( contentItem == null )
                return;

            var recipient = contentItem.As<IUser>();
            if ( recipient == null )
                return;

            if (recipient.Email.Contains(";"))
            {
                string[] emails = recipient.Email.Split(new char[] { ';' }, System.StringSplitOptions.RemoveEmptyEntries);
                foreach (string e in emails)
                {
                    context.MailMessage.To.Add(e);
                }
            }
            else 
                context.MailMessage.To.Add(recipient.Email);
        }
Is there a better way to do this?  I don't really like the idea of modifying the Email Module.

Coordinator
Sep 22, 2011 at 8:06 PM

Why don't you emit multiple messages?

Sep 22, 2011 at 8:20 PM
Edited Sep 22, 2011 at 8:21 PM

I thought about that am not sure on how to do this.  My controller handles the POST from the ContactUS Form, retrieves the ContentPart record and sends it to the IMessageManager.  See below:

        public ActionResult Send(int partId, string ReturnUrl)
        {
            var data = this.contentManager.Query<ContactUsPart, ContactUsPartRecord>()
                .Where(p => p.Id == partId)
                .List()
                .FirstOrDefault();

            Dictionary<string, string> content = new Dictionary<string, string>();
            
            //code to build content dictionary

            this.messageManager.Send(data.ContentItem.Record, "CONTACT_REQUEST", "email", content);

            return this.RedirectLocal("~/thank-you", "~/");
        }

I thought about creating a loop to call Messagemanager.Send() multiple times by building the ContentItem dynamically and passing it to MessageManager.  But the EmailMessageEventHandler retrieves the ContentItem using the PartId, something that my dynamically created ContentItem doesn't have.

I don't have a good grasp of the ContentManager/ContentItem concept so please correct me if I got this all wrong.

Thanks.

Coordinator
Sep 22, 2011 at 9:09 PM

You could clone what you need into a new part record and loop over that send.

Sep 22, 2011 at 9:44 PM

My ContactUs module does not save any records to the database, it only stores the recipient email address.  So I'm not sure how to create a "new part record".  Could you provide a simple example?

Thanks.

Coordinator
Sep 22, 2011 at 9:54 PM

new ContactUsPartRecord?