An orchyd from France - by David Jacquel - copyleft

Sending email when dmContactPlugin form is filled on site

dmContactPlugin plugin allows visitors to send you messages. We'll listen to this event to append and send a mail to the webmaster.

How to instruct Diem to sent you a mail when dmContactPlugin is used on your site ?
Just listen to the event attached to contact saved (dm_contact.saved) and use dmMail service to send an email.

Setting up a mail template for our mail

Just follow Diem documentation about mail templates .
Go to Admin->Tools->Mail templates and create a new template.
We'll use :

  • template name = email_contact_form
  • email subject = [my site] contact form
  • template body =
created_at : %created_at%  
name : %name%  
email : %email%  
body : %body%  
  • sender email = administrator@mysite.tld
  • recipient email = administrator@mysite.tld (multiple recipient must be entered one by line)

Then save the template.

Setting up mail service

Go to apps/front/config/factories.yml and add :

prod:  
  mailer:  
    class: sfMailer  
    param:  
      logging:  %SF_LOGGING_ENABLED%  
      charset:  %SF_CHARSET%  
      delivery_strategy: realtime  
      transport:  
        class: Swift_SmtpTransport  
        param:  
          host: smtp.mysite.tld  
          port:       465  
          encryption: ssl  
          username:   username  
          password:   password  

This will enable swift mailer to use a SMTP server to send mails.
You can use other transport means as sendmail or PHP mail() function (see symfony documentation on the mailer)

Setting up the dm_contact.saved event listener

Edit your apps/front/config/frontConfiguration.class.php file and add :

require_once(dm::getDir().'/dmFrontPlugin/lib/config/dmFrontApplicationConfiguration.php');  
 
class frontConfiguration extends dmFrontApplicationConfiguration  
{  
 
    public function configure()  
    {  
        // this will listen to contact saving and call listenToContactSavedEvent method  
        $this->dispatcher->connect('dm_contact.saved', array($this, 'listenToContactSavedEvent'));  
    }  
 
    public function listenToContactSavedEvent(sfEvent $e)  
    {  
        // get the contact object passed by the event  
        $contact = $e['contact'];  
        // get the service container from the context  
        $serviceContainer = $e->getSubject()->getServiceContainer();  
        // get a mail service instance  
        $mailService = $serviceContainer->getService('mail');  
        // use our DmMailTemplate  
        $mailService->setTemplate('email_contact_form');  
        // add values to populate the template  
        $mailService->addValues(array(  
                                'created_at' => $contact->created_at,  
                                'name'       => $contact->name,  
                                'email'      => $contact->email,  
                                'body'       => $contact->body  
                            ));  
        // send the mail using Swift Mailer  
        $sent = $mailService->send();  
    }  
}  

The final touch

./symfony dm:setup  

And that's it !

Now you will receive a mail each time a visitor use your dmContactPlugin contact form.

This can certainly be refined and Just let me know if anyone decide to add this functionality to dmContactPlugin.

Leave a comment

Your email will never be published

Comments for this post

No comment yet.