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

时间:2013-06-09 15:39:46

标签: php javascript email drupal email-attachments

我在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模块,现在他正在发送附件但没有Body和Subject邮件。我该怎么做才能发送它们?

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');



}
}

2 个答案:

答案 0 :(得分:2)

一些粗略的谷歌搜索显示它充其量是棘手的。

试试这堂课:

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

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

或者使用SwiftMailer:

http://swiftmailer.org

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

答案 1 :(得分:0)

我看不出您已经实施了hook_mail?您通常可以在此处设置电子邮件的主题和正文。这也在Swift Mailer documentation中描述。