I am trying to send mail with Mailgun. If I write this:
$mg = Mailgun::create('xxxx');
$mg->messages()->send('xxxx', [
'from' => 'dmt.akyol#gmail.com',
'to' => 'dmt.akyol#gmail.com',
'subject' => 'Your Link To Login!',
'text' => 'hello',
]);
it works but I want to send a view (blade) and I don't know how to do.
My code is:
public function build(array $customer)
{
return view('link')->with([
'customer'=> $customer,
]);
}
public function sendContactForm(array $customer)
{
$aaa=$this->build($customer);
$mg = Mailgun::create('xxxxxx')
$mg->messages()->send('xxxx'), [
'from' => $customer['customerEmail'],
'to' => ' dmt.akyol#gmail.com',
'subject' => 'Contact Message',
'html' => $aaa,
]);
}
This does not work when I write html or text.
What should I do?
Add ->render() to your build call to store the contents of the view as a string:
public function build(array $customer)
{
return view('link')->with([
'customer'=> $customer,
])->render();
}
Related
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use Auth,Str,Storage,Image,DB;
use GuzzleHttp\Client;
class SmsController extends Controller
{
//
public function index()
{
//
$data =[
'title'=>$description='Envoie SMS',
'description'=>$description,
//'membre'=>$membre,
//'membres'=>$membres,
//'departement'=>$departement,
];
return view('sms.index',$data);
}
public function envoyer(Request $request)
{
//
$senderid=$request->senderid;
$numero=$request->numero;
$message=$request->message;
$dateheure=date('Y-m-d h:i:s', time());
//dd($dateheure);
$client = new Client();
$res = $client->request('POST', 'http://africasmshub.mondialsms.net/api/api_http.php', [
'form_params' => [
'username' => 'test',
'password' => '0758224162',
'sender' => 'test',
'text' => 'Hello World!',
'type' => 'text',
'numero' => $numero,
'message' => $message,
'datetime' => $dateheure,
]
]);
//dd($res);
$success = 'message envoyé avec succès';
return redirect()->route('sms')->withSuccess($success);
}
}
Hello everyone ! I have a problem with Laravel, I cannot make an Http request to the Http address of an API which should allow me to send SMS. I am sending you as attachments the source code of my Controller "Smscontroller.php". I need your help. Thank you !
How can i send auth header, when test codeception rest api?
What i have now:
Yii2 project
"codeception/module-yii2": "^1.0.0"
"codeception/module-rest": "^1.3"
Generated test class by command codecept generate:cest api TestName
My class with test
class CreateWorkspaceCest
{
public function _before(ApiTester $I)
{
}
public function successCreate(ApiTester $I)
{
$title = 'create test';
$description = 'test description';
$I->sendPost('/workspace/create', [
'title' => $title,
'description' => $description,
]);
$I->seeResponseCodeIs(\Codeception\Util\HttpCode::OK); // 200
$I->seeResponseIsJson();
$I->seeResponseContainsJson([
'title' => $title,
'description' => $description,
'status' => 'active',
]);
}
}
Now it fails with 403 code, because backend expects header JWT-Key: <TOKEN>
How can i send auth header in sendPost
And where it is better to store auth token in one place to avoid code duplication, during writing tests?
Codeception has a method called haveHttpHeader you can add any header using it.
This is documented half-way down this page. There is also a section on authorization on this other page.
There are a few built-in authorization methods, like amBearerAuthenticated, amAWSAuthenticated, but I believe that there isn't a specific method for JWT.
class CreateWorkspaceCest
{
public function _before(ApiTester $I)
{
}
public function successCreate(ApiTester $I)
{
$title = 'create test';
$description = 'test description';
// You can add any header like this:
$I->haveHttpHeader('Content-Type', 'application/json');
$I->haveHttpHeader('Authorization', 'Bearer user-one-access-token');
// To add the header that you show in the question, you can use:
$I->haveHttpHeader('JWT-Key', '<TOKEN>');
$I->sendPost('/workspace/create', [
'title' => $title,
'description' => $description,
]);
$I->seeResponseCodeIs(\Codeception\Util\HttpCode::OK); // 200
$I->seeResponseIsJson();
$I->seeResponseContainsJson([
'title' => $title,
'description' => $description,
'status' => 'active',
]);
}
}
Good day
I'm using Mailgun to send emails to the users with required info from the order after submitting the form,I managed to send the subject and the email address ,but I'm having trouble sending the random number that gets assigned on the creation of the order.
here is my controller:
public function store(Request $request)
{
$order = $user->orders()->create([
'randomid' => rand(100000,999999),
'subject' => $request->get('subject'),
'email' => $request->get('email'),
]);
$data = $request->only('subject', 'email', 'randomid');
Mail::send('emails.note',
$data
, function($message) use ($data)
{
$message->subject('New Order: '.$data['subject'])
->from('myemail#myserver.com')
->to($data['email']);
});
}
I saw that you get the $data from request object
$data = $request->only('subject', 'email', 'randomid');
but randomid was generated in created method
$order = $user->orders()->create([
'randomid' => rand(100000,999999),
'subject' => $request->get('subject'),
'email' => $request->get('email'),
]);
So there is not randomid in request.
I think you should get $data from $order like the folowing:
$data = $order->toArray();
So You will have:
public function store(Request $request)
{
$order = $user->orders()->create([
'randomid' => rand(100000,999999),
'subject' => $request->get('subject'),
'email' => $request->get('email'),
]);
$data = $order->toArray();
Mail::send('emails.note',
$data
, function($message) use ($data)
{
$message->subject('New Order: '.$data['subject'])
->from('myemail#myserver.com')
->to($data['email']);
});
}
The randomid doesn't come from the request. You generate its value manually using rand(100000,999999) !
Please try this:
public function store(Request $request){
$data = [
'randomid' => rand(100000, 999999) ,
'subject' => $request->input('subject') ,
'email' => $request->input('email')
];
$order = $user->orders()->create($data);
Mail::send('emails.note', $data, function ($message) use($data) {
$message->subject('New Order: ' . $data['subject'])
->from('myemail#myserver.com')->to($data['email']);
});
}
Have you any idea how to control the queue of Mandrill send emails in PHP ?
Well, I'm using Symfony as a Framework, & this is my send function in the Mandrill class :
public function sendMandrill()
{
$listTo = array();
foreach ($this->contacts as $contact)
{
$listTo[] = [
'name' => $contact->getFname(),
'email' => $contact->getEmail()
];
}
var_dump($listTo);
$email = array(
'html' => $this->message->getBodyHtml(),
'text' => $this->message->getBodyText(),
'subject' => $this->message->getSubject(),
'from_email' => $this->apiDom,
'from_name' => $this->message->getFromName(),
'to' => $listTo,
"preserve_recipients"=> false,
);
$this->senderAPI = new \Mandrill("$this->apiKey");
return $this->senderAPI->messages->send($email);
}
Now, I want to create a function so I can pause the sending for a while so I can change such things, then resume it whenever I want or may be to stop it at all !
which mean there will be 3 functions as following :
public function pauseMandrill()
{
...
}
public function resumeMandrill()
{
...
}
public function stopMandrill()
{
...
}
I need help to figure out how to set the reply-to field in app/config/mail.php. I'm using Laravel 4 and it's not working. This is my app/config/mail.php:
<?php
return array(
'driver' => 'smtp',
'host' => 'smtp.gmail.com',
'port' => 587,
'from' => [
'address' => 'sender#domain.com',
'name' => 'E-mail 1'
],
'reply-to' => [
'address' => 'replyto#domain.com',
'name' => 'E-mail 2'
],
'encryption' => 'tls',
'username' => 'sender#domain.com',
'password' => 'pwd',
'pretend' => false,
);
Pretty sure it doesn't work this way. You can set the "From" header in the config file, but everything else is passed during the send:
Mail::send('emails.welcome', $data, function($message)
{
$message->to('foo#example.com', 'John Smith')
->replyTo('reply#example.com', 'Reply Guy')
->subject('Welcome!');
});
FWIW, the $message passed to the callback is an instance of Illuminate\Mail\Message, so there are various methods you can call on it:
->from($address, $name = null)
->sender($address, $name = null)
->returnPath($address)
->to($address, $name = null)
->cc($address, $name = null)
->bcc($address, $name = null)
->replyTo($address, $name = null)
->subject($subject)
->priority($level)
->attach($file, array $options = array())
->attachData($data, $name, array $options = array())
->embed($file)
->embedData($data, $name, $contentType = null)
Plus, there is a magic __call method, so you can run any method that you would normally run on the underlying SwiftMailer class.
It's possible since Laravel 5.3 to add a global reply. In your config/mail.php file add the following:
'reply_to' => [
'address' => 'info#xxxxx.com',
'name' => 'Reply to name',
],
I'm using mailable and in my App\Mail\NewUserRegistered::class on the build function I'm doing this,
public function build()
{
return $this->replyTo('email', $name = 'name')
->markdown('emails.admin_suggestion');
}
Another option is to customizing SwiftMailer Message - https://laravel.com/docs/8.x/mail#customizing-the-swiftmailer-message.
->withSwiftMessage(function ($message) use ($senderEmail) {
$message->getHeaders()
->addTextHeader('Return-Path', $senderEmail);
})