简体   繁体   中英

how to edit email body before sending using Laravel

I have generated email function, that sends email to multiple people using laravel.

Now I want to generate an edit window so that i can write the body of email,like in Gmail, if we are sending mail, we first edit body and hit send mail.

So, if anyone know how can I implement this, leave a comment.

It should be as simple as

Mail::send([], array('yourValue' => $yourValue), function($message) use ($yourValue) {
    $MailBody = 'Your Custom Body';
    $message->setBody($MailBody, 'text/html');
    $message->to('yourtoaddress@yourdomain.com');
    $message->subject('Your Custom Subject');
    });

Though I am fairly new to Laravel myself, I could try to help you out with this. Firstly, set up your routes in the Routes.php file. For eg

Route::get('myapp/sendEmail', 'EmailController@returnComposeEmail');
Route::post('myapp/sendEmail', 'EmailController@sendEmail');

The first route when visited should return a view to the user where he can compose his email. This basically is a form which will be submitted by the POST method when the user clicks the 'Send' button. The second route is for that method which would collect the submitted data and use it appropriately then to send the email.

If you go by the routes I have provided, you should have a controller file named EmailController.php with the following methods:

public function returnComposeEmail()
{
return view('pages.ComposeEmail');
}
public function sendEmail(Request $input)
{
    $input = $input->all();
    $dataArray = array();
    $dataArray['emailBody'] = $input['emailBody'];
    $to = $input['to'];
    $subject = $input['subject'];
    Mail::send('email.body', ['dataArray' => $dataArray], function ($instance) use ($to, $subject)
        {
            $instance->from(env('MAIL_USERNAME'), 'Your Name Here');
            $instance->to($to, 'Recipient Name');
            $instance->subject($subject);
            $instance->replyTo(env('MAIL_REPLY_TO', 'some@email.id'), 'Desired Name');
        });
}

You may use use the $dataArray in the email/body.blade.php file as is or as per your requirement.

Do let me know if I could be of help. :-)

Controller:

    public function showForm(Request $request )
    {
        //Get Content From The Form
        $name = $request->input('name');
        $email = Input::get('agree');
        $message = $request->input('message');

        //Make a Data Array
        $data = array(
            'name' => $name,
            'email' => $email,
            'message' => $message
        );

        //Convert the view into a string
        $emailView = View::make('contactemail')->with('data', $data);
        $contents = (string) $emailView;

        //Store the content on a file with .blad.php extension in the view/email folder
        $myfile = fopen("../resources/views/emails/email.blade.php", "w") or die("Unable to open file!");
        fwrite($myfile, $contents);
        fclose($myfile);

        //Use the create file as view for Mail function and send the email
        Mail::send('emails.email', $data, function($message) use ($data) {
            $message->to( $data['email'], 'Engage')->from('stifan@xyz.com')->subject('A Very Warm Welcome');
        });
//        return view();
    }

Routes:

Route::post('contactform', 'ClientsController@showForm');
Route::get('/', 'ClientsController@profile');

The view contactemail has the data to be sent, and the view email we are sending through mail function. When the user puts data in the form, that data will get saved in email.blade.php because of these lines of code:

//Convert the view into a string
        $emailView = View::make('contactemail')->with('data', $data);
        $contents = (string) $emailView;

        //Store the content on a file with .blad.php extension in the view/email folder
        $myfile = fopen("../resources/views/emails/email.blade.php", "w") or die("Unable to open file!");
        fwrite($myfile, $contents);
        fclose($myfile);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM