Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

viktor3177's avatar

Laravel 9 A copy of the letter to the client does not arrive through PHPMailer.

Hello! Connected (PHPMailer) to the site, but a copy of the letter to the client does not come! As an administrator, everything comes to me, but not to the client.

public function StoreReservation(Request $request) {
    $reservations = Reservation::create([
        'firstname' => $request -> firstname,
        'lastname' => $request -> lastname,
        'company' => $request -> company,
        'email' => $request -> email,
        'email_repeat' => $request -> email_repeat,
        'tel' => $request -> tel,
        'created_at' => Carbon::now(),
    ]);


    $notification = array(
        'message' => 'Your Reservation Submited Seccessfully',
        'alert-type' => 'success'
    );

    $mail = new PHPMailer(true);
    $to_email = '[email protected]';
        //Server settings
        $mail->SMTPDebug = SMTP::DEBUG_SERVER;
        $mail->isSMTP();
        $mail->Host       = 'smtp.hosting.com';
        $mail->SMTPAuth   = true;
        $mail->Username   = '[email protected]';
        $mail->Password   = '123456789';
        $mail->SMTPSecure = 'TLS';
        $mail->Port       = 465;

        //Recipients
        $mail->setFrom($to_email);
        $mail->addAddress($reservations->email);

        //Content
        $mail->isHTML(true);
        $mail->Subject = 'laravel Reservation';

        Mail::send('mail', compact('reservations'), function ($message) use ($reservations, $to_email) {
            $message->to("[email protected]", 'username')->subject('laravel reservation');
        });

        $mail2 = new PHPMailer(true);
        $mail2->CharSet = 'UTF-8';
        $mail2->IsHTML(true);
        $mail2->setFrom($to_email);
        $mail2->addAddress($reservations->email);

        $mail2->Subject = "laravel reservation client";

        Mail::send('mail_client', compact('reservations'), function ($message) use ($reservations) {
            $message->to($reservations->email)->subject('laravel reservation client');
        });

    return redirect()->back()->with($notification);

}
0 likes
68 replies
Sinnbeck's avatar

Just curious. Where do you set the client? Both emails are sent to the same address?

$mail->addAddress($reservations->email);
//vs
$mail2->addAddress($reservations->email);
1 like
viktor3177's avatar

@Sinnbeck

I understand that it is in this piece of code that I have an error, but I don’t understand how to get the copy to go to the client! :(

1 like
viktor3177's avatar

@Sinnbeck

Right here.

public function StoreReservation(Request $request) {
    $reservations = Reservation::create([
        'email' => $request -> email,
    ]);

}
Sinnbeck's avatar

@viktor3177 So both emails are to that person? The reservation is the client? And none of the two emails arrive? So mail does not work at all?

viktor3177's avatar

@Sinnbeck

The form is working. The letter comes to me as an administrator. But does not come to the client. And here (mailtrap.io) both copies come at once.

viktor3177's avatar

@Sinnbeck

Not in spam either! It turns out that it comes to the admin's mail, but not to the client's mail.

rodrigo.pedra's avatar

@Sinnbeck

https://mailtrap.io/ is a test mail server for development.

There is no spam box there as one of its features is to score your mail as a spam.

It will intercept any e-mails sent through its SMTP, much like how MailHog works

Sinnbeck's avatar

@rodrigo.pedra Yeah I know :) As I understood it, everything works when sending to mailtrap, but when using the real mailserver only 1 recipient gets the mail. Maybe I misunderstood?

rodrigo.pedra's avatar

@Sinnbeck no I guess you got it right.

My impression is that:

  1. OP is not aware Laravel can add multiple recipients
  2. On their PHPMailer code they are adding one recipient and expecting the email is sent to two.

Note that despite the variable is called $to_email, this variable is used on the $mail->setFrom() only -- which is not a recipient -- and they only call $mail->addAdress() once.

So only one email is expected to be sent, isn't it?

Sinnbeck's avatar

@rodrigo.pedra Yeah they send 1 using laravel and one using phpmailer :) So as you already suggested it should be a matter of just fixing that code :)

rodrigo.pedra's avatar

@viktor3177

$mail->setFrom() will not add a recipient. The from of an email is the sender. So, from your code, only one email should be expected to be sent.

