【问题标题】:Drupal 7 sends emails with out attachmentsDrupal 7 发送不带附件的电子邮件
【发布时间】:2013-06-05 08:44:09
【问题描述】:

我在 Drupal 7 中发送带有附件的邮件时遇到问题。

我尝试了通常的模块,我尝试了 mimemail,我尝试了 Zend Framework(或类似的东西)......但它就是不起作用。我收到一封带有消息的电子邮件,但它不包含附件。

这是我的代码:

function my_form_submit() {
if(!empty($_POST['body'])) {
$postbody = $_POST['body'];
$userpost = $_POST['usermail'];


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


 $body = '  <html>
               <body leftmargin="0" marginwidth="0" topmargin="0" marginheight="0" offset="0" bgcolor="#ffffff" >
               <span style="width:100%;float:left">
               <img style="width:20%; float:left" src="cid:logo" alt="" />
               <div style="width:80%; float:left">
               </div></span>
               <span style="width:100%; float:left">'.$_POST['body'].'</span>
               </body></html>';

        $my_module = 'mime';
        $my_mail_token = 'notice';


  $message = array(
   'to' => '"'.addslashes(mime_header_encode('Request')) .'"<'.$_POST['mail'].'>',
   'subject' => t('[Hinnaparing]'),
   'body' => $body,
   'headers' => array(
   'From' => 'noreply@test.com',
   'MIME-Version' => '1.0',
   'Content-Type' => 'text/html;charset=utf-8',
   'Content-Transfer-Encoding' => '8Bit',
      'X-Mailer' => 'Drupal',
   ),
        );
       $message['headers']['CC'] = '<'.$_POST['usermail'].'>';
      $message['params']['attachments'][] = $attachment;

          $system = drupal_mail_system($my_module, $my_mail_token);
        if ($system->mail($message)) {
          // Success.
        }
        else {
   // Failure.
  }
}
}

我安装了 SwiftMailer drupal 模块,现在他正在发送附件,但没有邮件的正文和主题。我应该怎么做才能发送它们?

function my_form_submit() {
if(!empty($_POST['body'])) {
$postbody = $_POST['body'];
$userpost = $_POST['usermail'];
$ourpost = $_POST['mail'];



 $body = '  <html>
               <body leftmargin="0" marginwidth="0" topmargin="0" marginheight="0" offset="0" bgcolor="#ffffff" >
               <span style="width:100%;float:left">
               <img style="width:20%; float:left" src="cid:logo" alt="" />
               <div style="width:80%; float:left">
               <h1>Hinnaparing № '.$file.'</h1>
               </div></span>
               <span style="width:100%; float:left">'.$_POST['body'].'</span>
               </body></html>';


  //File two (not managed by Drupal).
  $fileone = new stdClass();
  $fileone->uri = 'sites/default/files/034.jpg';
  $fileone->filename = 'drupal_logo.jpg';
  $fileone->filemime = 'image/jpeg';

  // Add attachments.
  $p['files'][] = $fileone;

  // Send e-mail.
  drupal_mail('modulename', 'key',$userpost, language_default(), $p,'noreply@test.com');
 drupal_mail('modulename', 'key',$ourpost, language_default(), $p,'noreply@test.com');



}
}

【问题讨论】:

  • 来吧,使用更多 Drupal代码。
  • 对不起,我不是 Drupal 大师,所以也许你会帮助改进我的代码?
  • 您已经安装并启用了 Drupal mime 邮件模块,对吧?
  • 是的,我试过了..但是没用..

标签: php javascript email drupal email-attachments


【解决方案1】:

粗略的谷歌搜索表明它充其量是棘手的。

试试这个课程:

https://github.com/gollyg/Attachment-Email

(通过:http://www.metachunk.com/blog/sending-e-mails-attachments-drupal-7

或者使用 SwiftMailer:

http://swiftmailer.org

我不确定后者是否会成为 Drupal 8 的一部分,但至少会是 compatible

【讨论】:

  • 这个类对我不起作用,但我安装了带有 drupal 模块的 SwiftMailer (drupal.org/project/swiftmailer)。它现在正在发送附件,但没有主题和正文,您可以看到我的编辑..
  • 老实说,我对 Drupal 或其 SwiftMailer 模块不够熟悉,无法提供更多帮助。但是 FWIW,看起来您并没有设置 $subject$body
  • 是的,我意识到了:)...这就是问题所在,我找不到应该设置主题和正文的地方..
【解决方案2】:

我看不到您已经实现了hook_mail?这是您通常设置电子邮件主题和正文的地方。这在Swift Mailer documentation 中也有描述。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-23
    • 2012-06-15
    • 1970-01-01
    • 1970-01-01
    • 2015-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多