Hey guys, I would like to send image uploads as an attachment to an email in a return form.Unfortunately it doesn’t quite work yet.
With my code the images are added directly to the email, but not as an attachment.Do you have any tips or does anyone know where the error might be?
Shopware 6.4.20
Thanks !
``/**
* @Route("/return/form/submit", name="test.return.form.submit", methods={"POST"}, defaults={"_csrf_protected"=true})
*/
public function submitReturnForm(Request $request, SalesChannelContext $salesChannelContext): Response
{
$formData = $request->request->get('data');
$files = $request->files->get('files'); $email = $formData['email'] ?? null;
$invoiceNumber = $formData['invoiceNumber'] ?? 'Unbekannte Rechnungsnummer';
$articleDetails = $formData['articleDetails'] ?? 'Keine Artikelnummer angegeben';
$returnReason = $formData['reason'] ?? 'Kein Grund angegeben';
$resolution = $formData['resolution'] ?? 'Keine Resolution angegeben';
$message = $formData['comment'] ?? '';
$senderName = 'Rückgabeformular - ' . ($formData['kundennummer'] ?? 'Unbekannte Kundennummer'); if (!$email) {
$this->addFlash('danger', 'E-Mail-Adresse ist erforderlich.');
return $this->redirectToRoute('frontend.home.page');
} $attachments = [];
foreach ($files as $file) {
if ($file instanceof UploadedFile && $file->isValid()) {
$attachments[] = [
'content' => file_get_contents($file->getPathname()),
'fileName' => $file->getClientOriginalName(),
'mimeType' => $file->getClientMimeType(),
];
}
} $additionalInfo = "
<strong>Rückgabeinformationen:</strong><br>
Kundennummer: {$formData['kundennummer']}<br>
Rechnungsnummer: $invoiceNumber<br>
Artikelnummer(n) und Menge(n): $articleDetails<br>
Grund: $returnReason<br>
Erstattung: $resolution<br><br>
"; $contentHtml = $additionalInfo . "<strong>Kommentar:</strong><br>" . nl2br($message);
$contentPlain = strip_tags($additionalInfo) . "\n\nKommentar:\n" . $message; $mailParameters = [
'recipients' => [
'testmail@web.de' => 'Test'
],
'senderName' => $senderName,
'subject' => 'Rückgabeformular - ' . ($formData['kundennummer'] ?? 'Unbekannte Kundennummer'),
'contentHtml' => $contentHtml,
'contentPlain' => $contentPlain,
'binAttachments' => $attachments,
'salesChannelId' => $salesChannelContext->getSalesChannel()->getId(),
]; $this->mailService->send($mailParameters, $salesChannelContext->getContext()); $this->addFlash('success', 'Ihre Rückgabeanfrage wurde erfolgreich gesendet.');
return $this->redirectToRoute('frontend.home.page');
} `