How do I check for spam before sending a message (contact form)?

Topics: Core, Customizing Orchard, Writing modules
Jan 4, 2014 at 2:52 PM
Edited Jan 4, 2014 at 2:53 PM
I have a contact form on my Orchard powered website. Via a ContactViewModel I get all the data the user has filled into the form. Before sending the message I would like to check whether it is spam. For now I would like to avoid having the user fill out a recaptcha. I would like to use the Akismet and TypePad spam filters.

I have looked into the Orchard.AntiSpam module. It seems like I should use the CheckForSpam method. Do I need to create a ContentItem to which I attach the SpamFilterPart? Or should I directly create a CommentCheckContext? (In the DefaultSpamService the SpamFilterPart is converted to a CommentCheckContext.) Or is there an easier method I have overlooked?

Here is the controller action where I would like to insert the spam check:
public ActionResult Contact(ContactViewModel emailProperties) {
    if (!ModelState.IsValid) {
        return View("Contact", emailProperties);
    }

     // check emailProperties for spam, especially emailProperties.Message

    var recipient = GetPortalOwner();
    var mailProperties = new Dictionary<string, string>
                                {
                                    { "FirstName", emailProperties.FirstName },
                                    { "Surname", emailProperties.Surname },
                                    { "Email", emailProperties.Email },
                                    { "Subject", emailProperties.Subject },
                                    { "Message", emailProperties.Message }
                                };
    _messageManager.Send(recipient.ContentItem.Record, EmailTypes.Contact, "email", mailProperties);
    return RedirectToAction("Contact");
}
Coordinator
Jan 5, 2014 at 1:01 AM
If you use custom forms, instead of rolling out your own, it's as simple as adding a part to the form's content type. Here is an example I implemented exactly this way: http://nwazet.com/contact
Jan 6, 2014 at 9:38 AM
If I understand correctly, Bertrand, you have a content type which represents your contact form. And you added a ReCaptchaPart to this content type.

Is it possible to use the functionality of the Orchard.AntiSpam module (checking whether a message is spam) without creating a content type for the form?
Coordinator
Jan 7, 2014 at 7:18 AM
Why don't you want to create a type for the form? It's possible to use pretty much anything in Orchard through your own code, yes, but it seems easier to just use custom forms, as that feature is built exactly for that type of application.
Jan 7, 2014 at 2:34 PM
Thanks for your suggestions, Bertrand. I was not aware of the Custom Forms module. I looked into it.

In the end I implemented a honeypot solution to trap bots, which was the easiest for me to implement.