使用drupal_mail发送附件


14

我正在尝试用Drupal的电子邮件发送附件。在我的自定义模块中,我添加了:

class SponsorprogramMailSystem implements MailSystemInterface {
  /**
   * Concatenate and wrap the e-mail body for plain-text mails.
   *
   * @param $message
   *   A message array, as described in hook_mail_alter().
   *
   * @return
   *   The formatted $message.
   */
  public function format(array $message) {
    $message['body'] = implode("\n\n", $message['body']);
    return $message;
  }
  /**
   * Send an e-mail message, using Drupal variables and default settings.
   *
   * @see http://php.net/manual/en/function.mail.php
   * @see drupal_mail()
   *
   * @param $message
   *   A message array, as described in hook_mail_alter().
   * @return
   *   TRUE if the mail was successfully accepted, otherwise FALSE.
   */
  public function mail(array $message) {
    $mimeheaders = array();
    foreach ($message['headers'] as $name => $value) {
      $mimeheaders[] = $name . ': ' . mime_header_encode($value);
    }
    $line_endings = variable_get('mail_line_endings', MAIL_LINE_ENDINGS);
    return mail(
      $message['to'],
      mime_header_encode($message['subject']),
      // Note: e-mail uses CRLF for line-endings. PHP's API requires LF
      // on Unix and CRLF on Windows. Drupal automatically guesses the
      // line-ending format appropriate for your system. If you need to
      // override this, adjust $conf['mail_line_endings'] in settings.php.
      preg_replace('@\r?\n@', $line_endings, $message['body']),
      // For headers, PHP's API suggests that we use CRLF normally,
      // but some MTAs incorrectly replace LF with CRLF. See #234403.
      join("\n", $mimeheaders)
    );
  }
}

我可以用html发送邮件,该部分正在工作。

但是,当我尝试附加文件时,它没有到达我的收件箱。我这样附上我的测试文件:

$attachment = array(
        'filecontent' => file_get_contents(DRUPAL_ROOT . '/README.txt'),
        'filename' => 'test.txt',
        'filemime' => 'text/plain',
      );

但是什么也没到达。

有人知道我该如何解决吗?


我不清楚您的示例中如何添加$ attachment。
大卫·梅斯特

Answers:


17

可能还有其他方法,但是我发现必须安装mailsystemmimemail模块才能发送带有附件的电子邮件。因此,请首先安装这两个模块。

然后实现hook_mail将附件传递给$ message

/**
 * Implements hook_mail().
 */
function mymodule_mail($key, &$message, $params) {
  $message['subject'] = $params['subject'];
  $message['body'][] = $params['body'];

  // Add attachment when available.
  if (isset($params['attachment'])) {
    $message['params']['attachments'][] = $params['attachment'];
  }
}

有两种添加附件的方法,您可以在添加非托管文件作为附件(未记录在DB中)时传递文件内容或文件路径,或者在添加托管文件时传递文件对象。

添加非托管文件时:

$attachment = array(
  'filepath' => $filepath, // or $uri
);

要么

$attachment = array(
  'filecontent' => file_get_contents($uri),
  'filename' => $filename,
  'filemime' => 'application/pdf'
);

通过使用filecontent方式,您可能会在2015年1月8日之前收到两个php错误,包括

添加托管文件时:

$attachment = file_load($fid);

然后通过以下方式发送电子邮件:

$params = array(
  'key' => 'my_email_template',
  'to' => 'test@example.com',
  'from' => 'test@example.com',
  'subject' => 'Test email',
  'body' => 'test',
  'attachment' => $attachment
);

drupal_mail('mymodule', $key, $to, $language, $params, $from);

是否需要设置任何标题?
siddiq '17

@siddiq无需设置任何标头
eric.chenchao 17-10-12

3
$attachment = array(
      'filecontent' => $filepathname,
      'filename' => $namefile,
      'filemime' => 'application/pdf'
      );
//where $filepathname should contain the path to the file and $filename should contain the name of the file.
$to = 'test@example.com'; // emails
$from = 'test@example.com';

$params = array(
  'headers' => array('Content-Type' => 'text/html'),
  'key' => 'test',
  'subject' => 'Test email',
  'body' => 'test',
  'attachment' => $attachment
);

drupal_mail($module, $key, $to, $language, $params, $from, $send = TRUE);

这对我有用。


在$ params中填充to和from似乎很奇怪,但是没有设置$ to和$ from...。我不确定这是否可行。
提请2015年

2

我记得我以前想做过,我尝试过并为我工作

function mymodule_mail($key, &$message, $params) {
  $data['user'] = $params['from'];
  $account = $data['user']->name;

  $file_content = file_get_contents('some/file/path');

  $attachments = array(
     'filecontent' => $file_content,
     'filename' => 'example-' . $account,
     'filemime' => 'application/pdf',
   );

  switch($key) {
    case 'notice':

      $langcode = $message['language']->language;
      $message = drupal_mail($module, $key, $to, $language, $params, $from, $send);
      $message['subject'] = 'example submission from '. $account;
      $message['body'][] =
        '<p>'. $account .' has submitted an example.</p>';
      $message['params']['attachments'][] = $attachments;
    $system = drupal_mail_system($module, $key);
    // Format the message body.
    $message = $system->format($message);
    // Send e-mail.
    $message['result'] = $system->mail($message);

    if($message['result'] == TRUE) {
        drupal_set_message(t('Your message has been sent.'));
    }
    else{
        drupal_set_message(t('There was a problem sending your message and it was not     sent.'), 'error');
    }
      break;
  }
}

1
file_get_contents()为我做了把戏。如果不使用它,我的文件附件将损坏。谢谢。
anou 2016年

@anou我很高兴两年后我的解决方案可以帮助另一个解决方案:D
Yusef
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.