Preview Mail Notification in browser - php

With Laravel, according to the documentation, I can return a Mailable via a controller to display it in the browser. It helps to preview mails.
Is there a way to preview Mail Notifications in browser?
I tried:
return (new MyNotification())->toMail($some_user);
But it does not work:
The Response content must be a string or object implementing __toString(), "object" given.

In your controller's function :
$message = (new \App\Notifications\MyNotification())->toMail('example#gmail.com');
$markdown = new \Illuminate\Mail\Markdown(view(), config('mail.markdown'));
return $markdown->render('vendor.notifications.email', $message->data());
Just change the name of the notification class (and also pass arguments if necessary) and hit the url in your browser to see the preview.

You can't render Notification. You can render Mailable that you use in toMail(). For example if that Mailable is called SomeMailable:
public function toMail($user)
{
return (new SomeMailable($user))->to($user->email);
}
Then you can render the Mailable with:
return new SomeMailable($some_user);

In Laravel 5.8 you can now preview it just like you would a Mailable.
Route::get('mail-preview', function () {
return (new MyNotification())->toMail($some_user);
});
More details here:
https://sampo.co.uk/blog/previewing-mail-notifications-in-laravel-just-got-easier

For me, with the particular notification that I wanted to preview toMail() method required a Notifiable instance rather than just an email address, so the following code was what worked for me:
$notification = new \Illuminate\Auth\Notifications\VerifyEmail();
$user = \App\User::where('email', 'example#gmail.com')->first(); // Model with Notifiable trait
$message = $notification->toMail($user);
$markdown = new \Illuminate\Mail\Markdown(view(), config('mail.markdown'));
return $markdown->render('vendor.notifications.email', $message->toArray());

Try this (Test pass after Laravel 5.6)
$message = (new \App\Notifications\YourNotification()->toMail($notifiable);
return app()->make(\Illuminate\Mail\Markdown::class)->render($message->markdown, $message->data());

Related

Creating a Google Map in Laravel,

I try to display Google Map which is display an address from database(I have address column in DB table). For this I made a blade, and bind to route and path it with controller. I am having display issue.
My step is right below. I used this google API.
https://github.com/farhanwazir/laravelgooglemaps
And set the route:
Route::get('/show', 'PagesController#map');
Set the controller:
public function map(){
$config['center'] = allestates::where('address')->get();
$config['zoom'] = '10';
$config['map_width'] = '300px';
$config['scrollwheel'] = false;
GMaps::initialize($config);
$map = GMaps::create_map();
return view('pages.show',[ 'map' => $map]);
}
And in my blade. This is how I am calling it in body tag.
{{$map['html']}}
But getting this error.
Non-static method FarhanWazir\GoogleMaps\GMaps::initialize() should
not be called statically
Any idea what the problem is?
This code works for me:
$gmap = new GMaps();
$gmap->initialize($config);
$map = $gmap->create_map();
return view('your_view', compact('map'));
Notice that $gmap->create_map() is not a static calling.
Try to do it like this
return view('pages.show',[ 'map' => $map]);
return view(pages.show)->with(['map'=> $map]);
Also check if the value is not empty
Goodluck

How to create global object Laravel

I'm using Laravel 5.6 and Instagram API library.
To work with this Instagram API I need to create object $ig = new \InstagramAPI\Instagram(). And then for getting any user's information I must use $ig->login('username', 'password') every time.
So I don't want to use this function all the time. The first I want to create a global variable which will contain $ig = new \InstagramAPI\Instagram(). However, I don't know how to correctly do it.
I tried to use singleton:
$this->app->singleton(Instagram::class, function ($app) {
Instagram::$allowDangerousWebUsageAtMyOwnRisk = true; // As wiki says
return new Instagram();
});
When I called $ig->login('name', 'pass') in any method all user profile's information changed in this object, but then if I call dd($ig = app(Instagram::class)) in another Controller method I see that previous data did not save. "WTF?" - I said.
Someone tells me that singleton just promise me that there won't be created the same object, but it does not save any changes.
I tried to use sessions:
However, when I tried to set variable with object as value anything did not happen.
$ig = new \InstagramAPI\Instagram();
session(['ig' => $ig]);
I think it's because of I tried to put a large object. And from the other hand it's not secure method!
Just let me know:
How can I create an object which I could use in every method with saving change for the next actions?
When I called $ig->login('name', 'pass') in any method all user profile's information changed in this object, but then if I call dd($ig = app(Instagram::class)) in another Controller method I see that previous data did not save.
That is the correct behavior. When a new request is sent to Laravel, a new instance of the Instagram is created. I'm not sure if you understand the meaning of a singleton but in terms of Laravel, there is one instance per HTTP request.
Since the Instagram API you're using does not contain functionality to relogin, I created a class (that would be place in the app/Classes folder).
<?php
namespace App\Classes;
use InstagramAPI\Instagram;
use InstagramAPI\Response\LoginResponse;
class CustomInstagram extends Instagram {
public function relogin(LoginResponse $response) {
$appRefreshInterval = 1800;
$this->_updateLoginState($response);
$this->_sendLoginFlow(true, $appRefreshInterval);
return $this;
}
}
Change the singleton instance so it uses the App\Classes\CustomInstagram class.
$this->app->singleton(Instagram::class, function ($app) {
Instagram::$allowDangerousWebUsageAtMyOwnRisk = true; // As wiki says
return new App\Classes\CustomInstagram();
});
In order to use the Instagram object with an authenticated user, the login information will need to be persisted some how. This would be placed where the login is occurring.
try {
$response = app(Instagram::class)->login($username, $password);
if ($response->isTwoFactorRequired()) {
// Need to handle if 2fa is needed (we're not completely logged in yet)
}
// Can use session to persist \InstagramAPI\Response\LoginResponse but I'd recommend the database.
session(['igLoginResponse' => serialize($response)]);
} catch (\Exception $e) {
// Login failed
}
Then create a middleware to relogin the user to Instagram (if the login response exists). You need to register this as described here. Then, the Instagram singleton can be used in your controller.
namespace App\Http\Middleware;
use Closure;
use InstagramAPI\Instagram;
class InstagramLogin
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
$responseSerialized = session('igLoginResponse');
if (!is_null($responseSerialized)) {
$ig = app(Instagram::class);
$response = unserialize($responseSerialized);
$ig->relogin($response);
}
return $next($request);
}
}

