Created
June 14, 2014 18:49
-
-
Save rabellamy/66033868021f6072b393 to your computer and use it in GitHub Desktop.
This file contains 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 | |
/** | |
* Implements hook_form(). | |
*/ | |
function contact_form($form, &$form_state) { | |
$form['email'] = array( | |
'#type' => 'textfield', | |
'#required' => TRUE, | |
'#title' => t('Email'), | |
'#title_display' => 'invisible', | |
'#attributes' => array ('placeholder' => t('Email')), | |
); | |
$form['submit'] = array( | |
'#type' => 'submit', | |
'#value' => t('Submit'), | |
'#submit' => array('contact_form_submit'), | |
); | |
$form['#validate'] = array('contact_form_validate'); | |
return $form; | |
} | |
/** | |
* Implements hook_validate(). | |
*/ | |
function contact_form_validate($form, &$form_state) { | |
$email = $form_state['values']['email']; | |
if(valid_email_address($email) == 0) { | |
form_set_error('email', t('Not a valid email address')); | |
} | |
} | |
/** | |
* Submit handler. | |
* @param $form | |
* @param $form_state | |
*/ | |
function contact_form_submit($form, &$form_state) { | |
$mail_message = t("Foo"); | |
$to = '[email protected]'; | |
$email = $form_state['values']['email']; | |
$send = TRUE; | |
$key = 'contact_form_email'; | |
$from = $form_state['values']['email']; | |
$message = drupal_mail('contact_form', 'notify', $email, language_default()); | |
if ($message['result'] == TRUE) { | |
drupal_set_message($mail_message,'status'); | |
} | |
else { | |
drupal_set_message(t('There was a problem sending your message and it was not sent.'), 'error'); | |
} | |
} | |
/** | |
* Builds email message. | |
* @param $form_state | |
* @return $body | |
*/ | |
function get_mail_content($form_state) { | |
$content = array(); | |
$body = '<b>' . 'Email: ' . '</b>' . ' ' . $form_state['values']['email']; | |
$content['body'] = $body; | |
return $content; | |
} | |
/** | |
* Implements hook_mail(). | |
*/ | |
function contact_form_mail($key, &$message, $params) { | |
$language = $message['language']; | |
switch ($key) { | |
case 'contact_form_email': | |
$message['headers']['Content-Type'] = 'text/html; charset=UTF-8;'; | |
$message['subject'] = t('YO'); | |
$message['email'][] = $params['email']; | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment