0

I'm writing my first module that will show a form to users, who submit data that is sent as a mail to the people chosen from a list.

But there is an issue; the mail is not sent. What is the problem with my code?

function my_module_menu() {

$items['kategori'] = array(

'page callback' => 'my_module_kategori',

'access callback' => TRUE,

'type' => MENU_CALLBACK,

);

return $items;

}


function my_module_cmp($a, $b) {

  $a = (array) $a;

  $b = (array) $b;

  return strcmp($a['name'], $b['name']);

}


function my_module_kategori() {

// Show both the form and the title

return array(

    'form' => drupal_get_form('my_module_form'),

);

}

function my_module_form($form, &$form_state) {

// The form

$form['offert'] = array(

    '#title' => t('Skicka offertförfrågan till FLERA leverantörer samtidigt!'),

    '#type' => 'fieldset'

);

$form['offert']['question'] = array(

    '#type' => 'textarea',

    '#title' => t('Förfrågan'),

    '#default_value' =>  t('Skriv en förfrågan till dina utvalda leverantörer här....'),

    '#cols' => 50,

    '#rows' => 5,

    '#resizable' => FALSE

);

$form['offert']['name'] = array(

    '#type' => 'textfield',

    '#title' => t('Namn'),

    '#default_value' => t('Ditt namn....'),

    '#size' => 33,

    '#maxlength' => 64

);

$form['offert']['mail'] = array(

    '#type' => 'textfield',

    '#title' => t('Epost'),

    '#default_value' => t('Epost....'),

    '#size' => 14,

    '#maxlength' => 30

);

$form['offert']['compname'] = array(

    '#type' => 'textfield',

    '#title' => t('Företagsnamn'),

    '#default_value' => t('Företagsnamn....'),

    '#size' => 15,

    '#maxlength' => 64

);

$form['offert']['phone'] = array(

    '#type' => 'textfield',

    '#title' => t('Telefon'),

    '#default_value' => t('Telefon...'),

    '#size' => 14,

    '#maxlength' => 20

);

$form['offert']['comptype'] = array(

    '#type' => 'textfield',

    '#title' => t('Företagstyp'),

    '#default_value' => t('Typ av företag...'),

    '#size' => 15,

    '#maxlength' => 64

);

$form['offert']['submit'] = array(

    '#type' => 'submit',

    '#value' => t('Skicka')

);



// The html

$url = taxonomy_get_term_by_name(arg(1));

foreach($url as $term) {

    $tid = $term->tid;

}

$term = taxonomy_term_load($tid);

$name = taxonomy_term_title($term);

$terms = taxonomy_get_tree(3,0,1);

usort($terms, "my_module_cmp");

$counter = 0;

$result = taxonomy_select_nodes($tid);

$options = array();



foreach($result as $nid) {

    $form[$nid] = array(

        '#type' => 'fieldset',

    );

    $node = node_load($nid);

    $options[$nid] = $node->title;

    $phone = db_query("SELECT field_phone_value FROM {field_data_field_phone} WHERE entity_id=$nid")->fetchField();

    $body = db_query("SELECT body_value FROM {field_data_body} WHERE entity_id=$nid")->fetchField(); 



    // company image

    $form[$nid]['logo'] = array (

        '#markup' => '<div class="logo"><a href="/branschguiden/'.$nid.'"><img src="hdfsd01372593e5e.gif"></a></div>',

    );



    // title

    $form[$nid]['title'] = array (

        '#markup' => '<div class="title"><a href="/branschguiden/'.$nid.'">' . $node->title . '</a></div>',

    );



    // body

    $form[$nid]['body'] = array (

        '#markup' => '<div class="body"><a href="/branschguiden/'.$nid.'">' . $body . '</a></div>',

    );



    // phone

    $form[$nid]['phone'] = array (

        '#markup' => '<div class="phone"><img src="/sites/hrtorget/themes/hrtorget/images/phone-icon.png">'.$phone.'</div>',

    );



    // checkbox

    $form[$nid]['companies'] = array (

        '#type' => 'checkboxes',

        '#title' => t('Check it'),

        '#options' => $options,

        '#default_value' => array(),

    );



$counter++;

}

return $form;

}

