How to send email in drupal 7

Can someone help with source code example for sending email in drupal 7. Can anyone help with contact_submit function using drupal_mail () . I am using a 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 ??? } 
+4
source share
5 answers

The sample module contains many examples to get you started, including emails. see here in the api here See the drupal_mail () function to see function parameters and user examples, as well as use functions.

GIYF.

+5
source

Insert 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, ' demo@demo.com ', TRUE); 

and create a 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; } } 
+6
source

see an example email example module. It has some really good examples for most basic functions.

http://drupal.org/project/examples

+2
source

You can use this code in your own choice in your custom module:

 function yourmodulename_mail($from = 'default_from', $to, $subject, $message) { $my_module = 'yourmodulename'; $my_mail_token = microtime(); if ($from == 'default_from') { // Change this to your own default 'from' email address. $from = variable_get('system_mail', ' admin@yoursite.com '); } $message = array( 'id' => $my_module . '_' . $my_mail_token, 'to' => $to, 'subject' => $subject, 'body' => array($message), 'headers' => array( 'From' => $from, 'Sender' => $from, 'Return-Path' => $from, ), ); $system = drupal_mail_system($my_module, $my_mail_token); $message = $system->format($message); if ($system->mail($message)) { return TRUE; } else { return FALSE; } } 

Then you can use the above function as follows:

  $user = user_load($userid); // load a user using its uid $usermail = (string) $user->mail; // load user email to send a mail to it OR you can specify an email here to which the email will be sent customdraw_mail('default_from', $usermail, 'You Have Won a Draw -- this is the subject', 'Congrats! You have won a draw --this is the body'); 
0
source

If you are new to drupal, you can use the built-in php sending mail without using the drupal functions:

For instance:

 function contact_form_submit($form, &$form_state) { to=$user->mail; $subject=""; $headers="mail-id"; $message="your message here"; $sentmail = mail($to,$subject,$message,$headers); } 
0
source

All Articles