5

I've got problems with sending emails after updating a Laravel 8 project using the Metronic 8 theme to Laravel 9. I didn't change any of my code related to emails, but now I got this error using the Sendmail driver :

An email must have a "To", "Cc", or "Bcc" header. {"userId":6,"exception":"[object] (Symfony\Component\Mime\Exception\LogicException(code: 0): An email must have a "To", "Cc", or "Bcc" header. at /home/myhome/public_html/myproject.com/vendor/symfony/mime/Message.php:128)

Controller

class MyController extends Controller
{
    public function activate($id)
    {
        $mymodel = MyModel::find($id);

        $contact = Contact::where('mymodel_id', $mymodel->id)->first();
        Mail::to($contact->email)->send(new MyMail($contact));
    }
}

Mailable

class MailActive extends Mailable
{
    use Queueable, SerializesModels;

    protected $contact;

    public function __construct(Contact $contact)
    {
        $this->contact = $contact;
    }

    public function build()
    {
        return $this->from('theemail@gmail.com', 'Me')
            ->view('emails.myemailview')
            ->with([
                'contact' => $this->contact
            ]);

        // $this->withSymfonyMessage(function (Email $message) {
        //     $message->getHeaders()->addTextHeader(
        //         'addAddress', 'theemail@gmail.com'
        //     );
        // });

        // return $this;
    }
}

I've tried to render the mail alone, and it works. Using the log driver, it works, and as you can see, I even tried to bypass the Mail facade using the withSymfonyMessage method in the mailable class, but nothing works.

Is there a config or a part I'm missing? Could I go back using SwiftMailer without much trouble for Laravel 9 to work?

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
Ankh
  • 61
  • 1
  • 7
  • In your upgrade process can you install these packages? `composer require symfony/mailgun-mailer symfony/http-client` – Jinal Somaiya Feb 17 '22 at 11:16
  • What have you tried to resolve the problem? As far as I see, you are not setting any receiver in the given code? – Nico Haase Feb 17 '22 at 15:10

5 Answers5

5

use ->to()

return $this->from('theemail@gmail.com', 'Me')
            ->to($email, $name)
            ->view('emails.myemailview')
            ->with([
                'contact' => $this->contact
            ]);
  • It works like that yes ! The Mail facade doesn't take the to() anymore and we have to specify the address in the Mailable class ? – Ankh Feb 17 '22 at 12:47
  • I am using Laravel 10. where to include this snippet ? I don't have any `build()` – mr-possible May 11 '23 at 16:29
1

An email must have a "To", "Cc", or "Bcc" header. {"userId":6,"exception":"[object] (Symfony\Component\Mime\Exception\LogicException(code: 0): An email must have a "To", "Cc", or "Bcc" header. at /home/myhome/public_html/myproject.com/vendor/symfony/mime/Message.php:128)

You will get the above exception if to() is missing or if the to() method receive an empty collection in your Mailable class

Hemerson Varela
  • 24,034
  • 16
  • 68
  • 69
0

I am using Laravel 10+ and doing just this while calling Mail facade, worked for me:

\Mail::to('to@example.com')->send(new MyMailableClass());

P.S. I am using MailTrap to test my emails for this.

Hope it helps!

mr-possible
  • 109
  • 1
  • 9
-2

to() mail address is null given or cache issue

cache issue: php artisan optimize:cl php artisan config:cache php artisan config:cl

Romit
  • 1
  • 1
    Please add some explanation to your answer such that others can learn from it. What makes you think that this is a caching issue? – Nico Haase Mar 28 '23 at 12:40
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 02 '23 at 12:21
-2

go to .env file and just add:

MAIL_TO_ADDRESS="example@gmail.com"

  • Please add some explanation to your answer such that others can learn from it. Shouldn't the mail be sent to a varying mail address instead of a static one? – Nico Haase Aug 14 '23 at 07:36