drupal电子邮件问题

发布于 2024-10-19 11:50:40 字数 3126 浏览 0 评论 0原文

下面是我的联系方式模块,

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

我的代码片段中是否有任何错误

<?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 = '[email protected]';
  $from = '[email protected]';
  $subject = st('New Drupal site created!');

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

?>

Below is my contactus module,

am trying to send email, guess email not sending successfully,

is there any mistake in my snippet

<?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 = '[email protected]';
  $from = '[email protected]';
  $subject = st('New Drupal site created!');

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

?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

穿越时光隧道 2024-10-26 11:50:40

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

看一下这里的一个简单示例: http://api.lullabot.com/drupal_mail

但是,如果,你想跳过 drupal_mail 你可以使用这个:

<?php
    $message = array(
      'to' => '[email protected]',
      'subject' => t('Example subject'),
      'body' => t('Example body'),
      'headers' => array('From' => '[email protected]'),
    );

    drupal_mail_send($message);

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

You're calling drupal_mail with the wrong parameters. The correct way to send mail in Drupal requires you to implement the hook_mail hook where, based on a key and some parameters sent by drupal_mail you return the title and the subject of the email.

Take a look here for a simple example: http://api.lullabot.com/drupal_mail

If, however, you want to skip drupal_mail you can use this:

<?php
    $message = array(
      'to' => '[email protected]',
      'subject' => t('Example subject'),
      'body' => t('Example body'),
      'headers' => array('From' => '[email protected]'),
    );

    drupal_mail_send($message);

Make sure you pass all text through t() since drupal_mail_send doesn't take care of localisation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文