Send a confirmation email with FormIt

on 19-Sep-2010 | Comments ( 11 ) Tags: Revolution
Seems that the 1.3.0 version and above has this functionality built in(see the AutoResponder hook). You can still use this tutorial as a guide to your custom hooks.

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:


Hopefully this makes sens. Let me know your opinion in the comments section.

Write a comment

  • Required fields are marked with *.

If you have trouble reading the code, click on the code itself to generate a new random code.
 
John
Posts: 9
Comment
Not working
Reply #11 on : Wed September 29, 2010, 05:00:51
Hi mate,

I having just been trying out your code and I'm having a bit of trouble getting it to work. I'm using modx revolution.

Do you know of any thing that might stop it working? Should my hosting be setup in a particular way in for mail to be sent? etc...

Any help would be appreciated.

Cheers

John
cipa
Posts: 2
Comment
Re: Send a confirmation email with FormIt
Reply #10 on : Wed September 29, 2010, 07:43:21
There are some discussions going on here:http://modxcms.com/forums/index.php/topic,53795.0/topicseen.html

See Bob's reply: MODx will use SMTP on it's own whenever a snippet sends mail if you set the SMTP System Settings. If you set those, don't use the snippet's SMTP parameters (if any)
ajay kumar
Posts: 9
Comment
confirmation
Reply #9 on : Sun December 05, 2010, 10:53:34
i want confirm my orkut account for downloding items, iwant to send confirm msg.
Dmitry
Posts: 9
Comment
Cool
Reply #8 on : Wed December 22, 2010, 01:37:21
Hey!
Nice and clear tutotorial!
It helps me!
Thanx!

How I used it you can see on that page:
http://germes.plohie.com/index.php?id=7

Thanx one more and more time! =)
1
Posts: 9
Comment
1
Reply #7 on : Tue August 30, 2011, 03:00:13
21231321
Sergio
Posts: 9
Comment
It worked!
Reply #6 on : Wed November 09, 2011, 16:36:39
But it slowed down my entire site and admin, so I uninstalled FormIt and my site is back to regular speeds. Odd.
Mukesh
Posts: 9
Comment
Re: Send a confirmation email with FormIt
Reply #5 on : Fri January 27, 2012, 06:22:45
Hi,

I am new in Modx Revo

Mine Validation is working, but form is not sending any emails to email id or not even success message.

Regards,
Mukesh
david
Posts: 9
Comment
adding a second email
Reply #4 on : Thu March 29, 2012, 09:28:09
Is there a way to add a second email address to the send list? and if so, is there a way to send a different set of content to it? Like an email goes out to the site mod giving him information and then and email goes out to the visitor giving them confirmation.
Marc
Posts: 9
Comment
redirect not working anymore
Reply #3 on : Sun July 13, 2014, 14:58:53
Hi there!

I'm using this modification/extra snipper "emailUser" in my FormIt. But by using this "custom hook" the form doesn't redirect anymore on succeddful validation.

What to do?
cipa
Posts: 2
Comment
Re: Send a confirmation email with FormIt
Reply #2 on : Sun July 13, 2014, 16:18:58
This was build for one of the first versions of FormIt. Maybe they changed the way it works. Anyway the new versions should have the autoresponder hook available
sunil
Posts: 9
Comment
help
Reply #1 on : Wed October 22, 2014, 02:58:52
how to use formit extra in modx revo.

Quick modx Evolution Tags

  • cached [[snippet]] or uncached [!snippet!]
  • {{chunk}}
  • [+placeholder+]
  • [*resourceField/TV*]
  • [^timing^]
  • [~link~]

Quick modx Revolution Tags

  • [[snippet]]
  • [[$chunk]]
  • [[+placeholder]]
  • [[*resourceField/TV]]
  • [[~link]]
  • [^timing^]
  • [[++systemSetting]]
  • [[%languageStringKey]]
  • all tags can be called un-cached like: [[! snippet]]

Timing Tags (Evo and Revo)

  • [^qt^] - Query time
  • [^q^] - Query count
  • [^p^] - Parse time
  • [^t^] - Total time
  • [^s^] - Source
© modxRULES! 2009-2014