Send attachment with Laravel SMTP and save to local storage - laravel

I read a similar topic in the forum solved, but it did not work successfully in me. Where am I making a mistake?
View
<input type="file" class="" name="document">
Send
Mail::send([], [], function ($message) use ($request) {
$message->to($request->to);
$message->subject($request->subject);
$message->setBody($request->message);
$data = $request->document;
$message->attach($data['document']->getRealPath(), array(
'as' => $data['document']->getClientOriginalName(),
'mime' => $data['document']->getMimeType()));
I want to send the attachment in this way, but it does not. Do I need to upload and then send it first?

There is a whole section about files in requests at official laravel's docs.
https://laravel.com/docs/7.x/requests#files
https://laravel.com/api/7.x/Illuminate/Http/UploadedFile.html
So, according to the above docs, the file you upload is (temporarly) saved when processing the request - you do not need to save it yourself.
$document_path = $request->document->path();
$message->attach($document_path, array(
'as' => $request->document->getClientOriginalName(),
'mime' => $request->document->getMimeType())
);

Related

how can i send http post requests from Laravel to Strapi CMS via Strapi's API

I tried sending post requests to the API in postman and it works fine. but i can't find a proper syntax of doing from Laravel, i tried following this tutorial from the Laravel documentation Http-client so i tried this syntax
function sendRequest (Request $data) {
$phonenumber = $data['number'];
$name = $data['name'];
$data2['data']['attributes'] = [
[
'phonenumber' => $phonenumber,
'name' => $name
]
];
Http::post('http://example.com/users', $data2);
}
didn't work.
here is the API's data structure
thanks in advance
I do not know if this will make a difference but you're missing a comma after value2 as per the syntax.
$response = Http::post('http://example.com/users', [
'name' => 'Steve',
'role' => 'Network Administrator', ]);
I'm rather new to laravel also and id love to know this fix too incase I need it in the future :D good luck!

Redirect response data to another view

im trying in my controller method to pass some data to a order sucess page, information regarding the details of payment, but i cant make it work or pass the data.
In my case i wish for example pass this request
$http = new \GuzzleHttp\Client;
$response = $http->request('POST', 'https://domain', [
'form_params' => [
'chave' => 'somekey',
'valor' => Cart::total(),
'id' => $order->id,
]
]);
$result = json_decode((string) $response->getBody(),true);
Cart::destroy();
return redirect()->route('frontend-cart-success')->with( ['data' => $result] );
And then in my view sucess page just calling the $data Info to show on my blade file.
But i cant it ut it work.
My route to pass in sucess page:
Route::get('cart/success/', 'Frontend\CartController#showSuccess')->name('frontend-cart-success');
Best regards
I code mostly in SPAs, but according to the API (https://github.com/laravel/framework/blob/5.7/src/Illuminate/Http/RedirectResponse.php#L42), it's flashing that data to the session, so you're going to have to get the data back out using the session.
See: https://laracasts.com/discuss/channels/laravel/redirect-to-route-with-data?page=1

When I send message from my website's contact form, which is build in Laravel, message is appearing as sent by me

Now, I'm building a contact form in Laravel. When I send message via the contact form, I'm taking messages by myself. And that message is appearing in Gmail spam folder.
I'm using my mail service which is connected to my domen. I'm forwarding messages to my gmail account.
My config:
namespace App\Http\Controllers;
use App\SendMessage;
use App\Mail\SendEmail;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\Http\Controllers\Controller;
use Session;
class SendMessageController extends Controller
{
public function store(Request $request) {
$this->validate($request, [
"email" => "required|email",
"message" => "min:10",
"subject" => "min:3"
]);
$data = array(
'email' => $request->email,
'name' => $request->name,
'company' => $request->company,
'subject' => $request->subject,
'bodyMessage' => $request->message
);
Mail::send('emails.contact', $data, function ($message) use ($data) {
$message->from($data['email']);
$message->to('audit#auditors.uz');
$message->subject($data['subject']);
});
Session::flash("success", "Ваше сообщение успешно отправлено.");
return back();
}
}
It is my view:
<div>
<p>{{ $bodyMessage }}</p>
</div>
<p> {{ $email }} tomonidan jo'natildi</p>
If somebody knows, why it is happening. Please, help me, I'm going crazy right now.
This is nothing to do with Laravel, this is because you are trying to send an email via your email server from a completely different user and domain. This would get it flagged up pretty easily. Basically, it looks like you are trying to spoof emails if website1.com is sending an email from user1#hotmail.com. It is not a genuine email from the server.
I would recommend using a generic email address on your server to send from, e.g. contact#mysite.com and include the user's email address in the reply-to field.

Unknown Payment Method Nonce

My setup is:
Laravel 4.2
Braintreepayments JS + PHP
Laravel Braintree
I tried to add a new customer as showed in the tutorial which works fine. But when I tried to add a payment method from the Front-End with the given code from the JavaScript SDK. I used the Drop-In-UI which generates me a Payment Method Nonce which is part of my question.
I configured the PHP Backend with the Sandbox credentials and pasted the example Code given in the docs.
When I try to create a user, everything's fine:
$result = Braintree_Customer::create(array(
'id' => Auth::id(),
'firstName' => 'Mike',
'lastName' => 'Jones',
'company' => 'Jones Co.',
'email' => 'mike.jones#example.com',
'phone' => '281.330.8004',
'fax' => '419.555.1235',
'website' => 'http://example.com'
));
As soon as it comes to the payment nonce, nothing works:
$result = Braintree_Transaction::sale(array(
'amount' => '10.00',
'paymentMethodNonce' => Input::get('payment_method_nonce'),
'customer' => array(
'id' => Auth::id()
),
'options' => array(
'storeInVaultOnSuccess' => true,
)
));
The server keeps saying 93108: Unknown paymentMethodNonce. This seems a little bit confusing and strange to me since the Input::get('payment_method_nonce') represents a valid string.
After a very long day with intensive studies of the Documentation i finally found the issue.
You need to pass the Client Token which is generated by the PHP Library (not the CSE Token from the Sandbox!) into the JavaScript snippet provided by Braintree:
braintree.setup(
"{{ Braintree_ClientToken::generate(['customerId' => Auth::id()]) }}",
'dropin', {
container: 'dropin'
}
);
The Drop-In-UI Code is copy+paste from the doc.
<form id="checkout" method="post" action="/checkout">
{!! csrf_field() !!}
<div id="dropin"></div>
<input type="submit" value="Pay $10">
</form>
Hopefully someone saves a lot of time with this answer provided.
Edit:
In Laravel 5 you need to manually add the csrf-field (I've updated the code), otherwise you will get a TokenMismatchException from Laravel.

CakePHP Observe field for validation check(already exists)

i have already user observe field in cakephp 1.3 Now i want to do the same thing in cakephp 2.0
I have one form.there is a one field named email. When user insert email. onblur there is a popup one message that displays "email already exists" or "right"
for that i have write below code in my projects. the below code is my add file code
<?php $options = array('url' => array( 'controller' => 'users', 'action' => 'is_exist'), 'update' => 'email_exist');
echo $this->ajax->observeField('UserEmailId',array('url' => array( 'controller' => 'users', 'action' => 'is_exist'), 'update' => 'email_exists')); ?>
In my controller i have created one function like
public function is_exist($id = null)
{
$result = "yes";
$this->set('existdata',$result);
}
i have also created is_exists.ctp fiel in view/uers.
i dont know why its not working.
i did the same thing in Cakephp 1.3 and its working file but not in cakephp 2.0
can anyone tell me how i implement this ?
thanks in advance
because ajax helper is not supported in cakephp 2.
you need to learn how to implement it using the js helper
check this link : js helper

Resources