Deliveres a noname file in the email as well

Created on 16 January 2024, 11 months ago

Problem/Motivation

Everything works ok but recieving the email in gmail contains also a file : noname which is 1 kb.

I checked the array and this one is good.

How to get rid of this noname file?

πŸ› Bug report
Status

Active

Version

1.4

Component

Code

Created by

πŸ‡³πŸ‡±Netherlands myDrupal2014_846824658246

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

Comments & Activities

  • Status changed to Postponed: needs info 11 months ago
  • πŸ‡©πŸ‡ͺGermany gogowitsch

    I suspect that the problem is in your particular Drupal installation or in your code. Online, there are many complaints about "noname" attachments that are not related at all to Drupal.

    Try to have a look what the file contains. It might be an inline image or a mime part added by another module.

  • πŸ‡³πŸ‡±Netherlands myDrupal2014_846824658246

    Hi,

    I created a test case.

    a) I send an email without attachments. The email is recieved perfectly in gmail.

    b) I add two files to the mail. I dubbelchecked and the output array is 100% matched with the code.

    Here is the source code:

    public function get_files_attached_from_email($emailid){

    // Based on example code on https://www.drupal.org/project/email_attachment β†’
    $node = \Drupal::entityTypeManager()->getStorage('node')->load($emailid);
    $files_nids = array();

    // Get the content of the file field of the content type
    foreach ($node->field_attach_file as $reference) {
    if ($reference->target_id != NULL){
    $files_nids[] = $reference->target_id;
    }
    }

    /*
    $params = ['attachments' => [
    [
    'filecontent' => 'Your attachment contents. You could use file_get_contents() instead of this string.',
    'filename' => 'attached_filename_how_recipients_see_it.txt',
    'filemime' => 'text/plain',
    ],
    [
    'filecontent' => file_get_contents('temporary://test.pdf'),
    'filename' => 'Invoice.pdf',
    'filemime' => 'application/pdf',
    ],
    ],
    // further params as needed by your hook_mail
    ];*/

    $filelist = array();
    foreach ($files_nids as $id) {

    // Load the file node
    // dump($id);
    $node = \Drupal::entityTypeManager()->getStorage('node')->load($id);
    //dump($node);

    foreach ($node->field_file as $reference) {
    dump($reference);
    $fileid = $reference->target_id;

    // Get each file within the file node fileds
    $f = \Drupal::entityTypeManager()->getStorage('file')->load($fileid);

    $file = array();
    $file['filecontent'] = file_get_contents(\Drupal::service('file_system')->realpath($f->getFileUri()));
    $file['filename'] = $f->getFilename();
    $file['filemime'] = $f->getMimeType();
    $filelist[] = $file;

    // dump($filelist);
    //dump($file);
    }
    }

    Which is send by code including a hook_mail who does nothing with the param array

    $node = Node::load($emailid);
    $node->field_send->value = FALSE;
    $node->field_date_last_send->value = $now->format('Y-m-d\TH:i:s');
    $node->save();

    // https://www.valuebound.com/resources/blog/how-to-send-mail-programmatica...
    // https://webazzle.com/knowledge-base/sending-e-mail-drupal-8
    // Set all parameters for the mail
    $mailManager = \Drupal::service('plugin.manager.mail');
    $module = xxxx
    $key =xxxx
    $to = $node->field_to->getString();
    $params['from'] = xxxxxxx
    $params['message'] = $node->get('body')->value;
    $params['subject'] = $node->title->value;
    $params['attachments'] = $this->get_files_attached_from_email($emailid);

    $langcode = \Drupal::currentUser()->getPreferredLangcode();
    $send = True;

    // Set content-type header for sending HTML email
    $headers = "From: " . strip_tags($params['from']) . "\r\n";
    $headers .= "Reply-To: ". strip_tags($params['from']) . "\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

    // Send the mail
    $result = $mailManager->mail($module, $key, $to, $langcode, $params, $params['from'], $send);

    Which specific suggestion can you do instead of a generic idea?
    Can you exclude the noname attachement by code in your module?

    Thanks !

  • πŸ‡³πŸ‡±Netherlands myDrupal2014_846824658246

    The content of the noname file of 1 kb is.

    This is a multi-part message in MIME format...

    ----

  • Status changed to Closed: cannot reproduce about 2 months ago
  • πŸ‡©πŸ‡ͺGermany gogowitsch

    Sorry that I don’t have a helpful answer here.

    As I cannot reproduce this issue locally, I will close this issue now.

Production build 0.71.5 2024