admin管理员组

文章数量:1397102

When I try to send an email with CC, BCC, or both CC and BCC, I get the following error: "Cannot access offset of type string on string." If I send the email without CC or BCC, there is no error. I'm not sure what is wrong with the code, and I can't seem to identify the issue. When i do DD i see it all work fine, till it reach the Mail::send($mail)

<?php

namespace BackendPackage\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

    class CustomMail extends Mailable
    {
        use Queueable, SerializesModels;
    
        public string $toEmail;
        public string $fromEmail;
        public string $fromName;
        public $subject;
        public string $content;
        public string $template;
        public $cc;
        public $bcc;
    
        public function __construct(
            string $toEmail,
            string $fromEmail,
            string $fromName,
            array  $cc = [],
            array  $bcc = [],
            string $subject,
            string $content,
            string $template
        )
        {
            $this->toEmail = $toEmail;
            $this->fromEmail = $fromEmail;
            $this->fromName = $fromName;
            $this->cc = $cc;
            $this->bcc = $bcc;
            $this->subject = $subject;
            $this->content = $content;
            $this->template = $template;
        }
    
        public function build(): CustomMail
        {
    
            return $this->to($this->toEmail)
                ->from($this->fromEmail, $this->fromName)
                ->cc($this->cc)
                ->bcc($this->bcc)
                ->subject($this->subject)
                ->view($this->template)
                ->with([
                    'userName' => $this->fromName,
                    'userEmail' => $this->fromEmail,
                    'title' => $this->subject,
                    'body' => $this->content,
                    'url' => '',
                ]);
        }
    }

And this is my MailController

public function sendAction(Request $request): JsonResponse
{
    $validated = $request->validate([
        'to' => 'required|email',
        'from' => 'required|email',
        'cc' => 'nullable|array',
        'cc.*' => 'email',
        'bcc' => 'nullable|array',
        'bcc.*' => 'email',
        'subject' => 'required|string',
        'body' => 'required|string',
        'template' => 'nullable|string',
    ]);

    $receiver = $validated['to'];
    $sender = $validated['from'];
    $clientIp = $request->ip();
    $user = auth()->user();

    if (!$user && !$this->isWhitelisted($receiver, $sender, $clientIp)) {
        return $this->makeApiResponse(self::httpForbidden(), 'You are not allowed to send mail to this address');
    }

    $mail = new CustomMail(
        toEmail: $receiver,
        fromEmail: $sender,
        fromName: $sender,
        cc: $validated['cc'] ?? [],
        bcc: $validated['bcc'] ?? [],
        subject: $validated['subject'],
        content: $validated['body'],
        template: $validated['template'] ?? 'emails.notification.default'
    );

    Mail::send($mail);

    return $this->makeApiResponse(self::httpOk(), 'Mail sent');
}

When I try to send an email with CC, BCC, or both CC and BCC, I get the following error: "Cannot access offset of type string on string." If I send the email without CC or BCC, there is no error. I'm not sure what is wrong with the code, and I can't seem to identify the issue. When i do DD i see it all work fine, till it reach the Mail::send($mail)

<?php

namespace BackendPackage\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

    class CustomMail extends Mailable
    {
        use Queueable, SerializesModels;
    
        public string $toEmail;
        public string $fromEmail;
        public string $fromName;
        public $subject;
        public string $content;
        public string $template;
        public $cc;
        public $bcc;
    
        public function __construct(
            string $toEmail,
            string $fromEmail,
            string $fromName,
            array  $cc = [],
            array  $bcc = [],
            string $subject,
            string $content,
            string $template
        )
        {
            $this->toEmail = $toEmail;
            $this->fromEmail = $fromEmail;
            $this->fromName = $fromName;
            $this->cc = $cc;
            $this->bcc = $bcc;
            $this->subject = $subject;
            $this->content = $content;
            $this->template = $template;
        }
    
        public function build(): CustomMail
        {
    
            return $this->to($this->toEmail)
                ->from($this->fromEmail, $this->fromName)
                ->cc($this->cc)
                ->bcc($this->bcc)
                ->subject($this->subject)
                ->view($this->template)
                ->with([
                    'userName' => $this->fromName,
                    'userEmail' => $this->fromEmail,
                    'title' => $this->subject,
                    'body' => $this->content,
                    'url' => 'https://www.example',
                ]);
        }
    }

And this is my MailController

public function sendAction(Request $request): JsonResponse
{
    $validated = $request->validate([
        'to' => 'required|email',
        'from' => 'required|email',
        'cc' => 'nullable|array',
        'cc.*' => 'email',
        'bcc' => 'nullable|array',
        'bcc.*' => 'email',
        'subject' => 'required|string',
        'body' => 'required|string',
        'template' => 'nullable|string',
    ]);

    $receiver = $validated['to'];
    $sender = $validated['from'];
    $clientIp = $request->ip();
    $user = auth()->user();

    if (!$user && !$this->isWhitelisted($receiver, $sender, $clientIp)) {
        return $this->makeApiResponse(self::httpForbidden(), 'You are not allowed to send mail to this address');
    }

    $mail = new CustomMail(
        toEmail: $receiver,
        fromEmail: $sender,
        fromName: $sender,
        cc: $validated['cc'] ?? [],
        bcc: $validated['bcc'] ?? [],
        subject: $validated['subject'],
        content: $validated['body'],
        template: $validated['template'] ?? 'emails.notification.default'
    );

    Mail::send($mail);

    return $this->makeApiResponse(self::httpOk(), 'Mail sent');
}
Share Improve this question edited Mar 26 at 11:15 B.Simsek asked Mar 26 at 11:05 B.SimsekB.Simsek 1,1781 gold badge10 silver badges18 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

The cc and bcc might still be null or an empty string ("") when being passed, even though you assigned them as an array. Modify the constructor assignment to always ensure an array.

 $this->cc = is_array($cc) ? $cc : [];
 $this->bcc = is_array($bcc) ? $bcc : [];

本文标签: laravelCC and BCC give Cannot access offset of type string on string inStack Overflow