Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to send email in drupal 7

Does any can help with an example source code to send email in drupal 7. Can any one help with the contact_submit function to use drupal_mail(). I'm using custom module :

function contact_menu() {
  $items['contact'] = array(
    'title' => 'contact form',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('contact_form'),
    'access arguments' => array('access content'),
  );

  return $items;
}

function contact_form() {
  $form['intro'] = array(
    '#markup' => t('Use this form to send a message to an e-mail address. No spamming!'),
  );
  $form['email'] = array(
    '#type' => 'textfield',
    '#title' => t('E-mail address'),
    '#required' => TRUE,
  );
  $form['message'] = array(
    '#type' => 'textarea',
    '#title' => t('Message'),
    '#required' => TRUE,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );

  return $form;
}

    function contact_validate($form, &$form_state) {
      if (!valid_email_address($form_state['values']['email'])) {
        form_set_error('email', t('That e-mail address is not valid.'));
      }
    }
function contact_form_submit($form, &$form_state) {
//What i should write here ???
}
like image 920
Haithem Rihane Avatar asked Dec 06 '22 12:12

Haithem Rihane


2 Answers

Put in contact_form_Submit function

$message = 'New signup email address'; // Body of your email here.
 $params = array(
           'body' => $message,
           'subject' => 'Website Information Request',
           'headers'=>'simple',
     );
     $to = "Your Email Address";
    drupal_mail('contactform', 'send_link', $to, language_default(), $params, '[email protected]', TRUE);

and Create New function

function contact_mail($key,&$message,$params) 
{

       switch ($key) {
             case 'send_link':
                  $message['subject']=t($params['subject']);
                  $message['body'][]=$params['body'];
                  $message['headers']['Content-Type'] = 'text/html; charset=UTF-8; format=flowed';
       break;
          }
 }
like image 98
Mehul Jethloja Avatar answered Jan 07 '23 09:01

Mehul Jethloja


Examples module contains number of examples to get started with, including emails. see it in api site here See drupal_mail() function to see function params and user examples as well as function use.

GIYF.

like image 23
AKS Avatar answered Jan 07 '23 08:01

AKS