Form & shortcode 2 - email

Create an emailform using shortcode (2)

Read first: Form & shortcode 1 - set up a subscription form saving submitted data to the database, using a custom controller and shortcode

In this second article, we'll let the subscription form send an email as well. this is not very hard, and examples are all over the web.

Defining default 'to' and 'from' addresses

The thing is: we'll need a 'from' and a 'to' address. Now that's not much of a problem. First of all we can use the SilverStripe default 'from' address - we just need to make sure it is defined:

In _config.php

// set the default sender e-mailaddress for this site
Email::setAdminEmail('noreply@somedomain');

As for a default 'to' address - there are many options. You could use some members address, or add a SiteConfig setting for that. In this case I'm going to define a static variable within the SubscriptionForm itself, together with a setter.

In SubscriptionForm.php:

protected static $default_mailto = '';

public static function set_mailto($mail = '') {
	if ($mail) self::$default_mailto = $mail;
}

In _config.php

// set the default to address for subscriptions
SubscriptionForm::set_mailto('me@domain');

That's that taken care of for now.

Custom e-mail template

The following template is not very flexible. We would like a template that can loop through existing fields en generate them, but that's another topic, and for now this will work:

SubscriptionEmail.ss

<h1 style="color:#CC6600;font-family:Arial,Helvetica,sans-serif;">
	<% _t('SubscriptionForm.MAILTITLE','Subscription from the website') %>
</h1>
<p style="font-family:Arial,Helvetica,sans-serif;">
	<% _t('SubscriptionForm.AUTOMATIC_MAIL','Do not reply directly, ...') %>
</p>
<p style="font-family:Arial,Helvetica,sans-serif;">
	<strong><% _t('Subscription.TOPIC','Topic') %>:</strong> $Topic<br />
	<strong><% _t('Subscription.FULLNAME','Name') %>:</strong> $FullName<br />
	<strong><% _t('Subscription.EMAIL','Email') %>:</strong> $Email<br />
</p>

Send the email

To send the e-mail, we'll add some code to the SubmitForm() function from the previous article

In SubscriptionForm.php

function SubmitForm($data, $form) {

	$result = 0;

	$mailfrom = Email::getAdminEmail();

	$mailto = self::$default_mailto;

	if ($mailfrom && $mailto ) {

		$topic = (!empty($data['Topic']))? $data['Topic'] : '';
		$subject = _t('SubscriptionForm.SUBSCRIPTION', 'Subscription') . ': ' . $topic;
		$secureData = Convert::raw2xml($data);
		$Email = new Email($mailfrom, $mailto, $subject);
		$Email->setTemplate('SubscriptionEmail');
		$Email->populateTemplate($secureData);
		$Email->send();
	}
	$subscription = new Subscription();
	$form->saveInto($subscription);
	$result = $subscription->write();

	$sessionKey = 'Subscription' . $data['FormID'];
	$sessionValue = (!empty($result))? 'success' : 'failed';

	Session::set($sessionKey, $sessionValue);
	Director::redirectBack();	
}

This will send the e-mail and save the contents of the file. There all manners of checking and validating you could (and should!) do, and we haven't touched jQuery validation yet, but for now lets keep it simple.

Specify a 'to' address using a shortcode parameter

Little forms like this are even more flexible, if we are able to use a different 'to' address for each form. We don't need any extra page settings in the CMS for that - we can just use a shortcode argument, as we did with the Topic in the first article. One critical point is:  adding the e-mailaddress to the form just like that is not really an option, so we'd probably rather use an alias:

[Subscription topic='Monet' to='ArtsDept']

Next we need to somehow map the alias to the corresponding e-mailaddress. There are lots of ways to tackle this. Think: Members, some custom Alias object, a commaseparated textfile in SiteConfig - you name it. For the sake of this example we'll keep it simple, and use a static array within the SubscriptionForm, that can be set from _config:

In SubscriptionForm.php:

protected static $mail_list = array();

public static function set_mail_list($list = array()) {
	if (!empty($list) && is_array($list)) self::$mail_list = $list;
}

public function getToAddress($data) {
	if (!empty($data['To'])) {
		$alias = $data['To'];
		if (array_key_exists($alias, self::$mail_list) && 
		    !empty(self::$mail_list[$alias])) {

			return self::$mail_list[$alias];
		}
	}
	return self::$default_mailto;
}

In _config.php

SubscriptionForm::set_mail_list(array(
	'Ann' => 'ann@domain',
	'John' => 'john@domain'
));

Then in the Form constructor, we add the the 'to' alias as a hidden field an in SubmitForm() we check the key against the posted alias and retrieve the corresponding e-mailaddress.

In SubscriptionForm.php:

function __construct($controller, $name, $arguments=array()) {

	...

	$to = (!empty($arguments['to']))? $arguments['to']: '';
	$fields->push(new HiddenField('To', 'To', $to));
	...
}

function SubmitForm($data, $form) {

	$result = 0;
	// default from address (set in /mysite/_config.php)
	$mailfrom = Email::getAdminEmail();

	// get the mailaddrs from the alias or use the default send address
	$mailto = $this->getToAddress($data);
	...
}

What's next?

 Form & shortcode 3 - customize - customize the shortcode name and arguments using a map

Comments

Het versturen van reacties is uitgeschakeld.

RSS feed voor reacties op deze pagina