أود استخدام نظام البريد الإلكتروني في Drupal لإرسال بريد إلكتروني برمجيًا من الوحدة النمطية المخصصة الخاصة بي. هل هذا ممكن؟
باستخدام hook_mail و drupal_mail يمكنك إنشاء وإرسال بريد إلكتروني.
تنفيذ بريد إلكتروني باستخدام hook_mail:
function MODULENAME_mail ($key, &$message, $params) {
switch ($key) {
case 'mymail':
// Set headers etc
$message['to'] = '[email protected]';
$message['subject'] = t('Hello');
$message['body'][] = t('Hello @username,', array('@username' => $params['username']));
$message['body'][] = t('The main part of the message.');
break;
}
}
لإرسال بريد استخدم drupal_mail:
drupal_mail($module, $key, $to, $language, $params = array('username' => 'John Potato'), $from = NULL, $send = TRUE)
من الواضح أن استبدال المعلمات: يجب أن يساوي مفتاح $ "mymail"
يتم إرسال بريد إلكتروني في بضع خطوات:
إذا كنت تريد طريقة أبسط لإرسال رسائل البريد الإلكتروني ، فراجع Simple Mail ؛ إنها وحدة أعمل عليها لجعل إرسال رسائل البريد الإلكتروني مع Drupal 7+ أسهل بكثير ، ولا يتطلب أي تطبيقات ربط إضافية أو معرفة نظام البريد. إن إرسال بريد إلكتروني بسيط مثل:
simple_mail_send($from, $to, $subject, $message);
يمكنك استخدام طريقة أبسط لإرسال رسائل البريد الإلكتروني ، تحقق من mailsystem ؛ إنها وحدة.
<?php
$my_module = 'foo';
$from = variable_get('system_mail', '[email protected]');
$message = array(
'id' => $my_module,
'from' => $from,
'to' => '[email protected]',
'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.
}
?>
يمكنك استخدام هذا الرمز في خطاف من اختيارك داخل الوحدة النمطية المخصصة الخاصة بك:
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');