Skip to content

Instantly share code, notes, and snippets.

@GreatPotato
Created October 8, 2013 09:44
Show Gist options
  • Save GreatPotato/6882259 to your computer and use it in GitHub Desktop.
Save GreatPotato/6882259 to your computer and use it in GitHub Desktop.
Send an email with validation in LemonStand
<?php
function send_contact_form() {
$validation = new Phpr_Validation();
$validation->add('name')->fn('trim')->required('Please enter your name');
$validation->add('email')->fn('trim')->required('Please enter your email address')->email(false, 'Please enter a valid email address');
$validation->add('subject')->fn('trim');
$validation->add('order_no')->fn('trim');
$validation->add('message')->fn('trim')->required('Please enter a message');
if(!$validation->validate($_POST))
$validation->throwException();
$template = System_EmailTemplate::create()->find_by_code('shop:contact_form');
if( ! $template)
throw new Cms_Exception('Could not find email template with code "shop:contact_form". Please create this email template to send the contact form.');
$users = Users_User::create()->from('users', 'distinct users.*');
$users->join('shop_roles', 'shop_roles.id=users.shop_role_id');
$users->where('shop_roles.name=?', 'Default');
$users->where('(users.status is null or users.status = 0)');
$users = $users->find_all();
if( ! $users->count)
throw new Cms_Exception('Could not find any people to send to. Please add some users to the "Default" user group.');
try {
$message = str_replace(
array(
'{name}',
'{email}',
'{subject}',
'{order_no}',
'{message}'
),
array(
$validation->fieldValues['name'],
$validation->fieldValues['email'],
$validation->fieldValues['subject'],
$validation->fieldValues['order_no'],
nl2br($validation->fieldValues['message']),
),
$template->content
);
$template->send_to_team($users, $message);
}
catch (Exception $ex) {
throw new Cms_Exception($ex->getMessage());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment