Hello,

How to send email with attachments ?

I have write the following code in a submit form :
I received the mail but without the attachment...

 $values = $form_state['values']; //result from my form
  
  $mail_message = 'test';

  $module = 'my_module';
  $key = 'contact';

  $to = 'mail_to@mail.com';
  $from = 'mail_from@mail.com';

  $params = array(
    'subject' => 'Contact ' . transliteration_get($values['type']) . ' - ' . transliteration_get($values['subject']),
    'message' => $mail_message,
  );

//my attachment
  if(isset($values['file_1']) && $values['file_1'] != 0)
  {
    $file = file_load($values['file_1']); //form return fid, I load file for infos
    $file_content = file_get_contents(file_create_url($file->uri));

      $attachment = array(
         'filecontent' => $file_content,
         'filename' => $file->filename,
         'filemime' => $file->filemime,
       );
    $params['attachments'][] = $attachment;
  }

  $language = language_default();

  $send = TRUE;
  $result = drupal_mail($module, $key, $to, $language, $params, $from, $send);
  if ($result['result'] == TRUE) {
    drupal_set_message(t('Your message has been accepted'));
  }
  else {
    drupal_set_message(t('There was a problem sending your message and it was not sent.'), 'error');
  }

My code in my_module_mail (hook_mail) :

switch ($key) {
    case 'contact':
      $headers = array(
        'MIME-Version' => '1.0',
        'Content-Type' => 'text/html; charset=UTF-8;',
        'X-Mailer' => 'Drupal',
      );
      foreach ($headers as $key => $value) {
          $message['headers'][$key] = $value;
      }
      
      $message['subject'] = t($params['subject'], $options);
      $message['body'][] = $params['message'];

      $message['params']['attachments'] = $params['attachments'];

      break;
  }

Should we change the content type ? If I set "multipart/mixed", MailjetSmtpMailSystem asks a boundary...

Thanks for your help.

Comments

olorin.fr created an issue. See original summary.

mailjet plugins’s picture

Hello, please check out this link:
http://drupal.stackexchange.com/questions/101035/send-attachments-with-d...

Best regards,
Mailjet Team

olorin.fr’s picture

Hello,

I have already seen and tested this example.
That works (I receive attachments) with "mailsystem" and "mimemail" but not with "mailjet" enabled. If I choose mailjet interface class or / and mailjet mime class from mail system settings, it doesn't work.

In my case, mailjet doesn't change content type (text/html) of the mail when I add attachments.

Do you have an example with attachments with mailjet ? In the source, we see that attachments are implemented, but no result.

Thanks

ken hawkins’s picture

StatusFileSize
new19.84 KB

We're also seeing this issue appear on a Drupal Commerce site.

We generate an invoice PDF (commerce_billy) and also send legal terms in a separate e-mail as a PDF attachment through Rules ("Send HTML e-mail" action). Both of these methods fail and generate an error relating to not being able to write the tmp file system.

E-mails are still sent, just without attachments.

We're also using Mimemail — and aside from attachments the Mailjet module worked fine.

As a workaround we're sending e-mail through Mailjet using the SMTP module (and not using the Mailjet module at all).

abramm’s picture

Assigned: olorin.fr » Unassigned
Status: Active » Closed (outdated)

D7 reached its EOL back in January 2025, and there is no active release for D7 for this module anymore.
Development or support is not planned for D7. All D7-related issues are marked as outdated in a bunch.

Everyone can apply the patch above (not tested by the maintainers, tested by the community) to their D7 projects.
If the issue remains relevant for D10+ versions, merge requests with proposed solutions for a new module version (D10+) are welcome in a new follow-up issue.

Thanks!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.