But please see my other answer below, on how to use Laravel's mailer to send it to multiple recipients.

rodrigo.pedra's avatar

@Sinnbeck I thought that was somewhat demo code to show the issue.

The PHPMailer instances are never sent up there, just configured.

hcb's avatar

Is there a specific requirement so that you are usign PHPMailer? because there is mail support out the box in Laravel.

viktor3177's avatar

@hcb

When sending an application from the site, 2 copies of the letter should leave, one for me as an admin and the second for the client! I couldn't do it with laravel.

Snapey's avatar

@viktor3177 Of course you can absolultely do it with the out of the box mailer or notifications

viktor3177's avatar

@Snapey

Through Laravel, I could not connect 2 mails. So I tried using (PHPMailer) but that doesn't work either. I understand that the problem is somewhere in this code, but I can't figure out how to fix it.

$mail2 = new PHPMailer(true);
    $mail2->CharSet = 'UTF-8';
    $mail2->setLanguage('en', 'phpmailer/language/');
    $mail2->IsHTML(true);
    $mail2->setFrom($to_email);
    $mail2->addAddress($reservations->email);

    $mail2->Subject = "laravel Reservation";

    Mail::send('mail_client', compact('reservations'), function ($message) use ($reservations) {
        $message->to($reservations->email)->subject('laravel reservation client');
    });
rodrigo.pedra's avatar

@viktor3177 in this code:

$mail2 = new PHPMailer(true);
    $mail2->CharSet = 'UTF-8';
    $mail2->setLanguage('en', 'phpmailer/language/');
    $mail2->IsHTML(true);
    $mail2->setFrom($to_email);
    $mail2->addAddress($reservations->email);

    $mail2->Subject = "laravel Reservation";

    Mail::send('mail_client', compact('reservations'), function ($message) use ($reservations) {
        $message->to($reservations->email)->subject('laravel reservation client');
    });

PHPMailer instance is not actually sending anything, as you never call $mail2->send().

If any mail is being sent, is through Laravel's mailer.

Are you aware of that right?

rodrigo.pedra's avatar

@viktor3177

Doesn't work, email doesn't arrive

Where are you expecting it to arrive?

As you are using https://mailtrap.io/ every sent email will be intercepted there, and not actually delivered to anyone else.

Can you clear your mailtrap inbox, try my code above again, and check how many emails were delivered to the mailtrap's inbox?

viktor3177's avatar

@Sinnbeck

