Tôi muốn sử dụng hệ thống email của Drupal để lập trình gửi email từ mô-đun tùy chỉnh của mình. Điều đó có thể không?
Tôi muốn sử dụng hệ thống email của Drupal để lập trình gửi email từ mô-đun tùy chỉnh của mình. Điều đó có thể không?
Câu trả lời:
Sử dụng hook_mail và drupal_mail, bạn có thể tạo và gửi e-mail.
Triển khai e-mail sử dụng hook_mail:
function MODULENAME_mail ($key, &$message, $params) {
  switch ($key) {
    case 'mymail':
      // Set headers etc
      $message['to'] = 'foo@bar.com';
      $message['subject'] = t('Hello');
      $message['body'][] = t('Hello @username,', array('@username' => $params['username']));
      $message['body'][] = t('The main part of the message.');
      break;
  }
}
Để gửi thư, hãy sử dụng drupal_mail:
drupal_mail($module, $key, $to, $language, $params = array('username' => 'John Potato'), $from = NULL, $send = TRUE)
Rõ ràng thay thế các tham số: $ key phải bằng 'mymail'
Một e-mail được gửi trong một vài bước:
$message['to']này là mã hóa cứng foo@bar.com. Bỏ qua điều này và tin nhắn sẽ được gửi đến người nhận được chỉ định khi drupal_mail()được gọi.
                    Nếu bạn muốn một cách đơn giản hơn để gửi email, hãy xem Simple Mail ; đó là một mô-đun tôi đang làm việc để gửi email với Drupal 7+ dễ dàng hơn nhiều và nó không yêu cầu bất kỳ triển khai hook nào hoặc kiến thức về MailSystem. Gửi một email đơn giản như:
simple_mail_send($from, $to, $subject, $message);
              Bạn có thể sử dụng một cách đơn giản hơn để gửi email, kiểm tra mailsystem ; đó là một mô-đun.
<?php
$my_module = 'foo';
$from = variable_get('system_mail', 'organization@example.com');
$message = array(
  'id' => $my_module,
  'from' => $from,
  'to' => 'test@example.com',
  'subject' => 'test',
  'body' => 'test',
  'headers' => array(
    'From' => $from, 
    'Sender' => $from, 
    'Return-Path' => $from,
  ),
);
$system = drupal_mail_system($my_module, $my_mail_token);
if ($system->mail($message)) {
  // Success.
}
else {
  // Failure.
}
?>
              Bạn có thể sử dụng mã này trong một móc của sự lựa chọn của riêng bạn trong mô-đun tùy chỉnh của bạn:
 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;
            }
        }
Sau đó, bạn có thể sử dụng chức năng trên như thế này:
        $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');