简体   繁体   English

如何在drupal 7中发送电子邮件

[英]how to send email in drupal 7

Does any can help with an example source code to send email in drupal 7. Can any one help with the contact_submit function to use drupal_mail() . 有没有人可以帮助一个示例源代码在drupal 7中发送电子邮件。任何人都可以帮助contact_submit函数使用drupal_mail() I'm using custom module : 我正在使用自定义模块:

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

Put in contact_form_Submit function 放入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);

and Create New function 并创建新功能

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

Examples module contains number of examples to get started with, including emails. 示例模块包含许多示例,包括电子邮件。 see it in api site here See drupal_mail() function to see function params and user examples as well as function use. 在api站点看到这里请参阅drupal_mail()函数以查看函数参数和用户示例以及函数使用。

GIYF. GIYF。

refer to the email example of the example module. 请参阅示例模块的电子邮件示例。 It has some really good examples for most of the core features 对于大多数核心功能,它有一些很好的例子

http://drupal.org/project/examples http://drupal.org/project/examples

You can use this code in a hook of your own choice within your custom module: 您可以在自定义模块中的自己选择的钩子中使用此代码:

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

Then you can use the above function like this: 然后你可以像这样使用上面的函数:

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

If you are new to drupal then you can use the built in php send mail without using drupal functions: 如果你是drupal的新手,那么你可以使用内置的php发送邮件而不使用drupal函数:

For example: 例如:

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