drupal电子邮件问题

时间:2011-02-28 17:53:56

标签: drupal drupal-6 drupal-modules

以下是我的联系人模块,

我正在尝试发送电子邮件,猜测电子邮件未成功发送,

我的代码段中有任何错误

<?php

function contactus_menu() {
  $items['contactus/reachus'] = array(
    'title' => 'Contact US',
    'page callback' => 'contactus_description',
    'access callback' => TRUE,
    'expanded' => TRUE,
  );

   return $items;
}

  function contactus_description(){

     return drupal_get_form('contactus_form');
}

function contactus_form() {
$form['#attributes']['enctype'] = 'multipart/form-data';
  $form['fullname'] = array(
    '#type' => 'textfield',
    '#title' => t('Enter your full name'),
    '#description' => t('Please enter your name  here'),
    '#required' => TRUE,
  );
   $form['emailid'] = array(
    '#type' => 'textfield',
    '#title' => t('Enter your Email-ID'),
    '#description' => t('Please enter your Email-ID'),
    '#required' => TRUE,
  );
  $form['message'] = array(
    '#type' => 'textarea',
    '#title' => t('Enter your message'),
    '#default_value' =>  variable_get('Please enter your message', ''),
    '#cols' => 60,
    '#rows' => 5,
    '#description' => t('Please write your mssage'),
  );
  $form['file_upload'] = array(
    '#title' => t('Upload file'),
    '#required' => TRUE,
    '#type'  => 'file',
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Reach Me '),
  );
  return $form;
}

function contactus_form_submit($form_id, $form_values) {
  $message = 'You have submitted the ' . $form_id . ' form which contains the following data:<pre>' . print_r($form_values,true) . '</pre>';
  $fullname =  $form_values['values']['fullname'];
  $emailid  =  $form_values['values']['emailid'];
  $email_flag = valid_email_address($emailid);

  $message  =  $form_values['values']['message'];
  $message = $fullname . "\n" .$emailid. "\n" .$message;

  $validators = array();  
  $dest = 'file';
  $file = file_save_upload('file_upload', $validators, $dest);
  //$file will be 0 if the upload doesn't exist, or the $dest directory
  //isn't writable
  if ($file != 0) {
    $dest_path = 'contactus';
    $result = file_copy($file, $dest_path, FILE_EXISTS_RENAME);
    if ($result == 1) {
      //Success, $file object will contain a different (renamed)
      //filename and filepath if the destination existed

      $file_msg = "successfully uploaded\n";
    }
    else {
      //Failure
       $file_msg = "failed uploaded\n";
    }
  }
  else {
    form_set_error('myform', t("Failed to save the file."));
  }
  $message = $fullname."\n".$emailid." ---$email_flag ----\n".$message."\n".$file_msg;
  $to = 'bharanikumariyerphp@gmail.com';
  $from = 'bharanikumariyerphp@gmail.com';
  $subject = st('New Drupal site created!');

drupal_mail('university-profile', $to, $subject, $message, $from);
}

?>

1 个答案:

答案 0 :(得分:0)

您使用错误的参数调用drupal_mail。在Drupal中发送邮件的正确方法要求你实现hook_mail钩子,根据一个密钥和drupal_mail发送的一些参数,你返回标题和电子邮件的主题。

点击此处查看一个简单示例:http://api.lullabot.com/drupal_mail

但是,如果您想跳过drupal_mail,可以使用:

<?php
    $message = array(
      'to' => 'example@mailinator.com',
      'subject' => t('Example subject'),
      'body' => t('Example body'),
      'headers' => array('From' => 'example@mailinator.com'),
    );

    drupal_mail_send($message);

请确保您通过t()传递所有文本,因为drupal_mail_send不会处理本地化。