簡體   English   中英

如何在drupal 7中發送電子郵件

[英]how to send email in drupal 7

有沒有人可以幫助一個示例源代碼在drupal 7中發送電子郵件。任何人都可以幫助contact_submit函數使用drupal_mail() 我正在使用自定義模塊:

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

放入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, 'demo@demo.com', 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;
          }
 }

示例模塊包含許多示例,包括電子郵件。 在api站點看到這里請參閱drupal_mail()函數以查看函數參數和用戶示例以及函數使用。

GIYF。

請參閱示例模塊的電子郵件示例。 對於大多數核心功能,它有一些很好的例子

http://drupal.org/project/examples

您可以在自定義模塊中的自己選擇的鈎子中使用此代碼:

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

然后你可以像這樣使用上面的函數:

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

如果你是drupal的新手,那么你可以使用內置的php發送郵件而不使用drupal函數:

例如:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM