Fri, 01 Apr 2011 00:00 CEST in Diem
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.
Just follow Diem documentation about mail templates .
Go to Admin->Tools->Mail templates and create a new template.
We'll use :
created_at : %created_at% name : %name% email : %email% body : %body%
Then save the template.
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)
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(); } }
./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.
Comments for this post
No comment yet.