【问题标题】:how to send email in drupal 7如何在drupal 7中发送电子邮件
【发布时间】:2012-08-16 12:03:19
【问题描述】:

有没有人可以帮助提供在 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 ???
}

【问题讨论】:

    标签: drupal drupal-7


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      示例模块包含许多可供入门的示例,包括电子邮件。 在 api 站点中看到它here 请参阅drupal_mail() 函数以查看函数参数和用户示例以及函数使用。

      GIYF.

      【讨论】:

        【解决方案3】:

        参考示例模块的电子邮件示例。它为大多数核心功能提供了一些非常好的示例

        http://drupal.org/project/examples

        【讨论】:

          【解决方案4】:

          你可以在你的自定义模块中使用你自己选择的钩子:

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

          【讨论】:

            【解决方案5】:

            如果您是 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);    
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-06-15
              • 1970-01-01
              • 2014-09-11
              • 1970-01-01
              • 2013-06-05
              • 1970-01-01
              相关资源
              最近更新 更多