Laravel mail: how to add user's form answers to mail - php

Basically , I have a form and I want to send confirmation link after form posted , also I want to add all form fields and answers of users .I stored all data in my controller's store function and in the same function I wrote this mail function . However, I could not manage to add user's answers .
Mail::send('isim',['name'=>'email'],function($message){
$message->to('mail','mail')- >from('email','name')->subject('welcome');
});

Assuming that your user model has attributes name and a relationship to fetch answers called answers, You can do the following.
Here, Mail::send('isim',['name'=>$user->name,'answers'=>$user->answers] will inject $name and $answers to your view. You can access them there.
use ($user) will inject the $user variable to the function inside where you set the receiver's email address.
$user= //fetch user from the database as you wish
Mail::send('isim',['name'=>$user->name,'answers'=>$user->answers],function($message) use ($user){
$message->to('mail',$user->email)->from('email',"Your email address")->subject('welcome');
});

Related

PHP Laravel Sending email inside variable to Mail::raw

I've been having this issue where I want to send a plaintext email to a user (me in this case) but the email adress is inside of an object I retrieved from the database. All examples using Mail::raw use a string but not a passed variable. Is there a way to do that?
Code:
Mail::raw($messageContent, function($message)
{
$message->to($foo->email);
});
This is what I want, in essence. $foo is a variable declared in the function that this is in. I don't want to make $foo a class variable because I am only using it here.
Thanks for helping!
Mail::raw($suggestionString, function($message) use ($follower)
{
$message->to('support#company.com')->subject('Suggestion')->replyTo($follower->email);
});
When you reply to this message it's automatically sent to the email specified in ->replyTo()

Validation email on url

Im creating a survey that is sent by newsletter email, and basically the app records the user data based on the email of the user, if the email isnt present in the route isnt possible to fill in the survey, but im not quite sure if im doing it right way and also for security purpose maybe i should have some kind of validation regarding the email. Can someone suggest me what is the best practise or the way im doing is already alright?!
The url that the users enter is like:
http://domain.com/surveys/23/email#hotmail.com/show
Here is my code:
Route:
Route::get('surveys/{id}/{email}/show/', 'SurveyController#show');
Controller:
public function show($id,$email)
{
$survey = Survey::find($id);
$email = $email;
return view('admin.surveys.show', compact('survey','email'));
}
View:
Html
...
#if(!empty($email))
show the survey form
#else
A message saying is not possibile fill without a email
#endif
Note: The survey is completelly a part from the newsletter system, it cannot have any kind of integration between them.
Definitely you should validate if e-mail was provided. In controller you should do check like this:
$this->validate($request, [
'email' => 'required|email|exists:users,email',
]);
Example above will make sure that e-mail was provided (required), if is actually an email and if it exists in database (table users, column email).
You can read more about this in documentation. On this page you can also check all available rules if I missed any that could be used.
EDIT: Please also remember to add "Request $request" as method parameter, like so:
public function show(Request $request, $id,$email) {...}
Regarding Larans sugegstion to inject the Request as well.. Maybe you don't need it in the show method. If you feel it messes with your code, maybe inject it in the constructor...
private $request;
public function __construct(Request $request)
{
$this->request = $request;
}
And do the validation in your show method,

Email Builder Laravel

I'm using Laravel 5.3 to build an application which allows admins to build template emails to be used within the system for email address verification, forgot password etc. The template emails are built using a form which creates a new record in the database. Once the templates have been created they can be used within the application using the following command
Mail::to($user)->queue(new MailEmailTemplate($emailtemplate, $user));
where $emailtemplate is a model with the following fillable fields (there is nothing else worth disclosing within the model)
$fillable = ['from_name', 'from_email','subject','body','email_type','status'];
In order to allow the emails to be personalised to the user which they are being sent to (i.e. 'Dear John' at the top of the email body) I need to allow admins to enter variables into the form.
An example of how the form body should be filled out by the system admins:
Dear {{$user->first_name}}
Image of the form that admins will use to create the email template
The problem I am having is when the emails are sent the variables are not injected into the HTML sent to the client. i.e in the example above the recipient receives the email without the variables inserted
Dear {{$user->first_name}} as opposed to Dear John.
I have added the following route in my app/routes/web.php file
Route::get('/email_templates/{emailtemplate}/send/{user}', 'emailTemplatesController#send')->name('email_templates.send');
my App/Http/Controllers/emailTemplatesController.php
...
use App\Mail\MailEmailTemplate;
class EmailTemplatesController extends Controller
{
...
public function send(EmailTemplate $emailtemplate, User $user)
{
Mail::to($user)->queue(new MailEmailTemplate($emailtemplate, $user));
return back();
}
}
my app/Mail/MailEmailTemplate.php Mailable
use App\Models\User;
use App\Models\EmailTemplate;
use Session;
class MailEmailTemplate extends Mailable
{
use Queueable, SerializesModels;
public $user;
public $emailtemplate;
public function __construct(EmailTemplate $emailtemplate, User $user)
{
$this->emailtemplate = $emailtemplate;
$this->user = $user;
}
public function build()
{
$data['user'] = $this->user;
$data['email_template'] = $this->emailtemplate;
return $this->subject('testing')
->view('emails.index')
->with($data);
}
}
and finally the resources/views/emails/index.blade.php file (which is used to dislpay the email to the recipient)
{!! $email_template->body !!}
I've used unescaped output here as the text that is stored in the body field is HTML (admins will be developing the email template as a laravel 'view' in a text editor (phpStorm) then copy the code from the view into the form field for the body to store it within the application).
How can I modify this to allow the variables from the $user object to be inserted in place of the variables specified in the $emailtemplate->body?
If I modify the resources/views/emails/index.blade.php to the following the correct output can be achieved
{!! str_replace('<<$user->first_name>>',$user->first_name, $email_template->body)!!}
however this method is inefficient as it would require a str_replace to be used for any variable to be used within the emails. Note I've had to use << >> tags in the $email_template->body to prevent a parse error.
Update:
Could variable variables be used in conjunction with a regex to find every instance of {{ }} tags inside the $email_template->body and treat the text contained inside as a variable variable?
I apologise for the long question - I am relatively new to Laravel and wanted to make sure you guys could scrutinise my code structure.
Appreciate your help.
Jordan
try using
{{$email_template->body}}

Laravel 5 login without password

I'm using an API wherein I want to login the user. The only information that I have is his/her email.
How will I be able to authenticate that user without having to insert his/her password?
Here's the code that I tried.
public function linkedinLogin()
{
$email = json_decode($_POST['email']);
Session::put('linkedin_email', $email);
//$value = session('linkedin_email');
if (Session::has('linkedin_email')) Auth::login(Session::get('linkedin_email'));
return $value;
}
I believe that you have to pass App\User to the Auth::login function, but you are passing an email address. This will not work.
What I suggest is doing one of two things:
Find the ID of the user associated with that linkedin_email and use the Auth::loginUsingId(id) to authenticate the user. or,
Find the user associated with that linkedin_email and pass the $user to the Auth::login function.
For Example:
$user = App\User::where('linkedin_email', $value)->get()->first();
Auth::login($user);

How can I get the form data from the FOSUserBundle registration form?

I am using the FOSUserBundle and have overwritten the RegistrationController. When the form is submitted and valid, I want to get the email address the user entered in the registration form.
But I don't see any way to get it. As taken from the Symfony2 forms documentation, you can get form data like this:
$this->get('request')->request->get('name');
But the RegistrationController does not know the get() method (as it's not inherited from the Symfony2 controller entity). So I could go like this:
// Note the ...->container->...
$this->container->get('request')->request->get('name');
But this returns NULL. Now I try to get it from the $form.
// Does contain a lot of stuff, but not the entered email address
$form->get('email');
// Does also contain a lot of stuff, but not the desired content
$request->get('email');
$request->request('email');
// Throws error message: No method getData()
$request->getData();
Any idea?
It's really, really simple. You create a form with related entity. In FOSUserBundle you should have a RegistrationFormHandler, and in process method you've got:
$user = $this->createUser();
$this->form->setData($user);
if ('POST' === $this->request->getMethod()) {
$this->form->bind($this->request);
if ($this->form->isValid()) /**(...)**/
After the line $this->form->bind($this->request) every value in $user object is overwritten by data from form. So you can use $user->getEmail().
On the other hand you are able to get data directly from request, but not by the property name, but by form name. In FOSUserBundle registration form it is called fos_user_registration - you can find it in FOS/UserBundle/Form/Type/RegistrationFormType.php in getName method.
You get it by:
$registrationArray = $request->get('fos_user_registration');
$email = $registrationArray['email'];
If you were to use Controller as a Service (which should work with this), you could pass the RequestStack (sf >=2.4) in the constructor and do $this->request_stack->getCurrentRequest()->get();
My guess is that you are trying to get the POST data. You are trying to put the data in a form object I presume. I would recommend you to take a look at: http://symfony.com/doc/current/book/forms.html if you have a custom form.
As regarding to your question, the form is probably containing a name. If you want to have direct access to it instead of doing things in your form, you need to fetch it multilevel if directly via the true at $deep, get('registration_form_name[email]', null, true); You can also do $email = $request->get('registration_form_name')['email']; (if you have php 5.4+)

Categories