Unable to send attachment with mail using ->mailAttachment() function #3842
Unanswered
MattWiseParking
asked this question in
Q&A
Replies: 2 comments
-
I'm having almost the same problem. <?php
declare(strict_types=1);
namespace App\Mail;
use App\Enums\MediaCollectionName;
use App\Models\Email;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Address;
use Illuminate\Mail\Mailables\Attachment;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
final class CommunicationMessageMail extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*/
public function __construct(
public Email $email,
) {}
/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
subject: $this->email->subject,
replyTo: $this->email->reply_to ? [
new Address($this->email->reply_to),
] : [],
);
}
/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
markdown: 'mail.communication-message',
);
}
/**
* Get the attachments for the message.
*
* @return array<int, Attachment>
*/
public function attachments(): array
{
/**
* @var array<int, Attachment> $attachments
*/
$attachments = $this->email->getMedia(MediaCollectionName::ATTATCHMENT->value)
->map(
fn (Media $media): Attachment => Attachment::fromPath($media->getPath())
->as($media->file_name)
->withMime($media->mime_type)
)
->toArray();
return $attachments;
}
} This here is the important part, i created the attachment myself no using the mailAttachment /**
* Get the attachments for the message.
*
* @return array<int, Attachment>
*/
public function attachments(): array
{
/**
* @var array<int, Attachment> $attachments
*/
$attachments = $this->email->getMedia(MediaCollectionName::ATTATCHMENT->value)
->map(
fn (Media $media): Attachment => Attachment::fromPath($media->getPath())
->as($media->file_name)
->withMime($media->mime_type)
)
->toArray();
return $attachments;
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
Hoow can i do this as im using s3 storage disk? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I am unable to create a bug issue hence i have created a new discussion.
If you attach a media file to an email but use the ->mailAttachment() function, this errors with the following:
TypeError: The body of "Symfony\Component\Mime\Part\TextPart" must be a string, a resource, or an instance of "Symfony\Component\Mime\Part\File" (got "null"). in /var/app/current/vendor/symfony/mime/Part/TextPart.php:50 Stack trace: #0 /var/app/current/vendor/symfony/mime/Part/DataPart.php(41): Symfony\Component\Mime\Part\TextPart->__construct() #1 /var/app/current/vendor/symfony/mime/Email.php(334): Symfony\Component\Mime\Part\DataPart->__construct() #2 /var/app/current/vendor/laravel/framework/src/Illuminate/Mail/Message.php(328): Symfony\Component\Mime\Email->attach() #3 /var/app/current/vendor/laravel/framework/src/Illuminate/Mail/Mailable.php(483): Illuminate\Mail\Message->attachData() #4 /var/app/current/vendor/laravel/framework/src/Illuminate/Mail/Mailable.php(213): Illuminate\Mail\Mailable->buildAttachments() #5 /var/app/current/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php(316): Illuminate\Mail\Mailable->Illuminate\Mail{closure}() #6 /var/app/current/vendor/laravel/framework/src/Illuminate/Mail/Mailable.php(206): Illuminate\Mail\Mailer->send() #7 /var/app/current/vendor/laravel/framework/src/Illuminate/Support/Traits/Localizable.php(19): Illuminate\Mail\Mailable->Illuminate\Mail{closure}() #8 /var/app/current/vendor/laravel/framework/src/Illuminate/Mail/Mailable.php(199): Illuminate\Mail\Mailable->withLocale() #9 /var/app/current/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php(354): Illuminate\Mail\Mailable->send() #10 /var/app/current/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php(301): Illuminate\Mail\Mailer->sendMailable() #11 /var/app/current/vendor/laravel/framework/src/Illuminate/Mail/PendingMail.php(124): Illuminate\Mail\Mailer->send() #12 /var/app/current/app/Jobs/SendWelcomeMailJob.php(35): Illuminate\Mail\PendingMail->send() #13 /var/app/current/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): App\Jobs\SendWelcomeMailJob->handle() #14 /var/app/current/vendor/laravel/framework/src/Illuminate/Container/Util.php(43): Illuminate\Container\BoundMethod::Illuminate\Container{closure}() #15 /var/app/current/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(95): Illuminate\Container\Util::unwrapIfClosure() #16 /var/app/current/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(35): Illuminate\Container\BoundMethod::callBoundMethod() #17 /var/app/current/vendor/laravel/framework/src/Illuminate/Container/Container.php(696): Illuminate\Container\BoundMethod::call() #18 /var/app/current/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(126): Illuminate\Container\Container->call() #19 /var/app/current/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(170): Illuminate\Bus\Dispatcher->Illuminate\Bus{closure}() #20 /var/app/current/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(127): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}() #21 /var/app/current/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(130): Illuminate\Pipeline\Pipeline->then() #22 /var/app/current/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(126): Illuminate\Bus\Dispatcher->dispatchNow() #23 /var/app/current/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(170): Illuminate\Queue\CallQueuedHandler->Illuminate\Queue{closure}() #24 /var/app/current/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(127): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}() #25 /var/app/current/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(121): Illuminate\Pipeline\Pipeline->then() #26 /var/app/current/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(69): Illuminate\Queue\CallQueuedHandler->dispatchThroughMiddleware() #27 /var/app/current/vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php(102): Illuminate\Queue\CallQueuedHandler->call() #28 /var/app/current/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(442): Illuminate\Queue\Jobs\Job->fire() #29 /var/app/current/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(392): Illuminate\Queue\Worker->process() #30 /var/app/current/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(178): Illuminate\Queue\Worker->runJob() #31 /var/app/current/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(149): Illuminate\Queue\Worker->daemon() #32 /var/app/current/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(132): Illuminate\Queue\Console\WorkCommand->runWorker() #33 /var/app/current/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): Illuminate\Queue\Console\WorkCommand->handle() #34 /var/app/current/vendor/laravel/framework/src/Illuminate/Container/Util.php(43): Illuminate\Container\BoundMethod::Illuminate\Container{closure}() #35 /var/app/current/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(95): Illuminate\Container\Util::unwrapIfClosure() #36 /var/app/current/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(35): Illuminate\Container\BoundMethod::callBoundMethod() #37 /var/app/current/vendor/laravel/framework/src/Illuminate/Container/Container.php(696): Illuminate\Container\BoundMethod::call() #38 /var/app/current/vendor/laravel/framework/src/Illuminate/Console/Command.php(213): Illuminate\Container\Container->call() #39 /var/app/current/vendor/symfony/console/Command/Command.php(279): Illuminate\Console\Command->execute() #40 /var/app/current/vendor/laravel/framework/src/Illuminate/Console/Command.php(182): Symfony\Component\Console\Command\Command->run() #41 /var/app/current/vendor/symfony/console/Application.php(1094): Illuminate\Console\Command->run() #42 /var/app/current/vendor/symfony/console/Application.php(342): Symfony\Component\Console\Application->doRunCommand() #43 /var/app/current/vendor/symfony/console/Application.php(193): Symfony\Component\Console\Application->doRun() #44 /var/app/current/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(198): Symfony\Component\Console\Application->run() #45 /var/app/current/artisan(35): Illuminate\Foundation\Console\Kernel->handle() #46 {main}
The code against the mail file:
The docs explain that returning the Attachment of the mail using ->mailAttachment() can be used against the attachments array, however when I do I receive the above error.
using Laravel version 11
Originally posted by @MattWiseParking in #3819
Beta Was this translation helpful? Give feedback.
All reactions