- Issue created by @myDrupal2014_846824658246
- Status changed to Postponed: needs info
11 months ago 5:58pm 16 January 2024 - π©πͺ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 5:50pm 26 October 2024 - π©πͺ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.