Home > Software engineering >  Email attachment uses the wrong file extension
Email attachment uses the wrong file extension

Time:03-05

I am working on some code that will create an iCalendar and then send an Outlook email with an ics file of the created event, it works as intended but there is one problem.

When the email gets sent, the attachment is given the wrong name (ATT00001.bin instead of Meeting.ics) and it isn't sent as a .ics but as a .bin. The contents of the file are still just as they are supposed to be.

Any help is appreciated, thanks!

        use Spatie\IcalendarGenerator\Components\Calendar;
        use Spatie\IcalendarGenerator\Components\Event;
        ...

        $calendar = Calendar::create('Company test meeting')
            ->event(Event::create()
            ->name('Company test meeting')
            ->description('A test meeting about Company')
            ->startsAt(new \DateTime('24-03-2022 10:00'))
            ->endsAt(new \DateTime('24-03-2022 11:30'))
        )->get();
        
        $mailer = new Mailer('default');
        $mailer->setAttachments([
            'Meeting.ics' => [
                'data' => $calendar,
                'contentDisposition' => false
            ]
        ]);
        $mailer->setFrom(['[email protected]' => 'CompanyName'])
            ->setTo('[email protected]')
            ->setSubject('Company meeting')
            ->deliver('Hey there I would like to have a meeting about Company');

CodePudding user response:

So I figured it out. When I was making the email, I was using the cakephp 4 cookbook. There it said that: "The mimetype and contentId are optional in this form." and I made the mistake of just assuming that I didn't need it.

The mimetype for a ics file is text/calendar so I just added this to my setAttachments like this:

$mailer = new Mailer('default');
        $mailer->setAttachments([
            'Meeting.ics' => [
                'data' => $ical,
                'mimetype' => 'text/calendar', //I added the mimetype here
                'contentDisposition' => false
            ]
        ]);
        $mailer->setFrom(['[email protected]' => 'Company'])
            ->setTo('[email protected]')
            ->setSubject('Companymeeting')
            ->deliver("Dummy text " . $attendee . " About " . $description . "");

        $this->set('calendar', $ical);

I'm still working on how to get the right filename but that isn't as important as the file extension. But if anybody knows, I would love to know.

  • Related