Home > Software design >  How to update a field in a database via email?
How to update a field in a database via email?

Time:01-20

When a user subscribes to news from the site, he receives a welcome letter in his mail with a question, does he really want to subscribe to news?

Also in the letter there is a button that confirms his consent to the newsletter.

How can I make it so that when I click this button from an email, the values ​​​​in my database are updated?

This is my mail form welcome.blade.php

Welcome, User
<form action="{{route('welcome', $data->hash)}} method="POST">@csrf
    <button type="submit">Click me</button>
</form>

Controller

public function welcome($hash) {
    \DB::table('config')->where('hash', $hash)->update(['agree' => 1]);

Route

Route::post('welcome', 'WelcomeController@welcome')->name('welcome')

CodePudding user response:

Embedding forms in emails is not allowed/recommended. It is a security risk. Email clients will simply warn the recipients of potential danger and will disable the form.

You need to add a link to your application in the email content.

<a href="http://yourapp.com/add-consent/{$token}">click me</a>

When a user will click on the URL below route will hit.

Route::post('/add-consent/{token}', 'WelcomeController@welcome')->name('welcome');

In the action identify user based on token and perform the action.

public function welcome($token) {
// Identify user based on token and perform the action...
    \DB::table('config')->where('hash', $token)->update(['agree' => 1]);
}
  •  Tags:  
  • Related