There is (') in the code. Yes, that's right, a copy of the letter must be sent to the client's mail from ($reservations->email).

Mail::send('mail_client', compact('reservations'), function ($message) use ($reservations) {
	$message
		->to($reservations->email) //Mail Client
		->to('[email protected]') //Mail Admin
		->cc('[email protected]')
		->cc('[email protected]')
		->subject('laravel reservation client');
});
viktor3177's avatar

I don’t understand why the code doesn’t work :(. Now both copies are sent to the admin’s mail.

$to_name = 'Reservation';
    $to_email = '[email protected]';
    Mail::send('mail', compact('reservations'), function ($message) use ($reservations, $to_email) {
        $message->from("[email protected]", 'username');
        $message->to($to_email)->subject('laravel reservation');
    });

    Mail::send('mail_client', compact('reservations'), function ($message) use ($reservations) {
        $message
            ->to($reservations->email)
            ->to('[email protected]')
            ->cc('[email protected]')
            ->cc('[email protected]')
            ->subject('laravel reservation client');
    });
rodrigo.pedra's avatar

@viktor3177

Well you are sending the first email to only your admin mail (it is both the sender ->from() and the recipient ->to()).

And the second is also sent to your admin e-mail, along the others.

So the admin email should receive two copies.

In your mailtrap.io account, how many e-mails does it receive? If your mailtrap.io account receive all expected e-mails, as it should, then your code is working fine.

Sending mail in real life, and get it delivered, can have many other issues besides just getting the code right, as @snapey already mentioned.

viktor3177's avatar

@rodrigo.pedra

2 letters arrive and in real life 2 letters also come to the admin mail. I tried to write (addAddress) in (->addAddress($reservations->email)), but for some reason it does not work.

rodrigo.pedra's avatar

@viktor3177

And when using mailtrap.io, how many emails arrive?

Test it with your personal email hardcoded, maybe $reservation->email is not filled with the data you expect, as @snapey suggested earlier but you seemed to ignore or to not have understood.

Please try as we suggest so we can better help you find the issue. It is very hard when asking one thing and you answer with another or just with half of what was asked:

Mail::send('mail_client', compact('reservations'), function ($message) {
        $message
            ->to('[email protected]')
            ->to('[email protected]') // <<< REPLACE WITH YOUR PERSONAL EMAIL
            ->subject('laravel reservation client');
    });

Please post mailtrap.io screenshot, you can upload the screenshot to a GitHUb Gist, and copy the image's link here.

viktor3177's avatar

@rodrigo.pedra

Now I have such a code and 2 letters come to the admin mail. But it is necessary that a copy of the letter comes to the client's mail.

$to_email = '[email protected]';
    Mail::send('mail', compact('reservations'), function ($message) use ($reservations, $to_email) {
        $message->from("[email protected]", 'username');
        $message->to($to_email)->subject('laravel reservation');
    });

    Mail::send('mail_client', compact('reservations'), function ($message) use ($reservations) {
        $message
            ->to($reservations->email) //Mail Client
            ->to('[email protected]') //Mail Admin
            ->cc('[email protected]')
            ->cc('[email protected]')
            ->subject('laravel reservation client');
    });
rodrigo.pedra's avatar

@viktor3177 did you read my last message?

Specially this :

Please try as we suggest so we can better help you find the issue. It is very hard when asking one thing and you answer with another or just with half of what was asked.

Posting the same code you already said you have tried, and not signaling if you tested last suggestions nor actually answering any questions made, is not the best use of neither my time, neither yours.

As you are not willing to test my suggestions nor to cooperate, I wish you the best luck on solving your issue.

Have a nice day =)

rodrigo.pedra's avatar

@viktor3177

This is very weird, as in the last snippet only e-mail is being sent.

  • Is your admin email forwarding messages to your personal email?
  • Did you tested only the code suggested?
  • Or did you also keep the old code, so we can't be sure which one is working?

Maybe you may have some other issue on your installation.

I tested on a fresh install and it is working as expected when using a real SMTP.

I made a public repository to show it. All the code is in ./routes/web.php file. Besides it there are just simple views, and of course the .env file.

https://github.com/rodrigopedra/laracasts-issue-email

Please read the README.md file for installation instructions.

By default, for testing it is using the log email driver, this is a sample output:

[2022-10-04 16:52:39] local.DEBUG: From: Laravel <[email protected]>
Subject: sample mail
To: [email protected], [email protected]
MIME-Version: 1.0
Date: Tue, 04 Oct 2022 16:52:39 +0000
Message-ID: <[email protected]>
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable

<html>
<body>
<h1>Sample email</h1>

<p>2022-10-04 16:52:39</p>
</bo=
dy>
</html>

Note the to: email header lists both emails. Change the emails on the ./routes/web.php to real ones, add a real SMTP configuration on the .env file, and then test it.

I did it with two personal emails (mine and my wife's) and both email received the message as expected.

Sorry for not being able to look further into it. Good luck, and have a nice day =)

1 like
Snapey's avatar

Its not hard. If you want two different emails then send two emails.

But if you want one email with one recipient and a copy to the admin then put the admin as a BCC.

Only one mailer is required, called once, with two recipients.

Test with Mailtrap or log first. Check that both emails are sent.

Note that there are MANY reasons why an email might not be recieved by the recipient, the server sending is only the first step. Subsequent steps involve using a respected mail host, which has valid SPF and DKIM records so that your mail is not just consigned to the spam bucket

1 like
viktor3177's avatar

@Snapey

Letters are sent here no problem. And 2 copies come to the mail, both on (mailtrap) and in real life.

And since 2 copies are sent to real mail, it means that I made a mistake somewhere since the letter goes only to the admin, but not to the client.

viktor3177's avatar

@Snapey

Here is the whole code

public function StoreReservation(Request $request) {
    $reservations = Reservation::create([
        'firstname' => $request -> firstname,
        'lastname' => $request -> lastname,
        'company' => $request -> company,
        'email' => $request -> email,
        'email_repeat' => $request -> email_repeat,
        'tel' => $request -> tel,
        'created_at' => Carbon::now(),
    ]);


    $notification = array(
        'message' => 'Your Reservation Submited Seccessfully',
        'alert-type' => 'success'
    );

    $to_name = 'Reservation';
    $to_email = '[email protected]';
    Mail::send('mail', compact('reservations'), function ($message) use ($reservations, $to_email) {
        $message->from("[email protected]", 'username');
        $message->to($to_email)->subject('laravel reservation');
    });

    Mail::send('mail_client', compact('reservations'), function ($message) use ($reservations) {
        $message
            ->to($reservations->email)
            ->to('[email protected]')
            ->cc('[email protected]')
            ->cc('[email protected]')
            ->subject('laravel reservation client');
    });


    return redirect()->back()->with($notification);

}
Snapey's avatar

@viktor3177 Still doesn't answer the question. the email field might be empty (especially since you don't appear to do any validation)

