Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending an E-Mail with a different Reply-To in Laravel 5

Tags:

php

email

laravel

I have this email configurated in Laravel with SMTP. It works well.

I want some users to be able to send emails with their own e-mail address.

I used to do this:

Mail::to($receiver)->from("[email protected]")->send(new email());

I do this now:

Mail::to($receiver)->from($email_given_by_the_user)->send(new email());

This works fine but I don't like that because I am actually sending them from my e-mail, not from the email given by the user, even if the end user sees it as $email_given_by_the_user. I would like to send it as [email protected] but when the user wants to reply, it replies to $email_given_by_the_user. Is there any way to do this?

like image 499
prgrm Avatar asked Dec 13 '22 23:12

prgrm


1 Answers

In Laravel 5.4 Mailables, the replyTo, subject, cc, bcc and others can be set inside the mailable in the build method. This is also true for the to which can also be set on the Mail facade.

So you can do it somelike this:

$attributes = ['replyTo' => $email_given_by_the_user];    
Mail::to($receiver)->from("[email protected]")->send(new email($attributes));

and email class

class email extends Mailable
{
    public $attributes;

    public function __construct($attributes = null)
    {
        $this->attributes = $attributes;
    }

    public function build()
    {
        if(!empty($this->attributes['replyTo']))
            $this->replyTo($this->attributes['replyTo']);

        ...
    }

}

like image 71
Autista_z Avatar answered Jan 05 '23 17:01

Autista_z