Mocking a service called by a controller from a WebTestCase

I have an API written using Symfony2 that I'm trying to write post hoc tests for. One of the endpoints uses an email service to send a password reset email to the user. I'd like to mock out this service so that I can check that the right information is sent to the service, and also prevent an email from actually being sent.
Here's the route I'm trying to test:
/**
* #Route("/me/password/resets")
* #Method({"POST"})
*/
public function requestResetAction(Request $request)
{
$userRepository = $this->get('app.repository.user_repository');
$userPasswordResetRepository = $this->get('app.repository.user_password_reset_repository');
$emailService = $this->get('app.service.email_service');
$authenticationLimitsService = $this->get('app.service.authentication_limits_service');
$now = new \DateTime();
$requestParams = $this->getRequestParams($request);
if (empty($requestParams->username)) {
throw new BadRequestHttpException("username parameter is missing");
}
$user = $userRepository->findOneByUsername($requestParams->username);
if ($user) {
if ($authenticationLimitsService->isUserBanned($user, $now)) {
throw new BadRequestHttpException("User temporarily banned because of repeated authentication failures");
}
$userPasswordResetRepository->deleteAllForUser($user);
$reset = $userPasswordResetRepository->createForUser($user);
$userPasswordResetRepository->saveUserPasswordReset($reset);
$authenticationLimitsService->logUserAction($user, UserAuthenticationLog::ACTION_PASSWORD_RESET, $now);
$emailService->sendPasswordResetEmail($user, $reset);
}
// We return 201 Created for every request so that we don't accidently
// leak the existence of usernames
return $this->jsonResponse("Created", $code=201);
}
I then have an ApiTestCase class that extends the Symfony WebTestCase to provide helper methods. This class contains a setup method that tries to mock the email service:
class ApiTestCase extends WebTestCase {
public function setup() {
$this->client = static::createClient(array(
'environment' => 'test'
));
$mockEmailService = $this->getMockBuilder(EmailService::class)
->disableOriginalConstructor()
->getMock();
$this->mockEmailService = $mockEmailService;
}
And then in my actual test cases I'm trying to do something like this:
class CreatePasswordResetTest extends ApiTestCase {
public function testSendsEmail() {
$this->mockEmailService->expects($this->once())
->method('sendPasswordResetEmail');
$this->post(
"/me/password/resets",
array(),
array("username" => $this->user->getUsername())
);
}
}
So now the trick is to get the controller to use the mocked version of the email service. I have read about several different ways to achieve this, so far I've not had much luck.
Method 1: Use container->set()
See How to mock Symfony 2 service in a functional test?
In the setup() method tell the container what it should return when it's asked for the email service:
static::$kernel->getContainer()->set('app.service.email_service', $this->mockEmailService);
# or
$this->client->getContainer()->set('app.service.email_service', $this->mockEmailService);
This does not effect the controller at all. It still calls the original service. Some write ups I've seen mention that the mocked service is 'reset' after a single call. I'm not even seeing my first call mocked out so I'm not certain this issue is affecting me yet.
Is there another container I should be calling set on?
Or am I mocking out the service too late?
Method 2: AppTestKernel
See: http://blog.lyrixx.info/2013/04/12/symfony2-how-to-mock-services-during-functional-tests.html
See: Symfony2 phpunit functional test custom user authentication fails after redirect (session related)
This one pulls me out of my depth when it comes to PHP and Symfony2 stuff (I'm not really a PHP dev).
The goal seems to be to change some kind of foundation class of the website to allow my mock service to be injected very early in the request.
I have a new AppTestKernel:
<?php
// app/AppTestKernel.php
require_once __DIR__.'/AppKernel.php';
class AppTestKernel extends AppKernel
{
private $kernelModifier = null;
public function boot()
{
parent::boot();
if ($kernelModifier = $this->kernelModifier) {
$kernelModifier($this);
$this->kernelModifier = null;
};
}
public function setKernelModifier(\Closure $kernelModifier)
{
$this->kernelModifier = $kernelModifier;
// We force the kernel to shutdown to be sure the next request will boot it
$this->shutdown();
}
}
And a new method in my ApiTestCase:
// https://stackoverflow.com/a/19705215
protected static function getKernelClass(){
$dir = isset($_SERVER['KERNEL_DIR']) ? $_SERVER['KERNEL_DIR'] : static::getPhpUnitXmlDir();
$finder = new Finder();
$finder->name('*TestKernel.php')->depth(0)->in($dir);
$results = iterator_to_array($finder);
if (!count($results)) {
throw new \RuntimeException('Either set KERNEL_DIR in your phpunit.xml according to http://symfony.com/doc/current/book/testing.html#your-first-functional-test or override the WebTestCase::createKernel() method.');
}
$file = current($results);
$class = $file->getBasename('.php');
require_once $file;
return $class;
}
Then I alter my setup() to use the kernel modifier:
public function setup() {
...
$mockEmailService = $this->getMockBuilder(EmailService::class)
->disableOriginalConstructor()
->getMock();
static::$kernel->setKernelModifier(function($kernel) use ($mockEmailService) {
$kernel->getContainer()->set('app.service.email_service', $mockEmailService);
});
$this->mockEmailService = $mockEmailService;
}
This works! However I now can't access the container in my other tests when I'm trying to do something like this:
$c = $this->client->getKernel()->getContainer();
$repo = $c->get('app.repository.user_password_reset_repository');
$resets = $repo->findByUser($user);
The getContainer() method returns null.
Should I be using the container differently?
Do I need to inject the container into the new kernel? It extends the original kernel so I don't really know why/how it's any different when it comes to the container stuff.
Method 3: Replace the service in config_test.yml
See: Symfony/PHPUnit mock services
This method requires that I write a new service class that overrides the email service. Writing a fixed mock class like this seems less useful than a regular dynamic mock. How can I test that certain methods have been called with certain parameters?
Method 4: Setup everything inside the test
Going on #Matteo's suggestion I wrote a test that did this:
public function testSendsEmail() {
$mockEmailService = $this->getMockBuilder(EmailService::class)
->disableOriginalConstructor()
->getMock();
$mockEmailService->expects($this->once())
->method('sendPasswordResetEmail');
static::$kernel->getContainer()->set('app.service.email_service', $mockEmailService);
$this->client->getContainer()->set('app.service.email_service', $mockEmailService);
$this->post(
"/me/password/resets",
array(),
array("username" => $this->user->getUsername())
);
}
This test fails because the expected method sendPasswordResetEmail wasn't called:
There was 1 failure:
1) Tests\Integration\Api\MePassword\CreatePasswordResetTest::testSendsEmail
Expectation failed for method name is equal to <string:sendPasswordResetEmail> when invoked 1 time(s).
Method was expected to be called 1 times, actually called 0 times.
Thanks to Cered's advice I've managed to get something working that can test that the emails I expect to be sent actually are. I haven't been able to actually get the mocking to work so I'm a bit reluctant to mark this as "the" answer.
Here's a test that checks that an email is sent:
public function testSendsEmail() {
$this->client->enableProfiler();
$this->post(
"/me/password/resets",
array(),
array("username" => $this->user->getUsername())
);
$mailCollector = $this->client->getProfile()->getCollector('swiftmailer');
$this->assertEquals(1, $mailCollector->getMessageCount());
$collectedMessages = $mailCollector->getMessages();
$message = $collectedMessages[0];
$this->assertInstanceOf('Swift_Message', $message);
$this->assertEquals('Reset your password', $message->getSubject());
$this->assertEquals('info#example.com', key($message->getFrom()));
$this->assertEquals($this->user->getEmail(), key($message->getTo()));
$this->assertContains(
'This link is valid for 24 hours only.',
$message->getBody()
);
$resets = $this->getResets($this->user);
$this->assertContains(
$resets[0]->getToken(),
$message->getBody()
);
}
It works by enabling the Symfony profiler and inspecting the swiftmailer service. It's documented here: http://symfony.com/doc/current/email/testing.html

Symfony 2 Class Constructer Not Getting ContainerInterface?

ok, So I am still a little new to Symfony 2 but I have been all over the web trying to see what I am doing wrong, but going over and over the Symfony docs, I can't see why its not working.
So I have tow points in my app that will need to send two new user emails, one with a password and one with an active path to verify the email. I am using MailGun to send the emails, this works fine. But has I have two controllers that will send these emails, I thought that if I wanted to change/edit them, it would be best if they where in the same place. So I build my own class for them to go in.
This is the problem, as it is not an controller I am trying to 'render' an Standard Email template layout. And for the life of me can not figure out why its not working.
So my Class:
namespace xxxBundle\Classes;
//use Symfony\Component\Templating\EngineInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class NewUserEmails {
//private $templating;
//public function __construct(EngineInterface $templating) {
// $this->templating = $templating;
//}
private $container;
public function __construct(ContainerInterface $container) {
$this->templating = $container->get('templating');
}
public function SendActivePathEmail($GetNewUserID, $Token) {
/* Send Active Path To New User - Before Password */
$Email_Header_ActiveUser = 'Access';
$Email_Content_ActiveUser = 'Please active your account, by clicking the link below.';
$Email_Content_ActiveUser .= '<br/><br/><strong>PLEASE NOTE:</strong> Your account is not active untill you have vifyied your email';
$Email_Content_ActiveUser .= '<br/><br/><p>Active Account Link</p>';
$ActiveUserEmail = $this->templating->render('xxxBundle:Emails:StandardEmail.html.twig',
['EmailContent' => $Email_Header_ActiveUser,
'EmailMessage' => $Email_Content_ActiveUser],
'text/html');
return $ActiveUserEmail;
}
public function SendPswEmail($PlainPwd) {
/* Send Password To New User */
$Email_Header_NewPsw = 'Access';
$Email_Content_NewPsw = 'This is your password <strong>' .$PlainPwd. '</strong> for xxxxx login';
$PasswordEmail = $this->templating->render('xxxBundle:Emails:StandardEmail.html.twig',
['EmailContent' => $Email_Header_NewPsw,
'EmailMessage' => $Email_Content_NewPsw],
'text/html');
return $PasswordEmail;
}
} //Class End
Now this is what I have in my services YML file,
new_user_emails:
class: xxxBundle\Classes\NewUserEmails
arguments: [#service_container]
This is the services file within my bundle, which I know is being loaded has I have a login handler which works without any problems.
And this is how I am calling the class within my Controller,
$GetNewUserEmails = new NewUserEmails();
$ActiveUserEmail = $GetNewUserEmails->SendActivePathEmail($GetNewUserID, $Token);
$PasswordEmail = $GetNewUserEmails->SendPswEmail($PlainPwd);
So has far as I can tell I am doing it right, but when I try to save the user (which does save without any problems) I get the following error,
Catchable Fatal Error: Argument 1 passed to xxxxBundle\Classes\NewUserEmails::__construct() must implement interface Symfony\Component\DependencyInjection\ContainerInterface, none given
P.S. I have tried to insert just the tempting, but that gave me the same error!
All help most welcome,
What am I doing wrong?
Thanks,
You need to retrieve your service from the symfony container.
try this:
$GetNewUserEmails = $this->get('new_user_emails');
$ActiveUserEmail = $GetNewUserEmails->SendActivePathEmail($GetNewUserID, $Token);
$PasswordEmail = $GetNewUserEmails->SendPswEmail($PlainPwd);
Instead of this:
$GetNewUserEmails = new NewUserEmails();
$ActiveUserEmail = $GetNewUserEmails->SendActivePathEmail($GetNewUserID, $Token);
$PasswordEmail = $GetNewUserEmails->SendPswEmail($PlainPwd);
PS: Is not a good practice to pass the whole container to the service, but only the service it really needed.
Hope this help

Laravel mail: pass string instead of view

I want to send a confirmation e-mail using laravel.
The laravel Mail::send() function only seems to accept a path to a file on the system.
The problem is that my mailtemplates are stored in the database and not in a file on the system.
How can I pass plain content to the email?
Example:
$content = "Hi,welcome user!";
Mail::send($content,$data,function(){});
update on 7/20/2022: For more current versions of Laravel, the setBody() method in the Mail::send() example below has been replaced with the text() or html() methods.
update: In Laravel 5 you can use raw instead:
Mail::raw('Hi, welcome user!', function ($message) {
$message->to(..)
->subject(..);
});
This is how you do it:
Mail::send([], [], function ($message) {
$message->to(..)
->subject(..)
// here comes what you want
->setBody('Hi, welcome user!'); // assuming text/plain
// or:
->setBody('<h1>Hi, welcome user!</h1>', 'text/html'); // for HTML rich messages
});
For Html emails
Mail::send(array(), array(), function ($message) use ($html) {
$message->to(..)
->subject(..)
->from(..)
->setBody($html, 'text/html');
});
It is not directly related to the question, but for the ones that search for setting the plain text version of your email while keeping the custom HTML version, you can use this example :
Mail::raw([], function($message) {
$message->from('contact#company.com', 'Company name');
$message->to('johndoe#gmail.com');
$message->subject('5% off all our website');
$message->setBody( '<html><h1>5% off its awesome</h1><p>Go get it now !</p></html>', 'text/html' );
$message->addPart("5% off its awesome\n\nGo get it now!", 'text/plain');
});
If you would ask "but why not set first argument as plain text ?", I made a test and it only takes the html part, ignoring the raw part.
If you need to use additional variable, the anonymous function will need you to use use() statement as following :
Mail::raw([], function($message) use($html, $plain, $to, $subject, $formEmail, $formName){
$message->from($fromEmail, $fromName);
$message->to($to);
$message->subject($subject);
$message->setBody($html, 'text/html' ); // dont miss the '<html></html>' or your spam score will increase !
$message->addPart($plain, 'text/plain');
});
Hope it helps you folks.
The Mailer class passes a string to addContent which via various other methods calls views->make(). As a result passing a string of content directly won't work as it'll try and load a view by that name.
What you'll need to do is create a view which simply echos $content
// mail-template.php
<?php echo $content; ?>
And then insert your string into that view at runtime.
$content = "Hi,welcome user!";
$data = [
'content' => $content
];
Mail::send('mail-template', $data, function() { });
I had a similar issue where the HTML and/or plain text of my email were not built by a view and I didn't want to create a dummy view for them (as proposed by #Matthew Odedoyin).
As others have commented, you can use $this->html() to set the HTML content of the message, but what if you want your email to have both HTML and plain text content?
Unfortunately $this->text() only takes a view, but I got around this by using:
$this->text(new HtmlString('Here is the plain text content'));
Which renders the content of the HTMLString instead of the view.
try
public function build()
{
$message = 'Hi,welcome user!'
return $this->html($message)->subject($message);
}
as you know
Only mailables may be queued.
meaning, if you use ShouldQueue interface
1) first, you should always do
php artisan queue:restart
2) second, in your mailable you can use html method (tested in laravel 5.8)
public function build(): self
{
return $this
->html('
<html>
<body>
ForwardEmail
</body>
</html>
')
->subject(config('app.name') . ' ' . 'email forwarded')
->attachData($this->content, 'email.eml', [
'mime' => 'application/eml',
]);
}
If you were using mailables. You can do something like this in the build method :
public function build()
{
return $this->view('email')
->with(['html'=>'This is the message']);
}
And you just go ahead and create the blade view email.blade.php in your resource folder.
Then in the blade you can reference your string using laravel blade syntax
<html>
<body>
{{$html}}
</body>
</html>
or
<html>
<body>
{!!$html!!}
</body>
</html>
If your raw text contains HTML mark up
I hope this works for those who have templates stored in the database and wants to take advantage of the Mailables class in Laravel.
To send raw html, text etc using Laravel Mailables you can
override Mailable->send() in your Mailable and in there, use the method in previous responses:
send([], [], function($message){ $message->setBody() } )
No need to call $this->view() at your build function at all.
NOTE: Below answer is for those who are looking for a flexible approach. i,e (with or without laravel template)
With Template
$payload['message'] = View::make('emails.test-mail',$data)->render();
Without Template
$payload['message'] = "lorem ipsum";
Mail::raw([], function ($mail) use ($payload) {
$mail->from($payload['from_email'])
->to($payload['to'])
->setBody($payload['message'], 'text/html')
->cc($payload['cc'])
->bcc($payload['bcc'])
->subject($payload['subject']);
foreach ($payload['attachments'] as $file){
$mail->attach($file);
}
});
This can be accomplished within a Mailable implementation, with plain text and html content parts:
public function build() {
// Text and html content sections we wish to use in place of view output
$bodyHtml = ...
$bodyText = ...
// Internally, Mailer::renderView($view) interprets $view as the name of a blade template
// unless, instead of string, it is set to an object implementing Htmlable,
// in which case it returns the result $view->toHtml()
$htmlViewAlternative = new class($bodyHtml) implements Htmlable {
protected string $html;
public function __construct($html) {
$this->html = $html;
}
public function toHtml(): string {
return $this->html;
}
};
// We can now set both the html and text content sections without
// involving blade templates. One minor hitch is the Mailable::view($view)
// documents $view as being a string, which is incorrect if you follow
// the convoluted downstream logic.
/** #noinspection PhpParamsInspection */
return $this
->to(...)
->from(...)
->subject(...)
->view([
'html' => $htmlViewAlternative,
'raw' => $bodyText
]);
}
Laravel mailable now has an ->html() function to be used instead of ->view() and works both with o without ->text()
laravel 9 has built in function to send HTML without view. Here is the example:
\Illuminate\Support\Facades\Mail::html($content, function ($message) {
$message->to("email#example.com")
->subject("Test dev 4")
->from("email#example.com");
});
and also if we use accepted answer will return:
Symfony\Component\Mime\Message::setBody(): Argument #1 ($body) must be
of type ?Symfony\Component\Mime\Part\AbstractPart, string given,
called in
/Users/yaskur/Sites/laravel/mail-builder/vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php
on line 23
It's happened because laravel use new library to send email. Previously, use Swiftmailer and now use Symfony Mailer. To send HTML email without view you can also use below code:
Mail::raw("", function ($message) use ($content) {
$body = new \Symfony\Component\Mime\Part\TextPart($content);
$message->to("dyas#example.com")
->subject("Test dev")
->from("no-reply#example.com")
->setBody($body);
});

Categories