Created
November 20, 2013 16:29
-
-
Save GreatPotato/7566206 to your computer and use it in GitHub Desktop.
Validates and sends an email
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class ContactForm { | |
| public 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