Attachments not showing in received email

Created on 8 April 2022, over 2 years ago
Updated 26 October 2024, 26 days ago

I have a custom rest endpoint that gets called from a mobile app. It allows the user to receive a CSV file via email of their "favorite" contacts. I am trying to use email_attachment module to send the csv file attachment in email. The csv file does get attached, however, the email body is also getting attached as a file called "noname". When I *dont* include an email attachment, the body gets placed into the email in the correct place, not as an attachment. It only gets turned into an attachment when I have other attachments added.

My hook_mail() function:

function my_favorites_mail($key, &$message, $params) {
  switch ($key) {
    case 'mail_contacts_csv':
      $message['from'] = 'noreply@mysite.com';
      $message['reply-to'] = 'noreply@mysite.com';
      $message['subject'] = t('Your Favorite Contacts are Attached');
      $message['body'][] = 'Attached to this email, you\'ll find the CSV of your favorite contacts.';
      $message['params']['files'] = $params['attachments'];
    break;
  }
}

The code that sets up the attachment and triggers the mail:

      $file_contents = "\"Full Name\",\"Job Title\",\"Company Name\",Email,Phone\n\"John Smith\",\"Sr. Advisor, Marketing\",\"ABC Company\",somebody@abccompany.com,\" \"\n\"Jane Black\",\"Director, Marketing\",\"XYZ America\",jane@xyzamerica.com,123-456-7890\n";
      $params = [
        'attachments' => [[
          'filecontent' => $file_contents,
          'filename' => 'my_favorite_contacts.csv',
          'filemime' => 'text/csv',
        ]],
      ];
      $mail_manager = \Drupal::service('plugin.manager.mail');
      $result = $mail_manager->mail('my_favorites', 'mail_contacts_csv', $this->currentUser->getEmail(), $this->currentUser->getPreferredLangcode(), $params);

The $result variable from the above code gets assigned this data:

{
    "id": "my_favorites_mail_contacts_csv",
    "module": "my_favorites",
    "key": "mail_contacts_csv",
    "to": "me@mysite.com",
    "from": "noreply@mysite.com",
    "reply-to": "noreply@mysite.com",
    "langcode": "en",
    "params": {
        "attachments": [
            {
                "filecontent": "\"Full Name\",\"Job Title\",\"Company Name\",Email,Phone\n\"John Smith\",\"Sr. Advisor, Marketing\",\"ABC Company\",somebody@abccompany.com,\" \"\n\"Jane Black\",\"Director, Marketing\",\"XYZ America\",jane@xyzamerica.com,123-456-7890\n",
                "filename": "my_favorite_contacts.csv",
                "filemime": "text/csv"
            }
        ],
        "files": [
            {
                "filecontent": "\"Full Name\",\"Job Title\",\"Company Name\",Email,Phone\n\"John Smith\",\"Sr. Advisor, Marketing\",\"ABC Company\",somebody@abccompany.com,\" \"\n\"Jane Black\",\"Director, Marketing\",\"XYZ America\",jane@xyzamerica.com,123-456-7890\n",
                "filename": "my_favorite_contacts.csv",
                "filemime": "text/csv"
            }
        ]
    },
    "send": true,
    "subject": "Your Favorite Contacts are Attached",
    "body": "This is a multi-part message in MIME format.\r\n--qd-90939554823bf9e71e1f022c1ddbabb2\r\nContent-Type: text/plain; charset=UTF-8; format=flowed; delsp=yes \r\n\r\nAttached, you'll find the CSV of your favorite contacts.\r\n\r\n--qd-90939554823bf9e71e1f022c1ddbabb2\r\nContent-Type: text/csv;\r\n  name*0=\"my_favorite_contacts.csv\"\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment;\r\n  filename*0=\"my_favorite_contacts.csv\"\r\n\r\nIkZ1bGwgTmFtZSIsIkpvYiBUaXRsZSIsIkNvbXBhbnkgTmFtZSIsRW1haWwsUGhvbmUKIlNoZXJp\r\nIEphbW1hbGxvIiwiU3IuIEFkdmlzb3IsIE1hcmtldGluZyIsIkNhbm9uIFNvbHV0aW9ucyBBbWVy\r\naWNhIixzamFtbWFsbG9AY3NhLmNhbm9uLmNvbSwiICIKIlRvbnlhIFBvd2VycyIsIkRpcmVjdG9y\r\nLCBNYXJrZXRpbmciLCJDYW5vbiBTb2x1dGlvbnMgQW1lcmljYSIsdHBvd2Vyc0Bjc2EuY2Fub24u\r\nY29tLDU2MS05OTctMzMxOAo=\r\n\r\n\r\n--qd-90939554823bf9e71e1f022c1ddbabb2--",
    "headers": {
        "MIME-Version": "1.0",
        "Content-Type": "multipart/mixed; boundary=\"qd-90939554823bf9e71e1f022c1ddbabb2\"",
        "Content-Transfer-Encoding": "8Bit",
        "X-Mailer": "Drupal",
        "Return-Path": "noreply@mysite.com",
        "Sender": "noreply@mysite.com",
        "From": "My Site <noreply@mysite.com>"
    },
    "result": true
}

I've also tried putting the body in $params instead of in the hook_mail() function, and this had the same result:

      $params = [
        'attachments' => [[
          'filecontent' => $file_contents,
          'filename' => 'my_favorite_contacts.csv',
          'filemime' => 'text/csv',
        ]],
        'message' => 'Attached, you\'ll find the CSV of your favorite contacts.'
      ];

Then, I tried assigning just an empty array to $params before calling ->mail(). The body then showed up in the email how it's supposed to (not as attachment), however, this doesn't solve my problem of needing to have a normal body AND a file attachment. This is $result when not including an attachment:

{
    "id": "my_favorites_mail_contacts_csv",
    "module": "my_favorites",
    "key": "mail_contacts_csv",
    "to": "me@mysite.com",
    "from": "noreply@mysite.com",
    "reply-to": "noreply@mysite.com",
    "langcode": "en",
    "params": {
        "files": null
    },
    "send": true,
    "subject": "Your Favorite Contacts are Attached",
    "body": "Attached, you'll find the CSV of your favorite contacts.",
    "headers": {
        "MIME-Version": "1.0",
        "Content-Type": "text/plain; charset=UTF-8; format=flowed; delsp=yes",
        "Content-Transfer-Encoding": "8Bit",
        "X-Mailer": "Drupal",
        "Return-Path": "noreply@mysite.com",
        "Sender": "noreply@mysite.com",
        "From": "My Site <noreply@mysite.com>"
    },
    "result": true
}

I've also included a screenshot of the 2 emails here. The first one showing with the attachment but the body is in a file called noname. And, the second one shows the body showing up normal but no attachments.

Is there something I am doing wrong? How do I remedy this? I'm not sure if this is a bug in email_attachment or if I'm just implementing something wrong.

πŸ› Bug report
Status

Closed: cannot reproduce

Version

1.1

Component

Miscellaneous

Created by

πŸ‡ΊπŸ‡ΈUnited States cmcnamee

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024