I don't if you are aware, but laravel comes with built in support for mail (symfony mailer) https://laravel.com/docs/9.x/mail#main-content
There is no reason to implement this yourself :)
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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);
}
I don't if you are aware, but laravel comes with built in support for mail (symfony mailer) https://laravel.com/docs/9.x/mail#main-content
There is no reason to implement this yourself :)
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);
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! :(
@viktor3177 And what is the email of the client? In what variable?
Right here.
public function StoreReservation(Request $request) {
$reservations = Reservation::create([
'email' => $request -> email,
]);
}
@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?
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 So maybe it ended up in spam. Check the mail logs to see if it was delivered
Not in spam either! It turns out that it comes to the admin's mail, but not to the client's mail.
@viktor3177 Yes you said that. Check the mail logs on the smtp server
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
@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?
@Sinnbeck no I guess you got it right.
My impression is that:
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?
Yes that's right!
@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 :)
$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.
@Sinnbeck I thought that was somewhat demo code to show the issue.
The PHPMailer instances are never sent up there, just configured.
@rodrigo.pedra Oh damn you are right!
All controller code is at the top.
@viktor3177 Look at the answer by @rodrigo.pedra further down the thread, where he says you can use multiple ->to with the laravel mailer.
Is there a specific requirement so that you are usign PHPMailer? because there is mail support out the box in Laravel.
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.
@viktor3177 Of course you can absolultely do it with the out of the box mailer or notifications
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');
});
Through Laravel, I could not connect 2 mails.
Why not?
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');
});
Doesn't work, email doesn't arrive
@viktor3177 can you show your .env file.
We are interested only on the variable staring with MAIL_
@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?
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?
MAIL_MAILER=smtp
MAIL_HOST=1234567.host.com
MAIL_PORT=465
[email protected]
MAIL_PASSWORD=123456789
MAIL_ENCRYPTION=SSL
MAIL_FROM_ADDRESS="[email protected]"
MAIL_FROM_NAME="${APP_NAME}"
No, I didn't know.
I test not only on ( https://mailtrap.io/), but also on a live server.
I just prescribed it and now 2 copies come to the admin's mail.
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');
});
@viktor3177 so is it working now?
2 copies come, but both to the admin's mail!
This is how I wrote
->to($reservations->email) //Mail Client
->to([email protected]') //MaIL admin
@viktor3177 so what's in the reservations table?
@viktor3177 I assume it is an edit error, but that code is missing a '
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');
});
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');
});
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.
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.
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.
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');
});
@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 =)
I did as you wrote, but both letters still come to me. And nothing comes to the client's mail.
Mail::send('mail_client', compact('reservations'), function ($message) {
$message
->to('[email protected]')
->to('[email protected]')
->subject('laravel reservation client');
});
Was the email delivered to both email addresses? [email protected] and viktor3177@gmail.com ?
Both letters came to [email protected].
This is very weird, as in the last snippet only e-mail is being sent.
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 =)
Unfortunately, I don’t know how to upload files to (https://github.com), but I posted my site here. https://drive.google.com/file/d/1as5hWTtx3tAzCtj2OZBAWJHu2RHOLsXK/view
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
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 and I asked, but no reply, what is contained in $reservations->email
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);
}
@viktor3177 Still doesn't answer the question. the email field might be empty (especially since you don't appear to do any validation)
How can I then correctly register the mail for the client?
@viktor3177 All I am asking is that you dump the $reservation and check that it actually contains an email address
But how to do it? I'm just new to this.
Here is the form https://bdreservation.website/reservation
@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 I really recommend that you watch these from beginning to end multiple times before you do anything else.
I'm trying to check, but it gives an error!
$reservations = $request->validated(); dd($reservations);
Method Illuminate\Http\Request::validated does not exist.
I've been struggling with this problem for five days now.
Willing to pay for help to solve this problem! :(
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
#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
]
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');
});
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.
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.