0

I have written a custom module, which can send simple mail. how do I send mail so that user should get the verification link or something similar.

<?php    
/**
* implements hooks_menu
*/

function planet_extension_menu(){
$items['planetext']=array(
'title' => t('DEMO Planet Extension'),
'description' => 'Feeds aggregation ',
'page callback' => 'drupal_get_form',
'page arguments' => array('planet_extension'),
'access callback' => TRUE,
);
return $items;
}

// callback function
function planet_extension($form,&$form_submit){
$form['url']=array(
'#title' => 'URL',
'#type' => 'textfield',
'#required' => TRUE,
'#size' => 35,
'#description' => t('Enter the URL'),
'#attributes' =>array('placeholder' => t('www.example.com'))
);
$form['title']=array(
'#title' => 'Title',
'#type' => 'textfield',
'#required' => TRUE,
'#size' => 35,
'#description' => t('Enter the Title'),
'#attributes' =>array('placeholder' => t('Title'))
);
$form['email']=array(
'#title' => 'email',
'#type' => 'textfield',
'#required' => TRUE,
'#size' => 35,
'#description' => t('Enter you valid email'),
'#attributes' =>array('placeholder' => t('[email protected]'))
);
$form['submit']=array(
'#type' =>'submit',
'#value' => 'Submit',
);
return $form;
}

// below is the form submission function
function planet_extension_submit($form,$form_state){
//store the values entered in form into variables
$values = array(
'url' => $form_state['values']['url'],
'title' => $form_state['values']['title'],
'email' => $form_state['values']['email'],
);
//insert the variables into the table
$insert = db_insert('planet_extension')
                -> fields(array(
                        'url' => $values['url'],
                        'title' => $values['title'],
                        'email' => $values['email'],
                ))
                ->execute();

drupal_mail('planet_extension', 'planet_extension_mail', $form_state['values']['email'], 'en', array('values' => $values));

        drupal_set_message(t('Form submitted. Check your email for verification')); //custom message after form submission is successful
}


//implements hook_mail
function planet_extension_mail ($key, &$message, $params) {
  switch ($key) {
    case 'planet_extension_mail':
      $message['subject'] = t('Hello');
      $message['body'][] = t('Hello user');
      $message['body'][] = t('The main part of the message.');
      break;
  }
}
5
  • Format the code a bit, it will help other people understand what it does. Regarding your question - you will need a table to store generated verification codes paired with appropriate email addresses and a link that will accept the code, redirect to home page and show the message to the user. Oct 17, 2013 at 6:17
  • Thank you so much for reply. But, how do i generate verification codes and pair with email ??
    – shrish
    Oct 17, 2013 at 7:12
  • what kind of verification mails are you sending?
    – arpitr
    Oct 17, 2013 at 7:13
  • just the verification link in the mail to the user who entered the form. so that the user can click that link and verify. hope i am clear for you. thank you
    – shrish
    Oct 17, 2013 at 7:15
  • The logic of generating verification codes is up to you. You can have a look at the default Drupal implementation in user module: modules/user/user.module, line 2323, function user_pass_reset_url(). Oct 17, 2013 at 7:22

1 Answer 1

1

I am not sure about what kind of verification emails you are sending, though I believe if you peep inside drupal core user module you can get hold of function which does the same,as for user registration process we have email verification option also.

A very simple straight custom way might go in the following way

  1. How you will be generating the verification code?
    • while sending the email create a link something like www.mydomain.com/email-verification/%random_string%
  2. Create a custom table with column as email,random_string,verified
  3. once you have generated it, make a db query to insert the link you created a point back against the email user entered and set verified to 0 in the custom table.
  4. send the email
  5. In your custom module implement hook_menu() with menu entry fir "email-verification" .
  6. In the page callback using wild card check if the random_string exists in you custom table, if so set verified to 1

Not exact but close to what you may need.

Update:

global $base_url;
$random_string = substr(str_shuffle(md5(time())),0,10);
// Link 
$email_link = $base_url . "/email-verification/" . $random_string;
3
  • yes it is close enough to get me started with. thanks man really appreciated.
    – shrish
    Oct 17, 2013 at 7:38
  • in step 1 : how do i create a link ?
    – shrish
    Oct 17, 2013 at 7:47
  • @shrish check the updated answer
    – arpitr
    Oct 17, 2013 at 8:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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