Send a confirmation email with FormIt
As example we will be using a simple contact form that holds 4 fields: name, email, subject and message. On submit FormIt will send 2 different emails, one to you and one to the user.
In order to send a confirmation email with FormIt, we'll need to create a new chunk and a new snippet. The chunk will be the template of the confirmation email and the snippet will make use of this template, replace any placeholders and send the email to the user.
The FormIt call and the Form
[[!FormIt?
&hooks=`spam,email,emailUser,redirect`
&emailTo=`YOUR-EMAIL`
&emailSubject=`CONTACT FORM: [[+subject]]`
&emailFromName=`[[+name]]`
&emailTpl=`ContactTpl`
&redirectTo=`6`
]]
<div class="error">[[+fi.error.error_message]]</div>
<form action="[[~[[*id]]]]" method="post" class="form">
<input type="hidden" name="nospam:blank" value="" />
<div class="formRow">
<label for="name">Name: </label>
<input type="text" name="name:required" id="name" value="[[+fi.name]]" />
<span class="error">[[+fi.error.name]]</span>
</div>
<div class="formRow">
<label for="name">Email: </label>
<input type="text" name="email:email:required" id="email" value="[[+fi.email]]" />
<span class="error">[[+fi.error.email]]</span>
</div>
<div class="formRow">
<label for="subject">Subject: </label>
<input type="text" name="subject:required:stripTags" id="subject" value="[[+fi.subject]]" />
<span class="error">[[+fi.error.subject]]</span>
</div>
<div class="formRow">
<label for="message">Message: </label><span class="error">[[+fi.error.message]]</span> <br/>
<textarea name="message:required:stripTags" id="message" cols="55" rows="7">[[+fi.message]]</textarea>
</div>
<div class="formRowSubmit">
<input type="submit" value="Send" name="Submit"/>
</div>
</form>
The code above has 2 parts. The FormIt call and the html for the form. The only thing that you need to pay attention is the extra hook present in the FormIt call: emailUser. This is also the name of the snippet that will take the confirmation email, parse it and send it to the user.
The Contact Template
Please create a new chunk named ContactTpl and paste the code below:
This is the message [[+name]] ([[+email]]) sent from the Contact Form: <br/> [[+message]]
The Confirmation Template
Please create a new chunk named ConfirmationTpl and paste the code below:
<p> Hi [[+name]],<br/> We received your message and we will get back to you as soon as possible. </p> <p> Here is your message:<br/> [[+message:nl2br]] </p>
The emailUser snippet
This is the code that sends the confirmation email. Please create a new snippet named emailUser and paste the code below:
<?php
//configuration
$mailFrom = $modx->getOption('emailsender'); //OR USE YOUR OWN;
$mailFromName = $modx->getOption('site_name'); //OR USE YOUR OWN;
$mailSender = $modx->getOption('site_name'); //OR USE YOUR OWN;
$mailSubject = 'YOUR SUBJECT';
$mailReplyTo = $mailFrom;
//get fields values so they can be replaced in the email chunk
$confirmationFields['message'] = $scriptProperties['fields']['message'];
$confirmationFields['name'] = $scriptProperties['fields']['name'];
//get user's email
$mailTo= $scriptProperties['fields']['email'];
$message = $modx->getChunk('ConfirmationTpl', $confirmationFields);
$modx->getService('mail', 'mail.modPHPMailer');
$modx->mail->set(modMail::MAIL_BODY,$message);
$modx->mail->set(modMail::MAIL_FROM,$mailFrom);
$modx->mail->set(modMail::MAIL_FROM_NAME,$mailFromName);
$modx->mail->set(modMail::MAIL_SENDER,$mailSender);
$modx->mail->set(modMail::MAIL_SUBJECT,$mailSubject);
$modx->mail->address('to',$mailTo);
$modx->mail->address('reply-to',$mailReplyTo);
$modx->mail->setHTML(true);
if (!$modx->mail->send()) {
$modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while trying to send the email: '.$err);
return false;
}
$modx->mail->reset();
return true;
There are things that you might want to do to this snippet:
- Change the subject and the contact information. Do so in the configuration section.
- Get other field values so they can be replaced in the Confirmation Template(add them in the chunk as well)
Hopefully this makes sens. Let me know your opinion in the comments section.

Write a comment
Posts: 6
Reply #7 on : Wed September 29, 2010, 06:00:51
Posts: 1
Reply #6 on : Wed September 29, 2010, 08:43:21
Posts: 6
Reply #5 on : Sun December 05, 2010, 11:53:34
Posts: 6
Reply #4 on : Wed December 22, 2010, 02:37:21
Posts: 6
Reply #3 on : Tue August 30, 2011, 04:00:13
Posts: 6
Reply #2 on : Wed November 09, 2011, 17:36:39
Posts: 6
Reply #1 on : Fri January 27, 2012, 07:22:45