Hello,

I have a content Type "Jobs" and i display it with views (block) and i override fields in views-view-fields--jobs--block.tpl.php like this :

<li class="ic-on"><?php print ($fields['field_imagejob']->content);?></li>
<li class="wb-dis"><h4><?php  print ($fields['title']->content);?></h4><h5><?php  print ($fields['field_time']->content);?></h5></li>
<li class="dr-co"><p><?php  print ($fields['field_companyname']->content);?></p></li>
<li class="sa-pn"><p><i class="bung"> </i><?php  print ($fields['field_address']->content);?></p></li>
<li class="dolla"><p><?php  print ($fields['field_cost']->content);?></p></li>
<li class="b-tn"><div class="apply_btn"><form><input type="submit" name="submit" value="apply"</form></div></li>

Once i clik on apply, i would like to sent mail to : $fields['field_emailjob']->content.

How can i do that! plz i need help

Comments

patrickkearney’s picture

It looks like all you will need is a hook_form_submit and then the email API:

You can see the hook_form_submit documentation here:
https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.h...

And here is an example of using forms to send email:
https://www.drupal.org/node/1850384

Hope that helps :)

patrickkearney’s picture

Hello,
The easiest way is to use hook_form_submit to set a specific function to run upon being submitted. I would suggest validating the form data, then proceeding with the email process using drupal_mail. Be sure to validate as email is the most frequently abused system on websites.

Documentation for the hook_form_submit can be found in the form API info here:
https://api.drupal.org/api/drupal/includes!form.inc/function/drupal_form...

Documentation for drupal_mail can be found here:
https://api.drupal.org/api/drupal/includes!mail.inc/function/drupal_mail/7

Hope that helps,

Patrick

juagarc4’s picture

Hello

You provide few information of your code or your objects but I'll try to post something useful.

1.- Adjustments to the view

In order to obtain plain values without markup, we must to instruct Drupal to render field values
as plain text without HTML tags.
Moreover the filed "Title" is rendered as link to content. That must be corrected too.

For the field "Title" unselect the checkbox "Link this field to the original piece of content".
For each field in the area STYLE SETTINGS select Customize field HTML and select option -None-

2.- In template.php

First: Declare the form

function YOUR_THEME_NAME_jobs_form($form, &$form_state) {

  $form['#attributes'] = array(
    'name' => 'jobs_form',
    'id' => 'jobs_form',
    'class' => array('jobs_form'),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Send'),
  );

  return $form;
}

Second(optional): Validate the form

function YOUR_THEME_NAME_jobs_form_validate($form, &$form_state) {
  $valid_email = $form_state['input']['email'];
  if (!valid_email_address($valid_email)) {

    form_set_error('email', t('Sorry. Your email address, %email, is not valid. Please submit a valid E-mail address.', array('%email' => $valid_email)));
  }
}

Third: Getting Email components

function YOUR_THEME_NAME_jobs_form_mail($key, &$message, $params) {
  $message['subject'] = $params['subject'];
  $message['body'] = $params['body'];
}

Fourth: Sending the form

function YOUR_THEME_NAME_jobs_form_submit($form, &$form_state) {

//Here we get the email from form
  $valid_email = $form_state['input']['email'];
  $from = 'noreply@example.com';
  $body[] = $form_state['input']['title'] . " " . $form_state['input']['time'];
  $body[] = nl2br($form_state['input']['imagejob']);
  $body[] = $form_state['input']['companyname'];
  $body[] = $form_state['input']['address'];
  $body[] = $form_state['input']['cost'];
  $to = $valid_email; // Set this email address - emails will be sent to this email address!
  $params = array(
    'body' => $body,
    'subject' => t('Website Information Request'),
  );

  if (drupal_mail('prueba_jobs_form', 'block_jobs_form', $to, language_default(), $params, $from, TRUE)) {
    $message = t('Thanks, we will be in contact with more information soon.');
  }
  else {
    $message = t('There was an error subscribing you. Please try again later');
  }
}

3. In template views-view-fields--jobs--block.tpl

- First: Get the form and the fields to use.

<?php
//Getting the form component from Drupal
$form = drupal_get_form('YOUR_THEME_NAME_jobs_form');

//Setting some variables to use later
$form_id = $form['#id'];
$form_method = $form['#method'];
$form_action = $form['#action'];

$field_emailjob = render($fields['field_emailjob']->content);
$field_imagejob = render($fields['field_imagejob']->content);
$title = render($fields['title']->content);
$field_time = render($fields['field_time']->content);
$field_companyname = render($fields['field_companyname']->content);
$field_address = render($fields['field_address']->content);
?>

- Second: Override the fields of the view

<li class="ic-on"><?php print $field_imagejob; ?></li>
<li class="wb-dis"><h4><?php print $title; ?></h4><h5><?php print $field_time; ?></h5></li>
<li class="dr-co"><?php print $field_companyname; ?></li>
<li class="sa-pn"><?php print $field_address; ?></li>
<li class="dolla"><?php print $field_cost ?></li>

- Third: Define the form with hidden fields.
This is necessary in order to send the values and process them.

<li class="b-tn">
  <div class="apply_btn">
    <form id="<?php print $form_id; ?>" accept-charset="UTF-8" method="<?php print $form_method; ?>" action="<?php print $form_action; ?>">
      <input type="hidden" name="email" value="<?php print $field_emailjob; ?>">
      <input type="hidden" name="imagejob" value="<?php print $field_imagejob; ?>">
      <input type="hidden" name="title" value="<?php print $title; ?>">
      <input type="hidden" name="time" value="<?php print $field_time; ?>">
      <input type="hidden" name="companyname" value="<?php print $field_companyname; ?>">
      <input type="hidden" name="address" value="<?php print $field_address; ?>">
      <input type="hidden" name="cost" value="<?php print $field_cost; ?>">
      <input type="submit" value="apply">
      <?php print drupal_render($form['form_id']); ?>
      <?php print drupal_render($form['form_build_id']); ?>
      <?php print drupal_render($form['form_token']); ?>
    </form>
  </div>
</li>

I hope this help you.
Regards.

Raul Garcia

Joekz’s picture

Thank you very much