2012-08-16 45 views
4

是否有可以與示例源代碼有助於發送電子郵件在Drupal 7 任意一個幫助與contact_submit功能可使用drupal_mail()。 我使用自定義模塊:如何發送電子郵件在Drupal 7

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 ??? 
} 

回答

5

例子模塊包含上手的例子數,包括電子郵件。 在api站點中查看here 查看drupal_mail()函數查看函數參數和用戶示例以及函數使用。

GIYF。

6

放在contact_form_Submit功能

$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); 

,並創建新的函數

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; 
      } 
} 
+0

這是一個很好的簡約例子,謝謝。 – RevNoah 2014-09-25 20:12:24

+0

謝謝,RevNoah – 2014-09-26 08:27:41

0

您可以在使用此代碼您自定義模塊中的鉤子:

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', '[email protected]'); 
     } 
     $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; 
     } 
    } 

然後你可以這樣使用上述功能:

$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

如果你是新來的Drupal那麼你可以使用,而無需使用Drupal的功能內置在PHP發送郵件:

例如:

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