In light of the release of Form Wizard Pro 2.6, and its capability to process custom form submission handlers, I was wondering if I could quickly build something that would send a copy of a Form Wizard Pro submission to the person completing the form (in other words, where the "To" e-mail address is not known beforehand).
Modeling my code off of what's delineated at http://www.mojoportal.com/implementing-a-custom-form-submission-handler.aspx, here's what I came up with (in case it saves somebody a few minute's work ). Alternatively, see http://code.colostate.edu/implementing-a-custom-form-submission-email-handler.aspx for the zipped VS solution:
class MojoFWPMailer : FormSubmissionHandlerProvider
{
private static readonly ILog log = LogManager.GetLogger(typeof(MojoFWPMailer));
//Custom properties
private string _FromEmailAddress = "YOUR_EMAIL_ADDRESS";
private string _ToEmail;
private string _EmailQuestionText = "EMAIL";
private string _Subject = "Form Submission Received";
public MojoFWPMailer()
{ }
public override void FormSubmittedEventHandler(object sender, FormSubmissionEventArgs e)
{
if (e == null) return;
if (e.ResponseSet == null) return;
log.Info("MojoFWPMailerFormSubmissionHandlerProvider called");
StringBuilder results = new StringBuilder();
//how to get the site user if the user was authenticated
if (e.User != null)
{
results.Append("submitted by user: " + e.User.Name);
results.Append("\r\n");
}
//how to get the questions and answers
List<WebFormQuestion> questionList = WebFormQuestion.GetByForm(e.ResponseSet.FormGuid);
List<WebFormResponse> responses = WebFormResponse.GetByResponseSet(e.ResponseSet.Guid);
foreach (WebFormQuestion question in questionList)
{
if (question.QuestionTypeId == 8) { continue; } //skip instruction block
string response = GetResponse(e.ResponseSet.Guid, question.Guid, responses);
//Try to extract e-mail address
if (question.QuestionText.ToUpper().Contains(_EmailQuestionText))
{
_ToEmail = response;
}
results.Append("\r\n" + question.QuestionText + "\r\n");
results.Append(response);
results.Append("\r\n");
}
// how to send an email with the results and file attachments
// you could get settings from config settings if you don't want to hard code it
// you would need references to mojoPortal.Web.dll, mojoPortal.Business.dll, and mojoPortal.Net.dll
// to use this commented code
string fromAddress = _FromEmailAddress;
string emailTo = _ToEmail;
string subject = _Subject;
Email.Send(
SiteUtils.GetSmtpSettings(),
fromAddress,
string.Empty,
string.Empty,
emailTo,
string.Empty,
string.Empty,
subject,
results.ToString(),
false,
Email.PriorityNormal);
log.Info(results.ToString());
}
private string GetResponse(Guid responseSetGuid, Guid questionGuid, List<WebFormResponse> responses)
{
foreach (WebFormResponse response in responses)
{
if (
(response.ResponseSetGuid == responseSetGuid)
&& (response.QuestionGuid == questionGuid)
)
{
return response.Response;
}
}
return string.Empty;
}
}