function my_module_form_validate($form, &$form_state) {
// Validate that a company has been checked, at all

$valid_companies = $form_state['values']['companies'];

    if (!$valid_companies) {

    form_set_error('companies', 'Du har inte kryssat i något företag');

    }
}

function my_module_form_submit($form, &$form_state) {

// Send mail to each of the checked companies with the date from the form

$values = $form_state['values'];

$question = $form_state['values']['question'];

$name = $form_state['values']['name'];

$mail = $form_state['values']['mail'];

$compname = $form_state['values']['compname'];

$comptype = $form_state['values']['comptype'];

$phone = $form_state['values']['phone'];

$to = 'Ann array of all the checked companies email';

$from = 'test$tesfd.com';

$subject = 'Offer';



// Get the checkbox values from the form

//  $nids = array_keys($form_state['values']['company_wrapper']);

$nids = array_filter($form_state['values']['companies']);





// Print all the values from the form

drupal_set_message('<pre>'.print_r($form_state['values'], 1).'</pre>');

//  drupal_set_message('<pre>'.print_r($form_state['values']['company_wrapper'], 1).'</pre>');

drupal_set_message('<pre>'.print_r($form_state['values']['companies'], 1).'</pre>');

//  dpm($form_state['values']['company_wrapper']);



$body = 'Mesage text with a all the stuff from the form';



$message = array(

'body' => $body,

'subject' => $subject,

);



foreach ($nids as $nid) {

    // Get the node from nid

    $node = node_load($nid);

    // Get the author of that node

    $user = user_load(array('uid' => $node->uid));

    drupal_set_message(t('DEBUG nod:'.$nid.', användare: '.$user->name.','.$user->mail.'')); // DEBUG



    // Check if the mail has been sent and show a message based on that

    if (drupal_mail('my_module', 'branschguiden', $user->mail, language_default(), $message, $from, TRUE)) {

        drupal_set_message(t('Mail sent to %name',array('%name' => $user->name)));

    }

    // If no mail was sent.

    else {

        drupal_set_message(t('Error')); 

    }

}

}
1
  • As side note, the string passed to t() needs to be written in English, or t() will not translate the string. If you are not going to use the translation system used by Drupal, then you should not use the t() function. – apaderno Jul 14 '11 at 1:07
2

This is Drupal's example module mail code

$module = 'module_name';
$key = 'key';
$language = language_default();
$params = array();
$from = NULL;
$send = FALSE;
$message = drupal_mail($module, $key, $email, $language, $params, $from, $send);

$message['subject'] = $subject;
$message['body'] = array();
$message['body'][] = $line1;
$message['body'][] = $line2;

// Retrieve the responsible implementation for this message.
$system = drupal_mail_system($module, $key);

// Format the message body.
$message = $system->format($message);

// Send e-mail.
$message['result'] = $system->mail($message);

so $system->mail($message) actually sends the mail

3
  • Thank you so much for your help. Havent had the time to try it out yet, though. Will to it tomorrow morning. – Daniel Holm Jul 14 '11 at 0:27
  • So, now I'm using that code and there's still no mail sent. Do I have to set up any settings of some sort? – Daniel Holm Jul 14 '11 at 13:45
  • Well check if your server can send mail. Setup a contact form and send an email to yourself. Tell me if that works or not – iStryker Jul 14 '11 at 13:46
1

change

if (drupal_mail('my_module', 'branschguiden', $user->mail, language_default(), $message, $from, TRUE)) {
    drupal_set_message(t('Mail sent to %name',array('%name' => $user->name)));
}
// If no mail was sent.
else {
    drupal_set_message(t('Error')); 
}

to

 $result = drupal_mail('my_module', 'branschguiden', $user->mail, language_default(), $message, $from, TRUE))
  if ($result['result'] == TRUE) {
    drupal_set_message(t('Mail sent to %name',array('%name' => $user->name)));
  }
  else {
    drupal_set_message(t('There was a problem sending your message and it was not sent.'), 'error');
  }
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.