Drupal 7 发送不带附件的电子邮件

Posted

技术标签:

【中文标题】Drupal 7 发送不带附件的电子邮件【英文标题】:Drupal 7 sends emails with out attachments 【发布时间】: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" margin topmargin="0" margin offset="0" bgcolor="#ffffff" >
               <span style="width:100%;float:left">
               <img style="width:20%; float:left" src="cid:logo"  />
               <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" margin topmargin="0" margin offset="0" bgcolor="#ffffff" >
               <span style="width:100%;float:left">
               <img style="width:20%; float:left" src="cid:logo"  />
               <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 邮件模块,对吧? 是的,我试过了..但是没用.. 【参考方案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 中也有描述。

【讨论】:

以上是关于Drupal 7 发送不带附件的电子邮件的主要内容,如果未能解决你的问题,请参考以下文章

spring 发送邮件代码示例(带附件和不带附件的)

如何向 Drupal 7 Organic Groups 角色成员发送电子邮件?

drupal 7中使用mime邮件模块的电子邮件

使用 python 3.7.3 发送带附件的电子邮件

Python发送邮件

无法通过 Drupal 8 发送电子邮件