Snapey's avatar

@viktor3177 All I am asking is that you dump the $reservation and check that it actually contains an email address

Snapey's avatar

@viktor3177 You add to your code dd($reservation) after you created it and then you look at the result and see if it contains the email you typed on the form.

Do you really have no validation?

viktor3177's avatar

I'm trying to check, but it gives an error!

$reservations = $request->validated(); dd($reservations);

Method Illuminate\Http\Request::validated does not exist.

viktor3177's avatar

I've been struggling with this problem for five days now.

Willing to pay for help to solve this problem! :(

Snapey's avatar
public function StoreReservation(Request $request) {
    $reservations = Reservation::create([
        'firstname' => $request -> firstname,
        'lastname' => $request -> lastname,
        'company' => $request -> company,
        'email' => $request -> email,
        'email_repeat' => $request -> email_repeat,
        'tel' => $request -> tel,
        'created_at' => Carbon::now(),
    ]);

	dd($reservations);    // <----- here, just add this
viktor3177's avatar

@Snapey

#original: array:9 [▼
	"firstname" => "Viktor"
	"lastname" => "Venrjgo"
	"company" => "554555"
	"email" => "[email protected]"
	"email_repeat" => "[email protected]"
	"tel" => "44444"
	"updated_at" => "2022-10-03 21:22:36"
	"created_at" => "2022-10-03 21:22:36"
	"id" => 73
  ]
viktor3177's avatar

I tried to write like this, but it also gives an error. Undefined array key "viktor3177@gmail.com"

$to_email = '[email protected]';
    Mail::send('mail', compact('reservations'), function ($message) use ($reservations, $to_email) {
        $message->from("[email protected]", 'username');
        $message->to($to_email)->subject('laravel reservation');
    });
    $email = $_POST[$reservations->email];
    Mail::send('mail_client', compact('reservations'), function ($message) use ($reservations, $email) {
        $message
            ->addAddress($email)
            ->to('[email protected]')
            ->cc('[email protected]')
            ->cc('[email protected]')
            ->subject('laravel reservation client');
    });
ekpono's avatar

You are expecting $email = $_POST[$reservations->email];

But what you actually got is $POST[null]

Also, why are you using $POST[null] when your Request $request has that already.

As @snapey said, you really need validation in place to guard against this and also, seriously consider taking up a full Laravel course, that will save you more time than trying to figure it out yourself without convering up the basis.

viktor3177's avatar

@ekpono

I study Laravel diligently, but I didn’t find more than one video with this problem, and even in the lectures I bought there are no such topics as connecting a second mail! And the method that I know through the PCR does not want to work here.

This is the code I have now and both copies come to my admin mail.

$to_email = '[email protected]';
    Mail::send('mail', compact('reservations'), function ($message) use ($reservations, $to_email) {
        $message->from("[email protected]", 'username');
        $message->to($to_email)->subject('laravel reservation');
    });

    Mail::send('mail_client', compact('reservations'), function ($message) use ($reservations) {
        $message
            ->to($reservations->email)  //Mail CLIENT
            ->to('[email protected]') //Mail ADMIN
            ->subject('laravel reservation client');
    });

Please or to participate in this conversation.