blueink
blueink

Reputation: 69

Laravel 5.6 Sending Mail To and Bcc Diffrent subject and template

I have a problem to make diffrent mail template. When I got email from sender I would like to responce "Thank you" email to him or her. Sametime I would like to get the email details as diffrent type of tempate.

Here is my controller

class AjaxController extends Controller
{

    public function send(Request $request){

        $data = array(
                        'name'=>$request->name,
                        'email'=>$request->email,
                        'phone'=>$request->phone, 
                        'messagetext'=>$request->message
                    );

  Mail::send('contacttext', $data, function ($message) use ($request){


        $to_email = "[email protected]"; // webmaster 
        $to_name  = "webmaster";
        $subject  = "Thank you!";

        $message->subject ($subject);
        $message->from ($request->email, $request->name);
        $message->to ($request->email, $to_name);
        $message->bcc ('[email protected]')
        ->from ($request->email, $request->name)
        ->subject ('for web master ');

        if(count(Mail::failures()) > 0){
            $status = 'error';
        } else {
            $status = 'success';
        }    

        return response()->json(['response' => $status]);

    }

}

contacttext.blade.php

<html>
<head>Thank you! here is your summary</head>
<body>
<p><strong>name:</strong>   {{ $name }}</p>
<p><strong>email:</strong>  {{ $email }}</p>
<p><strong>message:</strong>        {{ $messagetext }}</p>
</body>
</html>

Email Sender can recive "Thank you" email. Webmaster can recive the email too but title and subject are overwrite as contacttext template Could someone teach me what is worng? I'm thinking it would be great to make as for webmaster template . I'm using Laravel 5.6

Upvotes: 1

Views: 748

Answers (1)

Tim Lewis
Tim Lewis

Reputation: 29278

It's not possible to change attributes of a CC or BCC, as that would invalidate it being a carbon copy. If you need to send a copy to an alternative address with a different subject, you will need to send it as a separate email:

public function send(Request $request){
    $data = array(
        "name" => $request->name,
        "email" => $request->email,
        "phone" => $request->phone, 
        "messagetext" => $request->message
    );

    Mail::send("contacttext", $data, function ($message) use ($request){
        $message->subject("Thank You!");
        $message->to($request->email);
        $message->from(config("mail")["from"]["address"]);
    }

    Mail::send("contacttext", $data, function ($message){
        $message->subject("For Webmaster");
        $message->to("[email protected]");
        $message->from(config("mail")["from"]["address"]);
    }

    return response()->json(["message" => "Email Sent!"], 200);
}

Notes: I'm using config("mail")["from"]["address"] as the sending address due to the mail being sent from within Laravel. This can be configured from config/mail.php

Also, to avoid waiting for the email to send before sending a response to the frontend (origin), move the response() outside of the Mail::send() function.

Upvotes: 2

Related Questions